1
1
using System . Collections . Concurrent ;
2
2
using System . Text . Json . Serialization ;
3
- using Blumchen . Configuration ;
4
3
using Blumchen . Serialization ;
5
4
using Blumchen . Subscriptions ;
6
5
using Blumchen . Subscriptions . Management ;
7
6
using Microsoft . Extensions . Hosting ;
8
7
using Microsoft . Extensions . Logging ;
8
+ using Npgsql ;
9
9
using Polly ;
10
10
11
11
12
12
namespace Blumchen . Workers ;
13
13
14
14
public abstract class Worker < T > (
15
- DatabaseOptions databaseOptions ,
15
+ NpgsqlDataSource dataSource ,
16
+ string connectionString ,
16
17
IHandler < T > handler ,
17
18
JsonSerializerContext jsonSerializerContext ,
18
19
IErrorProcessor errorProcessor ,
@@ -21,9 +22,8 @@ public abstract class Worker<T>(
21
22
PublicationManagement . PublicationSetupOptions publicationSetupOptions ,
22
23
ReplicationSlotManagement . ReplicationSlotSetupOptions replicationSlotSetupOptions ,
23
24
Func < TableDescriptorBuilder , TableDescriptorBuilder > tableDescriptorBuilder ,
24
- ILoggerFactory loggerFactory ) : BackgroundService where T : class
25
+ ILogger logger ) : BackgroundService where T : class
25
26
{
26
- private readonly ILogger < Worker < T > > _logger = loggerFactory . CreateLogger < Worker < T > > ( ) ;
27
27
private string WorkerName { get ; } = $ "{ nameof ( Worker < T > ) } <{ typeof ( T ) . Name } >";
28
28
private static readonly ConcurrentDictionary < string , Action < ILogger , string , object [ ] > > LoggingActions = new ( StringComparer . OrdinalIgnoreCase ) ;
29
29
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)
33
33
{
34
34
( LogLevel . Information , true ) => ( logger , template , parameters ) => logger . LogInformation ( template , parameters ) ,
35
35
( LogLevel . Debug , true ) => ( logger , template , parameters ) => logger . LogDebug ( template , parameters ) ,
36
- ( _, _) => ( _ , __ , ___ ) => { }
36
+ ( _, _) => ( _ , _ , _ ) => { }
37
37
} ;
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 ) ;
39
39
}
40
40
41
41
protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
@@ -45,21 +45,22 @@ await pipeline.ExecuteAsync(async token =>
45
45
await using var subscription = new Subscription ( ) ;
46
46
await using var cursor = subscription . Subscribe ( builder =>
47
47
builder
48
- . ConnectionString ( databaseOptions . ConnectionString )
48
+ . DataSource ( dataSource )
49
+ . ConnectionString ( connectionString )
49
50
. WithTable ( tableDescriptorBuilder )
50
51
. WithErrorProcessor ( errorProcessor )
51
52
. Handles < T , IHandler < T > > ( handler )
52
53
. NamingPolicy ( namingPolicy )
53
54
. JsonContext ( jsonSerializerContext )
54
55
. WithPublicationOptions ( publicationSetupOptions )
55
56
. 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 ) ;
58
59
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 ) ;
60
61
61
62
} , stoppingToken ) . ConfigureAwait ( false ) ;
62
- Notify ( _logger , LogLevel . Information , "{WorkerName} stopped" , WorkerName ) ;
63
+ Notify ( logger , LogLevel . Information , "{WorkerName} stopped" , WorkerName ) ;
63
64
return ;
64
65
}
65
66
0 commit comments