11using Microsoft . Extensions . DependencyInjection ;
22using Microsoft . Extensions . Hosting ;
33using 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 ===" ) ;
99Console . WriteLine ( ) ;
1010Console . WriteLine ( "This demo shows multiple instances competing for leadership" ) ;
1111Console . WriteLine ( "using different providers simultaneously." ) ;
3535var allTasks = Task . WhenAll ( tasks ) ;
3636
3737// Wait for user input
38- await Task . Run ( ( ) => Console . ReadKey ( ) ) ;
38+ await Task . Run ( Console . ReadKey ) ;
3939
4040Console . WriteLine ( ) ;
4141Console . WriteLine ( "Stopping all instances..." ) ;
5353}
5454
5555Console . WriteLine ( "All instances stopped." ) ;
56+ return ;
5657
5758static 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
7980static 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