11namespace SlimMessageBus . Host . PostgreSql ;
22
3- using System . Diagnostics ;
4-
5- public class PostgreSqlConsumer : AbstractConsumer
3+ public class PostgreSqlConsumer : RelationalConsumerBase < PostgreSqlTransportMessage , IPostgreSqlRepository >
64{
7- private readonly IServiceProvider _serviceProvider ;
8- private readonly PostgreSqlMessageBusSettings _providerSettings ;
9- private readonly IMessageProcessor < PostgreSqlTransportMessage > _messageProcessor ;
10- private readonly PathKind _pathKind ;
11- private readonly string ? _subscriptionName ;
12- private readonly string _instanceId ;
13- private Task ? _task ;
14-
155 public PostgreSqlConsumer (
166 ILogger < PostgreSqlConsumer > logger ,
177 IEnumerable < AbstractConsumerSettings > consumerSettings ,
@@ -23,112 +13,7 @@ public PostgreSqlConsumer(
2313 PathKind pathKind ,
2414 string ? subscriptionName ,
2515 string instanceId )
26- : base ( logger , consumerSettings , path , interceptors )
27- {
28- _serviceProvider = serviceProvider ;
29- _providerSettings = providerSettings ;
30- _messageProcessor = messageProcessor ;
31- _pathKind = pathKind ;
32- _subscriptionName = subscriptionName ;
33- _instanceId = instanceId ;
34- }
35-
36- protected override Task OnStart ( )
37- {
38- _task = Run ( ) ;
39- return Task . CompletedTask ;
40- }
41-
42- protected override async Task OnStop ( )
43- {
44- if ( _task != null )
45- {
46- try
47- {
48- await _task . ConfigureAwait ( false ) ;
49- }
50- catch ( OperationCanceledException ) when ( CancellationToken . IsCancellationRequested )
51- {
52- Logger . LogDebug ( "PostgreSQL consumer for path {Path} stopped" , Path ) ;
53- }
54- _task = null ;
55- }
56- }
57-
58- private async Task Run ( )
59- {
60- var idle = Stopwatch . StartNew ( ) ;
61-
62- while ( ! CancellationToken . IsCancellationRequested )
63- {
64- try
65- {
66- await PollOnce ( idle ) . ConfigureAwait ( false ) ;
67- }
68- catch ( OperationCanceledException ) when ( CancellationToken . IsCancellationRequested )
69- {
70- return ;
71- }
72- catch ( Exception e ) when ( ! CancellationToken . IsCancellationRequested )
73- {
74- Logger . LogWarning ( e , "Error occurred while polling PostgreSQL path {Path}" , Path ) ;
75- try
76- {
77- await Task . Delay ( _providerSettings . PollDelay , CancellationToken ) . ConfigureAwait ( false ) ;
78- }
79- catch ( OperationCanceledException ) when ( CancellationToken . IsCancellationRequested )
80- {
81- return ;
82- }
83- }
84- }
85- }
86-
87- private async Task PollOnce ( Stopwatch idle )
88- {
89- var scope = _serviceProvider . CreateScope ( ) ;
90- try
91- {
92- var repository = scope . ServiceProvider . GetRequiredService < IPostgreSqlRepository > ( ) ;
93- var messages = await repository . LockAndSelect ( Path , _pathKind , _subscriptionName , _instanceId , _providerSettings . PollBatchSize , _providerSettings . LockDuration , CancellationToken ) . ConfigureAwait ( false ) ;
94-
95- if ( messages . Count == 0 )
96- {
97- await DelayWhenIdle ( idle ) . ConfigureAwait ( false ) ;
98- return ;
99- }
100-
101- idle . Restart ( ) ;
102- await ProcessMessages ( repository , messages ) . ConfigureAwait ( false ) ;
103- }
104- finally
105- {
106- await scope . DisposeAsyncScope ( ) . ConfigureAwait ( false ) ;
107- }
108- }
109-
110- private async Task DelayWhenIdle ( Stopwatch idle )
111- {
112- if ( idle . Elapsed >= _providerSettings . PollDelay )
113- {
114- await Task . Delay ( _providerSettings . PollDelay , CancellationToken ) . ConfigureAwait ( false ) ;
115- }
116- }
117-
118- private async Task ProcessMessages ( IPostgreSqlRepository repository , IReadOnlyCollection < PostgreSqlTransportMessage > messages )
16+ : base ( logger , consumerSettings , interceptors , serviceProvider , providerSettings , messageProcessor , path , pathKind , subscriptionName , instanceId , "PostgreSQL" )
11917 {
120- foreach ( var message in messages )
121- {
122- var result = await _messageProcessor . ProcessMessage ( message , message . Headers , cancellationToken : CancellationToken ) . ConfigureAwait ( false ) ;
123- if ( result . Exception == null )
124- {
125- await repository . Complete ( [ message ] , CancellationToken ) . ConfigureAwait ( false ) ;
126- }
127- else
128- {
129- Logger . LogError ( result . Exception , "Error occurred while processing PostgreSQL message {MessageId} on {Path}" , message . Id , Path ) ;
130- await repository . Fail ( [ message ] , _providerSettings . MaxDeliveryAttempts , CancellationToken ) . ConfigureAwait ( false ) ;
131- }
132- }
13318 }
13419}
0 commit comments