Skip to content

Commit ff55c50

Browse files
committed
Merge pull request #35 from graemefoster/expose-task-instead-of-always-blocking
Expose task instead of always blocking
2 parents 70367c2 + b62a6fb commit ff55c50

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

AzureWebFarm.OctopusDeploy/WebFarmRole.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel.Design;
33
using System.Net.Configuration;
44
using System.Threading;
5+
using System.Threading.Tasks;
56
using AzureWebFarm.OctopusDeploy.Infrastructure;
67
using Microsoft.WindowsAzure.ServiceRuntime;
78
using Serilog;
@@ -58,26 +59,37 @@ public bool OnStart()
5859
/// Call from the RoleEntryPoint.Run() method.
5960
/// Note: This method is an infinite loop; call from a Thread/Task if you want to run other code alongside.
6061
/// </summary>
61-
public void Run()
62+
public async Task Run(CancellationToken token)
6263
{
6364
// Don't want to configure IIS if we are emulating; just sleep forever
64-
if (RoleEnvironment.IsEmulated)
65-
Thread.Sleep(-1);
66-
67-
while (true)
65+
try
6866
{
69-
try
67+
if (RoleEnvironment.IsEmulated)
7068
{
71-
IisEnvironment.ActivateAppInitialisationModuleForAllSites();
69+
while (!token.IsCancellationRequested)
70+
{
71+
await Task.Delay(100, token);
72+
}
7273
}
73-
catch (Exception e)
74+
75+
while (!token.IsCancellationRequested)
7476
{
75-
Log.Warning(e, "Failure to configure IIS");
76-
}
77+
try
78+
{
79+
IisEnvironment.ActivateAppInitialisationModuleForAllSites();
80+
}
81+
catch (Exception e)
82+
{
83+
Log.Warning(e, "Failure to configure IIS");
84+
}
7785

78-
Thread.Sleep(TimeSpan.FromMinutes(10));
86+
await Task.Delay(TimeSpan.FromMinutes(10), token);
87+
}
88+
}
89+
catch (TaskCanceledException)
90+
{
7991
}
80-
// ReSharper disable FunctionNeverReturns
92+
// ReSharper disable FunctionNeverReturns
8193
}
8294
// ReSharper restore FunctionNeverReturns
8395

ExampleWebFarm/WebRole.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Threading;
12
using Microsoft.WindowsAzure.ServiceRuntime;
23

34
namespace WebFarm
@@ -18,7 +19,7 @@ public override bool OnStart()
1819

1920
public override void Run()
2021
{
21-
_webFarmRole.Run();
22+
_webFarmRole.Run(new CancellationTokenSource().Token).Wait();
2223
}
2324

2425
public override void OnStop()

0 commit comments

Comments
 (0)