-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd6c175
commit e4cf183
Showing
28 changed files
with
597 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM microsoft/dotnet | ||
|
||
COPY / /app | ||
WORKDIR /app | ||
|
||
RUN dotnet restore Jobs | ||
RUN dotnet build Jobs | ||
|
||
RUN dotnet restore WebRole | ||
RUN dotnet build WebRole | ||
|
||
WORKDIR /app/WebRole | ||
|
||
ENTRYPOINT ["dotnet", "run"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM microsoft/dotnet | ||
|
||
COPY / /app | ||
WORKDIR /app | ||
|
||
RUN dotnet restore Jobs | ||
RUN dotnet build Jobs | ||
|
||
RUN dotnet restore WorkerRole | ||
RUN dotnet build WorkerRole | ||
|
||
WORKDIR /app/WorkerRole | ||
|
||
ENTRYPOINT ["dotnet", "run"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Jobs | ||
{ | ||
public class ComplexFactory | ||
{ | ||
public Task<ComplexObject> CreateSomethingComplicated(string name) | ||
{ | ||
return Task.FromResult(new ComplexObject() | ||
{ | ||
Name = name | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Jobs | ||
{ | ||
public class ComplexObject | ||
{ | ||
public int Whistles { get; set; } = 4; | ||
public int Bells { get; set; } = 10; | ||
public string Name { get; set; } = string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Jobs | ||
{ | ||
public interface IMessageDispatcher | ||
{ | ||
Task Dispatch(QueueMessage message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Jobs | ||
{ | ||
public interface IQueueListener | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Jobs | ||
{ | ||
public interface IQueueSender | ||
{ | ||
Task SendAsync(QueueMessage queueMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>149fb2f0-e14b-4561-b88a-a2f590a63aa3</ProjectGuid> | ||
<RootNamespace>Jobs</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Jobs | ||
{ | ||
public class MessageDispatcher : IMessageDispatcher | ||
{ | ||
private readonly IQueueSender client; | ||
|
||
public MessageDispatcher(IQueueSender client) { | ||
this.client = client; | ||
} | ||
|
||
public async Task Dispatch(QueueMessage message) | ||
{ | ||
if (message.Topic == "web") | ||
{ | ||
var body = message.GetBody<string>(); | ||
|
||
Console.WriteLine($"[{DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss")}] -> received message from web: {body}"); | ||
|
||
var complexObject = await new ComplexFactory().CreateSomethingComplicated(body); | ||
|
||
await client.SendAsync(new QueueMessage("worker", complexObject)); | ||
} | ||
else if (message.Topic == "worker") | ||
{ | ||
var body = message.GetBody<ComplexObject>(); | ||
|
||
Console.WriteLine($"[{DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss")}] -> received message from worker: {body.Name} with {body.Bells} bells and {body.Whistles} whistles."); | ||
} | ||
else | ||
{ | ||
throw new Exception($"Unexpected message {message.Topic}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("Jobs")] | ||
[assembly: AssemblyTrademark("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("149fb2f0-e14b-4561-b88a-a2f590a63aa3")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using Amqp; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Jobs | ||
{ | ||
public abstract class QueueConnection : IDisposable | ||
{ | ||
private Session oldSession; | ||
private Connection oldConnection; | ||
|
||
private Session session; | ||
private Connection connection; | ||
|
||
private CancellationTokenSource cancellationTokenSource; | ||
|
||
private readonly Address address; | ||
|
||
protected abstract void Renew(Session session); | ||
|
||
protected QueueConnection() | ||
{ | ||
address = new Address(Environment.GetEnvironmentVariable("SERVICE_BUS_URL"), | ||
5671, | ||
Environment.GetEnvironmentVariable("SERVICE_BUS_SAK_POLICYNAME"), | ||
Environment.GetEnvironmentVariable("SERVICE_BUS_SAK_SHAREDSECRET")); | ||
|
||
cancellationTokenSource = new CancellationTokenSource(); | ||
|
||
renewSession(); | ||
} | ||
private void renewSession() | ||
{ | ||
oldConnection = connection; | ||
oldSession = session; | ||
|
||
connection = new Connection(address); | ||
session = new Session(connection); | ||
|
||
Renew(session); | ||
|
||
if (oldSession != null) | ||
{ | ||
oldSession.Close(); | ||
oldSession = null; | ||
} | ||
|
||
if (oldConnection != null) | ||
{ | ||
oldConnection.Close(); | ||
oldConnection = null; | ||
} | ||
|
||
startRenewTimer(); | ||
} | ||
|
||
private void startRenewTimer() | ||
{ | ||
delayedRenewSession(cancellationTokenSource.Token); | ||
} | ||
|
||
private async Task delayedRenewSession(CancellationToken token) | ||
{ | ||
await Task.Delay(5 * 60 * 1000, token); | ||
|
||
if (!token.IsCancellationRequested) | ||
renewSession(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
cancellationTokenSource.Cancel(); | ||
|
||
if (oldSession != null) | ||
{ | ||
oldSession.Close(); | ||
} | ||
|
||
if (oldConnection != null) | ||
{ | ||
oldConnection.Close(); | ||
} | ||
|
||
if (session != null) | ||
{ | ||
session.Close(); | ||
} | ||
|
||
if (connection != null) | ||
{ | ||
connection.Close(); | ||
} | ||
|
||
cancellationTokenSource.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Amqp; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Jobs | ||
{ | ||
public class QueueListener : QueueConnection, IQueueListener, IDisposable | ||
{ | ||
private ReceiverLink oldReceiver; | ||
private ReceiverLink receiver; | ||
|
||
public Action<QueueMessage> OnMessage { get; set; } | ||
|
||
public QueueListener(Action<QueueMessage> onMessage) | ||
{ | ||
OnMessage = onMessage; | ||
} | ||
|
||
protected override void Renew(Session session) | ||
{ | ||
oldReceiver = receiver; | ||
|
||
receiver = new ReceiverLink(session, "receiver", Environment.GetEnvironmentVariable("SERVICE_BUS_NAME")); | ||
|
||
receiver.Start(20, (receiver, message) => | ||
{ | ||
Console.WriteLine($"[{DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss")}] -> received message"); | ||
|
||
try | ||
{ | ||
var queueMessage = JsonConvert.DeserializeObject<QueueMessage>(message.GetBody<string>()); | ||
|
||
OnMessage(queueMessage); | ||
|
||
receiver.Accept(message); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine($"[{DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss")}] -> message threw an exception: {e.Message}"); | ||
|
||
receiver.Reject(message); | ||
} | ||
}); | ||
|
||
if (oldReceiver != null) | ||
{ | ||
oldReceiver.Close(); | ||
oldReceiver = null; | ||
} | ||
} | ||
public new void Dispose() | ||
{ | ||
if (oldReceiver != null) | ||
{ | ||
oldReceiver.Close(); | ||
} | ||
|
||
if (receiver != null) | ||
{ | ||
receiver.Close(); | ||
} | ||
|
||
base.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Jobs | ||
{ | ||
public class QueueMessage | ||
{ | ||
public QueueMessage() { } | ||
|
||
public QueueMessage(string topic, object body) | ||
: this() | ||
{ | ||
Topic = topic; | ||
Body = JsonConvert.SerializeObject(body); | ||
} | ||
|
||
public string Topic { get; set; } | ||
public string Body { get; set; } | ||
|
||
public T GetBody<T>() | ||
{ | ||
return JsonConvert.DeserializeObject<T>(Body); | ||
} | ||
|
||
public void SetBody<T>(T body) | ||
{ | ||
Body = JsonConvert.SerializeObject(body); | ||
} | ||
} | ||
} |
Oops, something went wrong.