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
16
IHandler < T > handler ,
17
17
JsonSerializerContext jsonSerializerContext ,
18
18
IErrorProcessor errorProcessor ,
@@ -21,9 +21,8 @@ public abstract class Worker<T>(
21
21
PublicationManagement . PublicationSetupOptions publicationSetupOptions ,
22
22
ReplicationSlotManagement . ReplicationSlotSetupOptions replicationSlotSetupOptions ,
23
23
Func < TableDescriptorBuilder , TableDescriptorBuilder > tableDescriptorBuilder ,
24
- ILoggerFactory loggerFactory ) : BackgroundService where T : class
24
+ ILogger logger ) : BackgroundService where T : class
25
25
{
26
- private readonly ILogger < Worker < T > > _logger = loggerFactory . CreateLogger < Worker < T > > ( ) ;
27
26
private string WorkerName { get ; } = $ "{ nameof ( Worker < T > ) } <{ typeof ( T ) . Name } >";
28
27
private static readonly ConcurrentDictionary < string , Action < ILogger , string , object [ ] > > LoggingActions = new ( StringComparer . OrdinalIgnoreCase ) ;
29
28
private static void Notify ( ILogger logger , LogLevel level , string template , params object [ ] parameters )
@@ -33,9 +32,9 @@ static Action<ILogger, string, object[]> LoggerAction(LogLevel ll, bool enabled)
33
32
{
34
33
( LogLevel . Information , true ) => ( logger , template , parameters ) => logger . LogInformation ( template , parameters ) ,
35
34
( LogLevel . Debug , true ) => ( logger , template , parameters ) => logger . LogDebug ( template , parameters ) ,
36
- ( _, _) => ( _ , __ , ___ ) => { }
35
+ ( _, _) => ( _ , _ , _ ) => { }
37
36
} ;
38
- LoggingActions . GetOrAdd ( template , s => LoggerAction ( level , logger . IsEnabled ( level ) ) ) ( logger , template , parameters ) ;
37
+ LoggingActions . GetOrAdd ( template , _ => LoggerAction ( level , logger . IsEnabled ( level ) ) ) ( logger , template , parameters ) ;
39
38
}
40
39
41
40
protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
@@ -45,21 +44,21 @@ await pipeline.ExecuteAsync(async token =>
45
44
await using var subscription = new Subscription ( ) ;
46
45
await using var cursor = subscription . Subscribe ( builder =>
47
46
builder
48
- . ConnectionString ( databaseOptions . ConnectionString )
47
+ . DataSource ( dataSource )
49
48
. WithTable ( tableDescriptorBuilder )
50
49
. WithErrorProcessor ( errorProcessor )
51
50
. Handles < T , IHandler < T > > ( handler )
52
51
. NamingPolicy ( namingPolicy )
53
52
. JsonContext ( jsonSerializerContext )
54
53
. WithPublicationOptions ( publicationSetupOptions )
55
54
. WithReplicationOptions ( replicationSlotSetupOptions )
56
- , ct : token , loggerFactory : loggerFactory ) . GetAsyncEnumerator ( token ) ;
57
- Notify ( _logger , LogLevel . Information , "{WorkerName} started" , WorkerName ) ;
55
+ , ct : token ) . GetAsyncEnumerator ( token ) ;
56
+ Notify ( logger , LogLevel . Information , "{WorkerName} started" , WorkerName ) ;
58
57
while ( await cursor . MoveNextAsync ( ) . ConfigureAwait ( false ) && ! token . IsCancellationRequested )
59
- Notify ( _logger , LogLevel . Debug , "{cursor.Current} processed" , cursor . Current ) ;
58
+ Notify ( logger , LogLevel . Debug , "{cursor.Current} processed" , cursor . Current ) ;
60
59
61
60
} , stoppingToken ) . ConfigureAwait ( false ) ;
62
- Notify ( _logger , LogLevel . Information , "{WorkerName} stopped" , WorkerName ) ;
61
+ Notify ( logger , LogLevel . Information , "{WorkerName} stopped" , WorkerName ) ;
63
62
return ;
64
63
}
65
64
0 commit comments