Skip to content

Commit 1fe4c97

Browse files
committed
chore: Rename MultiProvider sample to MultiLock.MultiProvider
Refactor the multi-provider demo sample to align with the MultiLock naming convention: - Rename project from LeaderElection.MultiProvider to MultiLock.MultiProvider - Extract DemoBackgroundService into separate file - Update project references to use MultiLock namespace (MultiLock.csproj, MultiLock.InMemory, MultiLock.FileSystem) - Update using statements to match new namespace structure - Update console output and temp path to use MultiLock branding - Add project to solution file - Apply modern C# syntax improvements (var to explicit types, simplified method calls)
1 parent 0aa0e72 commit 1fe4c97

File tree

4 files changed

+82
-84
lines changed

4 files changed

+82
-84
lines changed

MultiLock.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<Project Path="src\Providers\MultiLock.ZooKeeper\MultiLock.ZooKeeper.csproj" Type="Classic C#" />
1111
</Folder>
1212
<Folder Name="/Samples/">
13+
<Project Path="samples\MultiLock.MultiProvider\MultiLock.MultiProvider.csproj" Type="Classic C#" />
1314
<Project Path="samples\MultiLock.Sample\MultiLock.Sample.csproj" Type="Classic C#" />
1415
</Folder>
1516
<Folder Name="/Tests/">
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Microsoft.Extensions.Hosting;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace MultiLock.MultiProvider;
5+
6+
/// <summary>
7+
/// Demo background service that shows leadership status using the AsyncEnumerable API.
8+
/// </summary>
9+
public class DemoBackgroundService(
10+
ILeaderElectionService leaderElection,
11+
ILogger<DemoBackgroundService> logger)
12+
: BackgroundService
13+
{
14+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
15+
{
16+
// Start listening to leadership changes using AsyncEnumerable
17+
var leadershipTask = Task.Run(async () =>
18+
{
19+
await foreach (LeadershipChangedEventArgs change in leaderElection.GetLeadershipChangesAsync(stoppingToken))
20+
{
21+
if (change.BecameLeader)
22+
{
23+
logger.LogInformation("🎉 [{ParticipantId}] Leadership acquired!",
24+
leaderElection.ParticipantId);
25+
}
26+
else if (change.LostLeadership)
27+
{
28+
logger.LogWarning("😞 [{ParticipantId}] Leadership lost!",
29+
leaderElection.ParticipantId);
30+
}
31+
}
32+
}, stoppingToken);
33+
34+
int workCounter = 0;
35+
36+
while (!stoppingToken.IsCancellationRequested)
37+
{
38+
if (leaderElection.IsLeader)
39+
{
40+
// Perform leader-only work
41+
workCounter++;
42+
logger.LogInformation("🏆 [{ParticipantId}] Leader performing work #{WorkCounter}",
43+
leaderElection.ParticipantId, workCounter);
44+
45+
await Task.Delay(TimeSpan.FromSeconds(3), stoppingToken);
46+
}
47+
else
48+
{
49+
// Follower behavior
50+
logger.LogInformation("👥 [{ParticipantId}] Follower waiting...",
51+
leaderElection.ParticipantId);
52+
53+
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
54+
}
55+
}
56+
57+
// Wait for leadership monitoring to complete
58+
try
59+
{
60+
await leadershipTask;
61+
}
62+
catch (OperationCanceledException)
63+
{
64+
// Expected when stopping
65+
}
66+
}
67+
}

samples/LeaderElection.MultiProvider/LeaderElection.MultiProvider.csproj renamed to samples/MultiLock.MultiProvider/MultiLock.MultiProvider.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<ProjectReference Include="..\..\src\MultiLock.Net\MultiLock.Net.csproj" />
17-
<ProjectReference Include="..\..\src\Providers\MultiLock.Net.InMemory\MultiLock.Net.InMemory.csproj" />
18-
<ProjectReference Include="..\..\src\Providers\MultiLock.Net.FileSystem\MultiLock.Net.FileSystem.csproj" />
16+
<ProjectReference Include="..\..\src\MultiLock\MultiLock.csproj" />
17+
<ProjectReference Include="..\..\src\Providers\MultiLock.InMemory\MultiLock.InMemory.csproj" />
18+
<ProjectReference Include="..\..\src\Providers\MultiLock.FileSystem\MultiLock.FileSystem.csproj" />
1919
</ItemGroup>
2020

2121
</Project>
Lines changed: 11 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.Extensions.Logging;
4-
using MultiLock.Net;
5-
using MultiLock.Net.InMemory;
6-
using MultiLock.Net.FileSystem;
4+
using MultiLock.FileSystem;
5+
using MultiLock.InMemory;
6+
using MultiLock.MultiProvider;
77

8-
Console.WriteLine("=== LeaderElection.Net Multi-Provider Demo ===");
8+
Console.WriteLine("=== MultiLock Multi-Provider Demo ===");
99
Console.WriteLine();
1010
Console.WriteLine("This demo shows multiple instances competing for leadership");
1111
Console.WriteLine("using different providers simultaneously.");
@@ -35,7 +35,7 @@
3535
var allTasks = Task.WhenAll(tasks);
3636

3737
// Wait for user input
38-
await Task.Run(() => Console.ReadKey());
38+
await Task.Run(Console.ReadKey);
3939

4040
Console.WriteLine();
4141
Console.WriteLine("Stopping all instances...");
@@ -53,11 +53,12 @@
5353
}
5454

5555
Console.WriteLine("All instances stopped.");
56+
return;
5657

5758
static IHost CreateInMemoryHost()
5859
{
59-
var builder = Host.CreateApplicationBuilder();
60-
60+
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
61+
6162
builder.Logging.ClearProviders();
6263
builder.Logging.AddConsole();
6364
builder.Logging.SetMinimumLevel(LogLevel.Information);
@@ -78,14 +79,14 @@ static IHost CreateInMemoryHost()
7879

7980
static IHost CreateFileSystemHost()
8081
{
81-
var builder = Host.CreateApplicationBuilder();
82-
82+
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
83+
8384
builder.Logging.ClearProviders();
8485
builder.Logging.AddConsole();
8586
builder.Logging.SetMinimumLevel(LogLevel.Information);
8687

8788
builder.Services.AddFileSystemLeaderElection(
88-
Path.Combine(Path.GetTempPath(), "LeaderElectionMultiProviderDemo"),
89+
Path.Combine(Path.GetTempPath(), "MultiLock-MultiProviderDemo"),
8990
options =>
9091
{
9192
options.ElectionGroup = "multi-provider-demo-fs";
@@ -121,74 +122,3 @@ static async Task RunInstanceAsync(string instanceName, IHost host, Cancellation
121122
host.Dispose();
122123
}
123124
}
124-
125-
/// <summary>
126-
/// Demo background service that shows leadership status using the AsyncEnumerable API.
127-
/// </summary>
128-
public class DemoBackgroundService : BackgroundService
129-
{
130-
private readonly ILeaderElectionService _leaderElection;
131-
private readonly ILogger<DemoBackgroundService> _logger;
132-
133-
public DemoBackgroundService(
134-
ILeaderElectionService leaderElection,
135-
ILogger<DemoBackgroundService> logger)
136-
{
137-
_leaderElection = leaderElection;
138-
_logger = logger;
139-
}
140-
141-
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
142-
{
143-
// Start listening to leadership changes using AsyncEnumerable
144-
var leadershipTask = Task.Run(async () =>
145-
{
146-
await foreach (var change in _leaderElection.GetLeadershipChangesAsync(stoppingToken))
147-
{
148-
if (change.BecameLeader)
149-
{
150-
_logger.LogInformation("🎉 [{ParticipantId}] Leadership acquired!",
151-
_leaderElection.ParticipantId);
152-
}
153-
else if (change.LostLeadership)
154-
{
155-
_logger.LogWarning("😞 [{ParticipantId}] Leadership lost!",
156-
_leaderElection.ParticipantId);
157-
}
158-
}
159-
}, stoppingToken);
160-
161-
var workCounter = 0;
162-
163-
while (!stoppingToken.IsCancellationRequested)
164-
{
165-
if (_leaderElection.IsLeader)
166-
{
167-
// Perform leader-only work
168-
workCounter++;
169-
_logger.LogInformation("🏆 [{ParticipantId}] Leader performing work #{WorkCounter}",
170-
_leaderElection.ParticipantId, workCounter);
171-
172-
await Task.Delay(TimeSpan.FromSeconds(3), stoppingToken);
173-
}
174-
else
175-
{
176-
// Follower behavior
177-
_logger.LogInformation("👥 [{ParticipantId}] Follower waiting...",
178-
_leaderElection.ParticipantId);
179-
180-
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
181-
}
182-
}
183-
184-
// Wait for leadership monitoring to complete
185-
try
186-
{
187-
await leadershipTask;
188-
}
189-
catch (OperationCanceledException)
190-
{
191-
// Expected when stopping
192-
}
193-
}
194-
}

0 commit comments

Comments
 (0)