11using System . Collections . Concurrent ;
22using System . Text . Json . Serialization ;
3- using Blumchen . Configuration ;
43using Blumchen . Serialization ;
54using Blumchen . Subscriptions ;
65using Blumchen . Subscriptions . Management ;
76using Microsoft . Extensions . Hosting ;
87using Microsoft . Extensions . Logging ;
8+ using Npgsql ;
99using Polly ;
1010
1111
1212namespace Blumchen . Workers ;
1313
1414public abstract class Worker < T > (
15- DatabaseOptions databaseOptions ,
15+ NpgsqlDataSource dataSource ,
16+ string connectionString ,
1617 IHandler < T > handler ,
1718 JsonSerializerContext jsonSerializerContext ,
1819 IErrorProcessor errorProcessor ,
@@ -21,9 +22,8 @@ public abstract class Worker<T>(
2122 PublicationManagement . PublicationSetupOptions publicationSetupOptions ,
2223 ReplicationSlotManagement . ReplicationSlotSetupOptions replicationSlotSetupOptions ,
2324 Func < TableDescriptorBuilder , TableDescriptorBuilder > tableDescriptorBuilder ,
24- ILoggerFactory loggerFactory ) : BackgroundService where T : class
25+ ILogger logger ) : BackgroundService where T : class
2526{
26- private readonly ILogger < Worker < T > > _logger = loggerFactory . CreateLogger < Worker < T > > ( ) ;
2727 private string WorkerName { get ; } = $ "{ nameof ( Worker < T > ) } <{ typeof ( T ) . Name } >";
2828 private static readonly ConcurrentDictionary < string , Action < ILogger , string , object [ ] > > LoggingActions = new ( StringComparer . OrdinalIgnoreCase ) ;
2929 private static void Notify ( ILogger logger , LogLevel level , string template , params object [ ] parameters )
@@ -33,9 +33,9 @@ static Action<ILogger, string, object[]> LoggerAction(LogLevel ll, bool enabled)
3333 {
3434 ( LogLevel . Information , true ) => ( logger , template , parameters ) => logger . LogInformation ( template , parameters ) ,
3535 ( LogLevel . Debug , true ) => ( logger , template , parameters ) => logger . LogDebug ( template , parameters ) ,
36- ( _, _) => ( _ , __ , ___ ) => { }
36+ ( _, _) => ( _ , _ , _ ) => { }
3737 } ;
38- LoggingActions . GetOrAdd ( template , s => LoggerAction ( level , logger . IsEnabled ( level ) ) ) ( logger , template , parameters ) ;
38+ LoggingActions . GetOrAdd ( template , _ => LoggerAction ( level , logger . IsEnabled ( level ) ) ) ( logger , template , parameters ) ;
3939 }
4040
4141 protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
@@ -45,21 +45,22 @@ await pipeline.ExecuteAsync(async token =>
4545 await using var subscription = new Subscription ( ) ;
4646 await using var cursor = subscription . Subscribe ( builder =>
4747 builder
48- . ConnectionString ( databaseOptions . ConnectionString )
48+ . DataSource ( dataSource )
49+ . ConnectionString ( connectionString )
4950 . WithTable ( tableDescriptorBuilder )
5051 . WithErrorProcessor ( errorProcessor )
5152 . Handles < T , IHandler < T > > ( handler )
5253 . NamingPolicy ( namingPolicy )
5354 . JsonContext ( jsonSerializerContext )
5455 . WithPublicationOptions ( publicationSetupOptions )
5556 . WithReplicationOptions ( replicationSlotSetupOptions )
56- , ct : token , loggerFactory : loggerFactory ) . GetAsyncEnumerator ( token ) ;
57- Notify ( _logger , LogLevel . Information , "{WorkerName} started" , WorkerName ) ;
57+ , ct : token ) . GetAsyncEnumerator ( token ) ;
58+ Notify ( logger , LogLevel . Information , "{WorkerName} started" , WorkerName ) ;
5859 while ( await cursor . MoveNextAsync ( ) . ConfigureAwait ( false ) && ! token . IsCancellationRequested )
59- Notify ( _logger , LogLevel . Debug , "{cursor.Current} processed" , cursor . Current ) ;
60+ Notify ( logger , LogLevel . Debug , "{cursor.Current} processed" , cursor . Current ) ;
6061
6162 } , stoppingToken ) . ConfigureAwait ( false ) ;
62- Notify ( _logger , LogLevel . Information , "{WorkerName} stopped" , WorkerName ) ;
63+ Notify ( logger , LogLevel . Information , "{WorkerName} stopped" , WorkerName ) ;
6364 return ;
6465 }
6566
0 commit comments