@@ -242,32 +242,33 @@ public function recordSuccessfulJob(string $jobId)
242
242
$ counts = $ this ->decrementPendingJobs ($ jobId );
243
243
244
244
if ($ this ->hasProgressCallbacks ()) {
245
- $ batch = $ this ->fresh ();
246
-
247
- (new Collection ($ this ->options ['progress ' ]))->each (function ($ handler ) use ($ batch ) {
248
- $ this ->invokeHandlerCallback ($ handler , $ batch );
249
- });
245
+ $ this ->invokeCallbacks ('progress ' );
250
246
}
251
247
252
248
if ($ counts ->pendingJobs === 0 ) {
253
249
$ this ->repository ->markAsFinished ($ this ->id );
254
250
}
255
251
256
252
if ($ counts ->pendingJobs === 0 && $ this ->hasThenCallbacks ()) {
257
- $ batch = $ this ->fresh ();
258
-
259
- (new Collection ($ this ->options ['then ' ]))->each (function ($ handler ) use ($ batch ) {
260
- $ this ->invokeHandlerCallback ($ handler , $ batch );
261
- });
253
+ $ this ->invokeCallbacks ('then ' );
262
254
}
263
255
264
256
if ($ counts ->allJobsHaveRanExactlyOnce () && $ this ->hasFinallyCallbacks ()) {
265
- $ batch = $ this ->fresh ();
257
+ $ this ->invokeCallbacks ('finally ' );
258
+ }
259
+ }
266
260
267
- (new Collection ($ this ->options ['finally ' ]))->each (function ($ handler ) use ($ batch ) {
268
- $ this ->invokeHandlerCallback ($ handler , $ batch );
261
+ /**
262
+ * Invoke the callbacks of the given type.
263
+ */
264
+ public function invokeCallbacks (string $ type , ?\Throwable $ e = null ): void
265
+ {
266
+ $ batch = $ this ->fresh ();
267
+
268
+ (new Collection ($ this ->options [$ type ]))
269
+ ->each (function ($ handler ) use ($ batch , $ e ) {
270
+ $ this ->invokeHandlerCallback ($ handler , $ batch , $ e );
269
271
});
270
- }
271
272
}
272
273
273
274
/**
@@ -346,28 +347,22 @@ public function recordFailedJob(string $jobId, $e)
346
347
$ this ->cancel ();
347
348
}
348
349
349
- if ($ this ->hasProgressCallbacks () && $ this ->allowsFailures ()) {
350
- $ batch = $ this ->fresh ();
350
+ if ($ this ->allowsFailures ()) {
351
+ if ($ this ->hasProgressCallbacks ()) {
352
+ $ this ->invokeCallbacks ('progress ' , $ e );
353
+ }
351
354
352
- ( new Collection ($ this ->options [ ' progress ' ]))-> each ( function ( $ handler ) use ( $ batch , $ e ) {
353
- $ this ->invokeHandlerCallback ( $ handler , $ batch , $ e );
354
- });
355
+ if ($ this ->hasFailureCallbacks () ) {
356
+ $ this ->invokeCallbacks ( ' failure ' , $ e );
357
+ }
355
358
}
356
359
357
360
if ($ counts ->failedJobs === 1 && $ this ->hasCatchCallbacks ()) {
358
- $ batch = $ this ->fresh ();
359
-
360
- (new Collection ($ this ->options ['catch ' ]))->each (function ($ handler ) use ($ batch , $ e ) {
361
- $ this ->invokeHandlerCallback ($ handler , $ batch , $ e );
362
- });
361
+ $ this ->invokeCallbacks ('catch ' , $ e );
363
362
}
364
363
365
364
if ($ counts ->allJobsHaveRanExactlyOnce () && $ this ->hasFinallyCallbacks ()) {
366
- $ batch = $ this ->fresh ();
367
-
368
- (new Collection ($ this ->options ['finally ' ]))->each (function ($ handler ) use ($ batch , $ e ) {
369
- $ this ->invokeHandlerCallback ($ handler , $ batch , $ e );
370
- });
365
+ $ this ->invokeCallbacks ('finally ' );
371
366
}
372
367
}
373
368
@@ -402,6 +397,16 @@ public function hasFinallyCallbacks()
402
397
return isset ($ this ->options ['finally ' ]) && ! empty ($ this ->options ['finally ' ]);
403
398
}
404
399
400
+ /**
401
+ * Determine if the batch has "failure" callbacks.
402
+ *
403
+ * @return bool
404
+ */
405
+ public function hasFailureCallbacks ()
406
+ {
407
+ return isset ($ this ->options ['failure ' ]) && ! empty ($ this ->options ['failure ' ]);
408
+ }
409
+
405
410
/**
406
411
* Cancel the batch.
407
412
*
0 commit comments