Skip to content

Commit 21697c3

Browse files
authored
Merge pull request #4 from stevewgh/develop
Merge Develop into Master
2 parents 79ca6b4 + 1410d9b commit 21697c3

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

src/Topshelf.Leader.Tests/MultipleNodeTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ public async Task only_one_node_should_be_running_at_any_one_time()
2020
BuildSubject(node2, manager, out var service2, out var node2Runner);
2121

2222
// start the runners async
23+
#pragma warning disable 4014
2324
node1Runner.Start();
2425
node2Runner.Start();
26+
#pragma warning restore 4014
27+
2528
await Task.Delay(500);
2629

2730
Assert.True(service1.Started);
@@ -45,8 +48,11 @@ public async Task should_be_notified_when_the_leadership_changes()
4548
BuildSubject(node2, manager, b => { if (!node2Elected) node2Elected = b; }, out var _, out var node2Runner);
4649

4750
// start the runners async
51+
#pragma warning disable 4014
4852
node1Runner.Start();
4953
node2Runner.Start();
54+
#pragma warning restore 4014
55+
5056
await Task.Delay(500);
5157

5258
manager.AssignLeader(node2);

src/Topshelf.Leader/Runner.cs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,45 +30,49 @@ public async Task Start()
3030
continue;
3131
}
3232

33-
var noLongerTheLeader = new CancellationTokenSource();
34-
var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(config.ServiceIsStopping.Token, noLongerTheLeader.Token);
35-
try
33+
using (var noLongerTheLeader = new CancellationTokenSource())
3634
{
37-
var leaseTask = RenewLease(linkedTokenSource.Token, noLongerTheLeader);
38-
var startupTask = config.Startup(service, linkedTokenSource.Token);
39-
var whenAnyTask = await Task.WhenAny(leaseTask, startupTask);
40-
41-
var exceptions = new List<Exception>();
42-
if (startupTask.IsFaulted)
35+
using (var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(config.ServiceIsStopping.Token, noLongerTheLeader.Token))
4336
{
44-
config.ServiceIsStopping.Cancel();
45-
if (startupTask.Exception != null)
37+
try
4638
{
47-
exceptions.Add(startupTask.Exception.GetBaseException());
48-
}
49-
}
39+
var leaseTask = RenewLease(linkedTokenSource.Token, noLongerTheLeader);
40+
var startupTask = config.Startup(service, linkedTokenSource.Token);
41+
var whenAnyTask = await Task.WhenAny(leaseTask, startupTask);
5042

51-
if (leaseTask.IsFaulted)
52-
{
53-
if (leaseTask.Exception != null)
43+
var exceptions = new List<Exception>();
44+
if (startupTask.IsFaulted)
45+
{
46+
config.ServiceIsStopping.Cancel();
47+
if (startupTask.Exception != null)
48+
{
49+
exceptions.Add(startupTask.Exception.GetBaseException());
50+
}
51+
}
52+
53+
if (leaseTask.IsFaulted)
54+
{
55+
if (leaseTask.Exception != null)
56+
{
57+
exceptions.Add(leaseTask.Exception.GetBaseException());
58+
}
59+
}
60+
61+
if (exceptions.Any())
62+
{
63+
throw new AggregateException(exceptions);
64+
}
65+
66+
await whenAnyTask;
67+
}
68+
catch (TaskCanceledException)
5469
{
55-
exceptions.Add(leaseTask.Exception.GetBaseException());
70+
}
71+
finally
72+
{
73+
linkedTokenSource.Cancel();
5674
}
5775
}
58-
59-
if (exceptions.Any())
60-
{
61-
throw new AggregateException(exceptions);
62-
}
63-
64-
await whenAnyTask;
65-
}
66-
catch (TaskCanceledException)
67-
{
68-
}
69-
finally
70-
{
71-
linkedTokenSource.Cancel();
7276
}
7377
}
7478
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
2+
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
33
<metadata>
44
<id>Topshelf.Leader</id>
5-
<version>1.0.0.0</version>
5+
<version>$version$</version>
66
<title>Topshelf Leader</title>
77
<authors>Steven Ward</authors>
88
<owners>Steven Ward</owners>
@@ -14,10 +14,12 @@
1414
<copyright>Copyright 2018</copyright>
1515
<tags>Topshelf Leader Election</tags>
1616
<dependencies>
17-
<dependency id="Topshelf" version="4.0.0" />
17+
<group>
18+
<dependency id="Topshelf" version="4.0.3" />
19+
</group>
1820
</dependencies>
1921
</metadata>
2022
<files>
21-
<file src="Topshelf.Leader.dll" target="lib\net452\Topshelf.Leader.dll" />
23+
<file src="bin\$configuration$\Topshelf.Leader.dll" target="lib\net452\Topshelf.Leader.dll" />
2224
</files>
2325
</package>

0 commit comments

Comments
 (0)