@@ -101,6 +101,7 @@ class Batch implements Arrayable, JsonSerializable
101
101
/**
102
102
* Create a new batch instance.
103
103
*
104
+ *
104
105
* @param \Illuminate\Contracts\Queue\Factory $queue
105
106
* @param \Illuminate\Bus\BatchRepository $repository
106
107
* @param string $id
@@ -225,7 +226,7 @@ public function processedJobs()
225
226
* Get the percentage of jobs that have been processed (between 0-100).
226
227
*
227
228
* @return int
228
- */
229
+ ** /
229
230
public function progress ()
230
231
{
231
232
return $ this ->totalJobs > 0 ? round (($ this ->processedJobs () / $ this ->totalJobs ) * 100 ) : 0 ;
@@ -242,32 +243,33 @@ public function recordSuccessfulJob(string $jobId)
242
243
$ counts = $ this ->decrementPendingJobs ($ jobId );
243
244
244
245
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
- });
246
+ $ this ->invokeCallbacks ('progress ' );
250
247
}
251
248
252
249
if ($ counts ->pendingJobs === 0 ) {
253
250
$ this ->repository ->markAsFinished ($ this ->id );
254
251
}
255
252
256
253
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
- });
254
+ $ this ->invokeCallbacks ('then ' );
262
255
}
263
256
264
257
if ($ counts ->allJobsHaveRanExactlyOnce () && $ this ->hasFinallyCallbacks ()) {
265
- $ batch = $ this ->fresh ();
258
+ $ this ->invokeCallbacks ('finally ' );
259
+ }
260
+ }
266
261
267
- (new Collection ($ this ->options ['finally ' ]))->each (function ($ handler ) use ($ batch ) {
268
- $ this ->invokeHandlerCallback ($ handler , $ batch );
262
+ /**
263
+ * Invoke the callbacks of the given type.
264
+ */
265
+ public function invokeCallbacks (string $ type , ?\Throwable $ e = null ): void
266
+ {
267
+ $ batch = $ this ->fresh ();
268
+
269
+ (new Collection ($ this ->options [$ type ]))
270
+ ->each (function ($ handler ) use ($ batch , $ e ) {
271
+ $ this ->invokeHandlerCallback ($ handler , $ batch , $ e );
269
272
});
270
- }
271
273
}
272
274
273
275
/**
@@ -346,28 +348,22 @@ public function recordFailedJob(string $jobId, $e)
346
348
$ this ->cancel ();
347
349
}
348
350
349
- if ($ this ->hasProgressCallbacks () && $ this ->allowsFailures ()) {
350
- $ batch = $ this ->fresh ();
351
+ if ($ this ->allowsFailures ()) {
352
+ if ($ this ->hasProgressCallbacks ()) {
353
+ $ this ->invokeCallbacks ('progress ' , $ e );
354
+ }
351
355
352
- ( new Collection ($ this ->options [ ' progress ' ]))-> each ( function ( $ handler ) use ( $ batch , $ e ) {
353
- $ this ->invokeHandlerCallback ( $ handler , $ batch , $ e );
354
- });
356
+ if ($ this ->hasFailureCallbacks () ) {
357
+ $ this ->invokeCallbacks ( ' failure ' , $ e );
358
+ }
355
359
}
356
360
357
361
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
- });
362
+ $ this ->invokeCallbacks ('catch ' , $ e );
363
363
}
364
364
365
365
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
- });
366
+ $ this ->invokeCallbacks ('finally ' );
371
367
}
372
368
}
373
369
@@ -402,6 +398,16 @@ public function hasFinallyCallbacks()
402
398
return isset ($ this ->options ['finally ' ]) && ! empty ($ this ->options ['finally ' ]);
403
399
}
404
400
401
+ /**
402
+ * Determine if the batch has "failure" callbacks.
403
+ *
404
+ * @return bool
405
+ */
406
+ public function hasFailureCallbacks ()
407
+ {
408
+ return isset ($ this ->options ['failure ' ]) && ! empty ($ this ->options ['failure ' ]);
409
+ }
410
+
405
411
/**
406
412
* Cancel the batch.
407
413
*
0 commit comments