Skip to content

Commit

Permalink
Add solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
duizendnegen committed Oct 11, 2016
1 parent bd6c175 commit e4cf183
Show file tree
Hide file tree
Showing 28 changed files with 597 additions and 241 deletions.
12 changes: 12 additions & 0 deletions WebRole.sln → DockerWebWorkerRole.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "WebRole", "WebRole\WebRole.xproj", "{8866C8B2-C92B-4CDA-B1B0-3590882DEF73}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "WorkerRole", "WorkerRole\WorkerRole.xproj", "{8C8A9176-0DD2-4AE5-9FD7-DE2416AF50E2}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Jobs", "Jobs\Jobs.xproj", "{149FB2F0-E14B-4561-B88A-A2F590A63AA3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +19,14 @@ Global
{8866C8B2-C92B-4CDA-B1B0-3590882DEF73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8866C8B2-C92B-4CDA-B1B0-3590882DEF73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8866C8B2-C92B-4CDA-B1B0-3590882DEF73}.Release|Any CPU.Build.0 = Release|Any CPU
{8C8A9176-0DD2-4AE5-9FD7-DE2416AF50E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C8A9176-0DD2-4AE5-9FD7-DE2416AF50E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C8A9176-0DD2-4AE5-9FD7-DE2416AF50E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C8A9176-0DD2-4AE5-9FD7-DE2416AF50E2}.Release|Any CPU.Build.0 = Release|Any CPU
{149FB2F0-E14B-4561-B88A-A2F590A63AA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{149FB2F0-E14B-4561-B88A-A2F590A63AA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{149FB2F0-E14B-4561-B88A-A2F590A63AA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{149FB2F0-E14B-4561-B88A-A2F590A63AA3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
14 changes: 14 additions & 0 deletions Dockerfile.web
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"]
14 changes: 14 additions & 0 deletions Dockerfile.worker
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"]
15 changes: 15 additions & 0 deletions Jobs/ComplexFactory.cs
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
});
}
}
}
9 changes: 9 additions & 0 deletions Jobs/ComplexObject.cs
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;
}
}
9 changes: 9 additions & 0 deletions Jobs/IMessageDispatcher.cs
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);
}
}
6 changes: 6 additions & 0 deletions Jobs/IQueueListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Jobs
{
public interface IQueueListener
{
}
}
9 changes: 9 additions & 0 deletions Jobs/IQueueSender.cs
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);
}
}
21 changes: 21 additions & 0 deletions Jobs/Jobs.xproj
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>
40 changes: 40 additions & 0 deletions Jobs/MessageDispatcher.cs
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}");
}
}
}
}
19 changes: 19 additions & 0 deletions Jobs/Properties/AssemblyInfo.cs
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")]
98 changes: 98 additions & 0 deletions Jobs/QueueConnection.cs
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();
}
}
}
68 changes: 68 additions & 0 deletions Jobs/QueueListener.cs
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();
}
}
}
33 changes: 33 additions & 0 deletions Jobs/QueueMessage.cs
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);
}
}
}
Loading

0 comments on commit e4cf183

Please sign in to comment.