@@ -49,6 +49,7 @@ protected override async Task OnStop()
4949 }
5050 catch ( OperationCanceledException ) when ( CancellationToken . IsCancellationRequested )
5151 {
52+ Logger . LogDebug ( "PostgreSQL consumer for path {Path} stopped" , Path ) ;
5253 }
5354 _task = null ;
5455 }
@@ -62,41 +63,7 @@ private async Task Run()
6263 {
6364 try
6465 {
65- var scope = _serviceProvider . CreateScope ( ) ;
66- try
67- {
68- var repository = scope . ServiceProvider . GetRequiredService < IPostgreSqlRepository > ( ) ;
69- var messages = await repository . LockAndSelect ( Path , _pathKind , _subscriptionName , _instanceId , _providerSettings . PollBatchSize , _providerSettings . LockDuration , CancellationToken ) . ConfigureAwait ( false ) ;
70-
71- if ( messages . Count == 0 )
72- {
73- if ( idle . Elapsed >= _providerSettings . PollDelay )
74- {
75- await Task . Delay ( _providerSettings . PollDelay , CancellationToken ) . ConfigureAwait ( false ) ;
76- }
77- continue ;
78- }
79-
80- idle . Restart ( ) ;
81-
82- foreach ( var message in messages )
83- {
84- var result = await _messageProcessor . ProcessMessage ( message , message . Headers , cancellationToken : CancellationToken ) . ConfigureAwait ( false ) ;
85- if ( result . Exception == null )
86- {
87- await repository . Complete ( [ message ] , CancellationToken ) . ConfigureAwait ( false ) ;
88- }
89- else
90- {
91- Logger . LogError ( result . Exception , "Error occurred while processing PostgreSQL message {MessageId} on {Path}" , message . Id , Path ) ;
92- await repository . Fail ( [ message ] , _providerSettings . MaxDeliveryAttempts , CancellationToken ) . ConfigureAwait ( false ) ;
93- }
94- }
95- }
96- finally
97- {
98- await scope . DisposeAsyncScope ( ) . ConfigureAwait ( false ) ;
99- }
66+ await PollOnce ( idle ) . ConfigureAwait ( false ) ;
10067 }
10168 catch ( OperationCanceledException ) when ( CancellationToken . IsCancellationRequested )
10269 {
@@ -116,4 +83,52 @@ private async Task Run()
11683 }
11784 }
11885 }
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 )
119+ {
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+ }
133+ }
119134}
0 commit comments