@@ -101,18 +101,19 @@ class Batch implements Arrayable, JsonSerializable
101
101
/**
102
102
* Create a new batch instance.
103
103
*
104
- * @param \Illuminate\Contracts\Queue\Factory $queue
105
- * @param \Illuminate\Bus\BatchRepository $repository
106
- * @param string $id
107
- * @param string $name
108
- * @param int $totalJobs
109
- * @param int $pendingJobs
110
- * @param int $failedJobs
111
- * @param array $failedJobIds
112
- * @param array $options
113
- * @param \Carbon\CarbonImmutable $createdAt
114
- * @param \Carbon\CarbonImmutable|null $cancelledAt
115
- * @param \Carbon\CarbonImmutable|null $finishedAt
104
+ *
105
+ * @param \Illuminate\Contracts\Queue\Factory $queue
106
+ * @param \Illuminate\Bus\BatchRepository $repository
107
+ * @param string $id
108
+ * @param string $name
109
+ * @param int $totalJobs
110
+ * @param int $pendingJobs
111
+ * @param int $failedJobs
112
+ * @param array $failedJobIds
113
+ * @param array $options
114
+ * @param \Carbon\CarbonImmutable $createdAt
115
+ * @param \Carbon\CarbonImmutable|null $cancelledAt
116
+ * @param \Carbon\CarbonImmutable|null $finishedAt
116
117
*/
117
118
public function __construct (
118
119
QueueFactory $ queue ,
@@ -225,8 +226,8 @@ 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
- public function progress ()
229
+ ** /
230
+ public function progress (): int
230
231
{
231
232
return $ this ->totalJobs > 0 ? round (($ this ->processedJobs () / $ this ->totalJobs ) * 100 ) : 0 ;
232
233
}
@@ -236,38 +237,39 @@ public function progress()
236
237
*
237
238
* @param string $jobId
238
239
* @return void
239
- */
240
+ ** /
240
241
public function recordSuccessfulJob (string $ jobId )
241
242
{
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
+ }
261
+
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 ();
266
268
267
- (new Collection ($ this ->options ['finally ' ]))->each (function ($ handler ) use ($ batch ) {
268
- $ this ->invokeHandlerCallback ($ handler , $ batch );
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
/**
@@ -338,36 +340,30 @@ public function hasFailures()
338
340
* @param \Throwable $e
339
341
* @return void
340
342
*/
341
- public function recordFailedJob (string $ jobId , $ e )
343
+ public function recordFailedJob ($ jobId , $ e )
342
344
{
343
345
$ counts = $ this ->incrementFailedJobs ($ jobId );
344
346
345
347
if ($ counts ->failedJobs === 1 && ! $ this ->allowsFailures ()) {
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
*
@@ -446,8 +452,8 @@ public function delete()
446
452
* Invoke a batch callback handler.
447
453
*
448
454
* @param callable $handler
449
- * @param \Illuminate\Bus\Batch $batch
450
- * @param \Throwable|null $e
455
+ * @param \Illuminate\Bus\Batch $batch
456
+ * @param \Throwable|null $e
451
457
* @return void
452
458
*/
453
459
protected function invokeHandlerCallback ($ handler , Batch $ batch , ?Throwable $ e = null )
0 commit comments