Skip to content

Commit

Permalink
Durable Task Samples
Browse files Browse the repository at this point in the history
  • Loading branch information
samarabbas committed Jan 19, 2015
1 parent d3e610c commit 07b039a
Show file tree
Hide file tree
Showing 35 changed files with 1,850 additions and 0 deletions.
42 changes: 42 additions & 0 deletions DurableTaskSamples.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DurableTaskSamples", "Samples\DurableTaskSamples\DurableTaskSamples.csproj", "{BC204374-927C-4A48-8473-F5BF8C97EAEA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Framework", "Framework\Framework.csproj", "{6F5D2EAD-726D-4FE5-A575-AEB96D1CCE37}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DurableTaskSamplesUnitTests", "Samples\DurableTaskSamplesUnitTests\DurableTaskSamplesUnitTests.csproj", "{1F20B608-0206-4B58-8C0B-CB7430A880FD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{D401FF8B-D1B0-4CBC-AC8C-B6E56BDF7377}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BC204374-927C-4A48-8473-F5BF8C97EAEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BC204374-927C-4A48-8473-F5BF8C97EAEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC204374-927C-4A48-8473-F5BF8C97EAEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC204374-927C-4A48-8473-F5BF8C97EAEA}.Release|Any CPU.Build.0 = Release|Any CPU
{6F5D2EAD-726D-4FE5-A575-AEB96D1CCE37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F5D2EAD-726D-4FE5-A575-AEB96D1CCE37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F5D2EAD-726D-4FE5-A575-AEB96D1CCE37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F5D2EAD-726D-4FE5-A575-AEB96D1CCE37}.Release|Any CPU.Build.0 = Release|Any CPU
{1F20B608-0206-4B58-8C0B-CB7430A880FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F20B608-0206-4B58-8C0B-CB7430A880FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F20B608-0206-4B58-8C0B-CB7430A880FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F20B608-0206-4B58-8C0B-CB7430A880FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EnterpriseLibraryConfigurationToolBinariesPath = packages\TransientFaultHandling.Core.5.1.1209.1\lib\NET4
EndGlobalSection
EndGlobal
27 changes: 27 additions & 0 deletions Samples/DurableTaskSamples/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="StorageConnectionString" value="UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://127.0.0.1:10002/" />
<add key="ServiceBusConnectionString" value="" />
<add key="taskHubName" value="SamplesHub" />
</appSettings>
<!--system.diagnostics>
<trace autoflush="true"/>
<sources>
<source name="DurableTask" switchName="mySwitch"
switchType="System.Diagnostics.SourceSwitch">
<listeners>
<clear />
<add name="configConsoleListener" type="DurableTask.Tracing.OrchestrationConsoleTraceListener, DurableTask"
traceOutputOptions="DateTime" />
</listeners>
</source>
</sources>
<switches>
<add name="mySwitch" value="Verbose" />
</switches>
</system.diagnostics-->
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace DurableTaskSamples.AverageCalculator
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DurableTask;

public class AverageCalculatorOrchestration : TaskOrchestration<double, int[]>
{
public override async Task<double> RunTask(OrchestrationContext context, int[] input)
{
if (input == null || input.Length != 3)
{
throw new ArgumentException("input");
}

int start = input[0];
int end = input[1];
int step = input[2];
int total = end - start + 1;

var chunks = new List<Task<int>>();
int current;
while (start < end)
{
current = start + step - 1;
if (current > end)
{
current = end;
}

var chunk = context.ScheduleTask<int>(typeof(ComputeSumTask), new int[] {start, current});
chunks.Add(chunk);

start = current + 1;
}

int sum = 0;
int[] allChunks = await Task.WhenAll(chunks.ToArray());
foreach (int result in allChunks)
{
sum += result;
}

return sum/total;
}
}
}
34 changes: 34 additions & 0 deletions Samples/DurableTaskSamples/AverageCalculator/ComputeSumTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace DurableTaskSamples.AverageCalculator
{
using System;
using DurableTask;

public sealed class ComputeSumTask : TaskActivity<int[], int>
{
public ComputeSumTask()
{
}

protected override int Execute(DurableTask.TaskContext context, int[] chunk)
{
if (chunk == null || chunk.Length != 2)
{
throw new ArgumentException("chunk");
}

Console.WriteLine("Compute Sum for " + chunk[0] + "," + chunk[1]);
int sum = 0;
int start = chunk[0];
int end = chunk[1];
for (int i = start; i <= end; i++)
{
sum += i;
}

Console.WriteLine("Total Sum for Chunk '" + chunk[0] + "," + chunk[1] + "' is " + sum.ToString());

return sum;
}
}

}
50 changes: 50 additions & 0 deletions Samples/DurableTaskSamples/Common.WorkItems/EmailTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace DurableTaskSamples.Common.WorkItems
{
using System.Net;
using System.Net.Mail;
using DurableTask;

public sealed class EmailInput
{
public string To;
public string ToAddress;
public string Subject;
public string Body;
}

public sealed class EmailTask : TaskActivity<EmailInput, object>
{
private static MailAddress FromAddress = new MailAddress("[email protected]", "Service Bus Task Mailer");
private const string FromPassword = "Broken!12";

public EmailTask()
{
}

protected override object Execute(TaskContext context, EmailInput input)
{
var toAddress = new MailAddress(input.ToAddress, input.To);

var smtp = new SmtpClient
{
Host = "smtp.live.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(FromAddress.Address, FromPassword)
};

using (var message = new MailMessage(FromAddress, toAddress)
{
Subject = input.Subject,
Body = input.Body
})
{
smtp.Send(message);
}
return null;
}

}
}
43 changes: 43 additions & 0 deletions Samples/DurableTaskSamples/Cron/CronOrchestration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace DurableTaskSamples.Cron
{
using System;
using System.Threading.Tasks;
using DurableTask;
using NCrontab;

public class CronOrchestration : TaskOrchestration<string, string>
{
public override async Task<string> RunTask(OrchestrationContext context, string cronSchedule)
{
int numberOfTimes = 4;
bool waitForCompletion = false;
int runAfterEverySeconds = 10;

for (int i = 1; i <= numberOfTimes; i++)
{
DateTime currentTime = context.CurrentUtcDateTime;
DateTime fireAt;
if (string.IsNullOrWhiteSpace(cronSchedule))
{
fireAt = currentTime.AddSeconds(runAfterEverySeconds);
}
else
{
CrontabSchedule schedule = CrontabSchedule.Parse(cronSchedule);
fireAt = schedule.GetNextOccurrence(context.CurrentUtcDateTime);
}

var attempt = await context.CreateTimer<string>(fireAt, i.ToString());

Task<string> resultTask = context.ScheduleTask<string>(typeof(CronTask), attempt);

if (waitForCompletion)
{
await resultTask;
}
}

return "Done";
}
}
}
26 changes: 26 additions & 0 deletions Samples/DurableTaskSamples/Cron/CronTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace DurableTaskSamples.Cron
{
using System;
using System.Threading;
using DurableTask;

public sealed class CronTask : TaskActivity<string,string>
{
public CronTask()
{
}

protected override string Execute(DurableTask.TaskContext context, string input)
{
Console.WriteLine("Executing Cron Job. Started At: '" + DateTime.Now.ToString() + "' Number: " + input);

Thread.Sleep(2 * 1000);

string completed = "Cron Job '" + input + "' Completed...";
Console.WriteLine(completed);

return completed;
}
}

}
Loading

0 comments on commit 07b039a

Please sign in to comment.