@@ -132,11 +132,11 @@ public void Run(CancellationToken cancellation = default)
132
132
133
133
// individual processing should not be cancelled as we have already grabbed from the queue.
134
134
Task . Factory . StartNew ( ( ) => { ProcessResults ( items ) ; } , CancellationToken . None , TaskCreationOptions . HideScheduler , threadPool )
135
- . ContinueWith ( t =>
135
+ . ContinueWith ( _ =>
136
136
{
137
137
foreach ( var item in items )
138
138
{
139
- if ( t . Exception != null || item . Failed )
139
+ if ( item . Failed )
140
140
{
141
141
Interlocked . Increment ( ref totalErrors ) ;
142
142
@@ -145,12 +145,12 @@ public void Run(CancellationToken cancellation = default)
145
145
146
146
Interlocked . Increment ( ref consecutiveErrors ) ;
147
147
148
- Error ? . Invoke ( t . Exception , item ) ;
148
+ Error ? . Invoke ( item . Exception , item ) ;
149
149
150
- if ( t . Exception != null )
151
- SentrySdk . CaptureException ( t . Exception ) ;
150
+ if ( item . Exception != null )
151
+ SentrySdk . CaptureException ( item . Exception ) ;
152
152
153
- Console . WriteLine ( $ "Error processing { item } : { t . Exception } ") ;
153
+ Console . WriteLine ( $ "Error processing { item } : { item . Exception } ") ;
154
154
attemptRetry ( item ) ;
155
155
}
156
156
else
@@ -197,8 +197,6 @@ private void setupSentry(SentryOptions options)
197
197
198
198
private void attemptRetry ( T item )
199
199
{
200
- item . Failed = false ;
201
-
202
200
if ( item . TotalRetries ++ < config . MaxRetries )
203
201
{
204
202
Console . WriteLine ( $ "Re-queueing for attempt { item . TotalRetries } / { config . MaxRetries } ") ;
@@ -274,11 +272,26 @@ protected virtual void ProcessResult(T item)
274
272
/// <summary>
275
273
/// Implement to process batches of items from the queue.
276
274
/// </summary>
275
+ /// <remarks>
276
+ /// In most cases, you should only need to override and implement <see cref="ProcessResult"/>.
277
+ /// Only override this if you need more efficient batch processing.
278
+ ///
279
+ /// If overriding this method, you should try-catch for exceptions, and set any exception against
280
+ /// the relevant <see cref="QueueItem"/>. If this is not done, failures will not be handled correctly.</remarks>
277
281
/// <param name="items">The items to process.</param>
278
282
protected virtual void ProcessResults ( IEnumerable < T > items )
279
283
{
280
284
foreach ( var item in items )
281
- ProcessResult ( item ) ;
285
+ {
286
+ try
287
+ {
288
+ ProcessResult ( item ) ;
289
+ }
290
+ catch ( Exception e )
291
+ {
292
+ item . Exception = e ;
293
+ }
294
+ }
282
295
}
283
296
}
284
297
}
0 commit comments