@@ -132,11 +132,11 @@ public void Run(CancellationToken cancellation = default)
132132
133133 // individual processing should not be cancelled as we have already grabbed from the queue.
134134 Task . Factory . StartNew ( ( ) => { ProcessResults ( items ) ; } , CancellationToken . None , TaskCreationOptions . HideScheduler , threadPool )
135- . ContinueWith ( t =>
135+ . ContinueWith ( _ =>
136136 {
137137 foreach ( var item in items )
138138 {
139- if ( t . Exception != null || item . Failed )
139+ if ( item . Failed )
140140 {
141141 Interlocked . Increment ( ref totalErrors ) ;
142142
@@ -145,12 +145,18 @@ public void Run(CancellationToken cancellation = default)
145145
146146 Interlocked . Increment ( ref consecutiveErrors ) ;
147147
148- Error ? . Invoke ( t . Exception , item ) ;
148+ try
149+ {
150+ Error ? . Invoke ( item . Exception , item ) ;
151+ }
152+ catch
153+ {
154+ }
149155
150- if ( t . Exception != null )
151- SentrySdk . CaptureException ( t . Exception ) ;
156+ if ( item . Exception != null )
157+ SentrySdk . CaptureException ( item . Exception ) ;
152158
153- Console . WriteLine ( $ "Error processing { item } : { t . Exception } ") ;
159+ Console . WriteLine ( $ "Error processing { item } : { item . Exception } ") ;
154160 attemptRetry ( item ) ;
155161 }
156162 else
@@ -197,8 +203,6 @@ private void setupSentry(SentryOptions options)
197203
198204 private void attemptRetry ( T item )
199205 {
200- item . Failed = false ;
201-
202206 if ( item . TotalRetries ++ < config . MaxRetries )
203207 {
204208 Console . WriteLine ( $ "Re-queueing for attempt { item . TotalRetries } / { config . MaxRetries } ") ;
@@ -274,11 +278,26 @@ protected virtual void ProcessResult(T item)
274278 /// <summary>
275279 /// Implement to process batches of items from the queue.
276280 /// </summary>
281+ /// <remarks>
282+ /// In most cases, you should only need to override and implement <see cref="ProcessResult"/>.
283+ /// Only override this if you need more efficient batch processing.
284+ ///
285+ /// If overriding this method, you should try-catch for exceptions, and set any exception against
286+ /// the relevant <see cref="QueueItem"/>. If this is not done, failures will not be handled correctly.</remarks>
277287 /// <param name="items">The items to process.</param>
278288 protected virtual void ProcessResults ( IEnumerable < T > items )
279289 {
280290 foreach ( var item in items )
281- ProcessResult ( item ) ;
291+ {
292+ try
293+ {
294+ ProcessResult ( item ) ;
295+ }
296+ catch ( Exception e )
297+ {
298+ item . Exception = e ;
299+ }
300+ }
282301 }
283302 }
284303}
0 commit comments