11using Hangfire ;
2+ using Hangfire . Console ;
3+ using Hangfire . Server ;
24using Microsoft . EntityFrameworkCore ;
35using Microsoft . Extensions . Caching . Memory ;
46using Microsoft . Extensions . Configuration ;
57using Microsoft . Extensions . DependencyInjection ;
68using Microsoft . Extensions . Logging ;
7- using OneGround . ZGW . Common . Messaging ;
9+ using OneGround . ZGW . Common . Batching ;
10+ using OneGround . ZGW . Common . CorrelationId ;
811using OneGround . ZGW . Notificaties . DataModel ;
912using OneGround . ZGW . Notificaties . Messaging . Configuration ;
1013using OneGround . ZGW . Notificaties . Messaging . Consumers ;
@@ -13,7 +16,9 @@ namespace OneGround.ZGW.Notificaties.Messaging.Jobs.Notificatie;
1316
1417public interface INotificatieJob
1518{
16- Task ReQueueNotificatieAsync ( Guid abonnementId , SubscriberNotificatie notificatie ) ;
19+ [ Obsolete ( "Use overload with PerformContext (which adds functionality like logging" ) ] // Note: Keep the old one for backward compatibility
20+ Task ReQueueNotificatieAsync ( Guid abonnementId , SubscriberNotificatie notificatie , Guid ? batchId = null ) ;
21+ Task ReQueueNotificatieAsync ( Guid abonnementId , SubscriberNotificatie notificatie , PerformContext context = null , Guid ? batchId = null ) ;
1722}
1823
1924[ Queue ( Constants . NrcListenerQueue ) ]
@@ -22,6 +27,8 @@ public class NotificatieJob : INotificatieJob
2227 private readonly INotificationSender _notificationSender ;
2328 private readonly IServiceProvider _serviceProvider ;
2429 private readonly IMemoryCache _memoryCache ;
30+ private readonly IBatchIdAccessor _batchIdAccessor ;
31+ private readonly ICorrelationContextAccessor _correlationIdAccessor ;
2532 private readonly ILogger < NotificatieJob > _logger ;
2633 private readonly ApplicationConfiguration _applicationConfiguration ;
2734
@@ -30,30 +37,43 @@ public NotificatieJob(
3037 IServiceProvider serviceProvider ,
3138 IConfiguration configuration ,
3239 IMemoryCache memoryCache ,
40+ IBatchIdAccessor batchIdAccessor ,
41+ ICorrelationContextAccessor correlationIdAccessor ,
3342 ILogger < NotificatieJob > logger
3443 )
3544 {
3645 _notificationSender = notificationSender ;
3746 _serviceProvider = serviceProvider ;
3847 _memoryCache = memoryCache ;
48+ _batchIdAccessor = batchIdAccessor ;
49+ _correlationIdAccessor = correlationIdAccessor ;
3950 _logger = logger ;
4051
4152 _applicationConfiguration = configuration . GetSection ( "Application" ) . Get < ApplicationConfiguration > ( ) ?? new ApplicationConfiguration ( ) ;
4253 }
4354
44- public async Task ReQueueNotificatieAsync ( Guid abonnementId , SubscriberNotificatie notificatie )
55+ public Task ReQueueNotificatieAsync ( Guid abonnementId , SubscriberNotificatie notificatie , Guid ? batchId = null )
56+ {
57+ return ReQueueNotificatieAsync ( abonnementId , notificatie , null , batchId ) ;
58+ }
59+
60+ public async Task ReQueueNotificatieAsync (
61+ Guid abonnementId ,
62+ SubscriberNotificatie notificatie ,
63+ PerformContext context = null ,
64+ Guid ? batchId = null
65+ )
4566 {
4667 ArgumentNullException . ThrowIfNull ( notificatie , nameof ( notificatie ) ) ;
4768
48- using ( GetLoggingScope ( notificatie . Rsin , notificatie . CorrelationId ) )
69+ using ( GetLoggingScope ( notificatie . Rsin , notificatie . CorrelationId , batchId ) )
4970 {
5071 // Get deliver data for this subscriber like callback url and auth
5172 var subscriber = await GetCachedAbonnementByIdAsync ( abonnementId , CancellationToken . None ) ;
5273 if ( subscriber == null )
5374 {
54- throw new GeneralException (
55- $ "{ DateTime . Now : yyyy-MM-dd HH:mm:ss} { notificatie . CorrelationId } : Could not find abonnement with id '{ abonnementId } '"
56- ) ;
75+ _logger . LogWarning ( "Abonnement with id {abonnementId} not found. Probably deleted within the retry period of the job." , abonnementId ) ;
76+ return ; // Job Ignored->Succeeded
5777 }
5878 if ( string . IsNullOrWhiteSpace ( subscriber . CallbackUrl ) )
5979 {
@@ -62,20 +82,62 @@ public async Task ReQueueNotificatieAsync(Guid abonnementId, SubscriberNotificat
6282 ) ;
6383 }
6484
85+ SetBatchIdOrDefault ( batchId ) ;
86+ SetCorrelationId ( notificatie ) ;
87+
6588 // Notify subscriber on channel....
89+ context . WriteLineColored (
90+ ConsoleTextColor . Yellow ,
91+ $ "Try to deliver notification to subscriber '{ subscriber . CallbackUrl } ' on channel '{ notificatie . Kanaal } '."
92+ ) ;
93+
6694 var result = await _notificationSender . SendAsync ( notificatie , subscriber . CallbackUrl , subscriber . Auth ) ;
6795 if ( ! result . Success )
6896 {
97+ context . WriteLineColored (
98+ ConsoleTextColor . Red ,
99+ $ "Could not deliver notification to subscriber '{ subscriber . CallbackUrl } ' on channel '{ notificatie . Kanaal } '."
100+ ) ;
101+
69102 throw new NotDeliveredException (
70103 $ "{ DateTime . Now : yyyy-MM-dd HH:mm:ss} { notificatie . CorrelationId } : Could not deliver notificatie to subscriber '{ notificatie . Rsin } ', channel '{ notificatie . Kanaal } ', endpoint '{ subscriber . CallbackUrl } '"
71104 ) ;
72105 }
106+
107+ context . WriteLineColored (
108+ ConsoleTextColor . Yellow ,
109+ $ "Successfully delivered notification to subscriber '{ subscriber . CallbackUrl } ' on channel '{ notificatie . Kanaal } '."
110+ ) ;
73111 }
74112 }
75113
76- private IDisposable GetLoggingScope ( string rsin , Guid correlationId )
114+ private void SetCorrelationId ( SubscriberNotificatie notificatie )
115+ {
116+ _correlationIdAccessor . SetCorrelationId ( notificatie . CorrelationId . ToString ( ) ) ;
117+ }
118+
119+ private void SetBatchIdOrDefault ( Guid ? batchId )
77120 {
78- return _logger . BeginScope ( new Dictionary < string , object > { [ "CorrelationId" ] = correlationId , [ "RSIN" ] = rsin } ) ;
121+ _batchIdAccessor . Id = batchId . HasValue ? batchId . ToString ( ) : null ;
122+ }
123+
124+ private IDisposable GetLoggingScope ( string rsin , Guid correlationId , Guid ? batchId )
125+ {
126+ if ( batchId . HasValue )
127+ {
128+ return _logger . BeginScope (
129+ new Dictionary < string , object >
130+ {
131+ [ "CorrelationId" ] = correlationId ,
132+ [ "RSIN" ] = rsin ,
133+ [ "BatchId" ] = batchId ,
134+ }
135+ ) ;
136+ }
137+ else
138+ {
139+ return _logger . BeginScope ( new Dictionary < string , object > { [ "CorrelationId" ] = correlationId , [ "RSIN" ] = rsin } ) ;
140+ }
79141 }
80142
81143 private async Task < Abonnement > GetCachedAbonnementByIdAsync ( Guid id , CancellationToken cancellationToken )
@@ -99,21 +161,3 @@ private async Task<Abonnement> GetCachedAbonnementByIdAsync(Guid id, Cancellatio
99161 return abonnementen [ id ] ;
100162 }
101163}
102-
103- public static class NotificatieExtensions
104- {
105- public static SubscriberNotificatie ToInstance ( this INotificatie notificatie )
106- {
107- return new SubscriberNotificatie
108- {
109- Kanaal = notificatie . Kanaal ,
110- HoofdObject = notificatie . HoofdObject ,
111- Resource = notificatie . Resource ,
112- ResourceUrl = notificatie . ResourceUrl ,
113- Actie = notificatie . Actie ,
114- Kenmerken = notificatie . Kenmerken ,
115- CorrelationId = notificatie . CorrelationId ,
116- Rsin = notificatie . Rsin ,
117- } ;
118- }
119- }
0 commit comments