@@ -168,12 +168,12 @@ public function add($jobs)
168168 if (is_array ($ job )) {
169169 $ count += count ($ job );
170170
171- return with ( $ this ->prepareBatchedChain ($ job ), function ( $ chain ) {
172- return $ chain -> first ()
173- -> allOnQueue ( $ this -> options [ ' queue ' ] ?? null )
174- -> allOnConnection ($ this ->options ['connection ' ] ?? null )
175- -> chain ( $ chain -> slice ( 1 )-> values ()-> all ());
176- } );
171+ $ chain = $ this ->prepareBatchedChain ($ job );
172+
173+ return $ chain -> first ( )
174+ -> allOnQueue ($ this ->options ['queue ' ] ?? null )
175+ -> allOnConnection ( $ this -> options [ ' connection ' ] ?? null )
176+ -> chain ( $ chain -> slice ( 1 )-> values ()-> all () );
177177 } else {
178178 $ job ->withBatchId ($ this ->id );
179179
@@ -242,31 +242,19 @@ public function recordSuccessfulJob(string $jobId)
242242 $ counts = $ this ->decrementPendingJobs ($ jobId );
243243
244244 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 ' );
250246 }
251247
252248 if ($ counts ->pendingJobs === 0 ) {
253249 $ this ->repository ->markAsFinished ($ this ->id );
254250 }
255251
256252 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 ' );
262254 }
263255
264256 if ($ counts ->allJobsHaveRanExactlyOnce () && $ this ->hasFinallyCallbacks ()) {
265- $ batch = $ this ->fresh ();
266-
267- (new Collection ($ this ->options ['finally ' ]))->each (function ($ handler ) use ($ batch ) {
268- $ this ->invokeHandlerCallback ($ handler , $ batch );
269- });
257+ $ this ->invokeCallbacks ('finally ' );
270258 }
271259 }
272260
@@ -281,6 +269,18 @@ public function decrementPendingJobs(string $jobId)
281269 return $ this ->repository ->decrementPendingJobs ($ this ->id , $ jobId );
282270 }
283271
272+ /**
273+ * Invoke the callbacks of the given type.
274+ */
275+ protected function invokeCallbacks (string $ type , ?Throwable $ e = null ): void
276+ {
277+ $ batch = $ this ->fresh ();
278+
279+ foreach ($ this ->options [$ type ] ?? [] as $ handler ) {
280+ $ this ->invokeHandlerCallback ($ handler , $ batch , $ e );
281+ }
282+ }
283+
284284 /**
285285 * Determine if the batch has finished executing.
286286 *
@@ -346,28 +346,22 @@ public function recordFailedJob(string $jobId, $e)
346346 $ this ->cancel ();
347347 }
348348
349- if ($ this ->hasProgressCallbacks () && $ this ->allowsFailures ()) {
350- $ batch = $ this ->fresh ();
349+ if ($ this ->allowsFailures ()) {
350+ if ($ this ->hasProgressCallbacks ()) {
351+ $ this ->invokeCallbacks ('progress ' , $ e );
352+ }
351353
352- ( new Collection ($ this ->options [ ' progress ' ]))-> each ( function ( $ handler ) use ( $ batch , $ e ) {
353- $ this ->invokeHandlerCallback ( $ handler , $ batch , $ e );
354- });
354+ if ($ this ->hasFailureCallbacks () ) {
355+ $ this ->invokeCallbacks ( ' failure ' , $ e );
356+ }
355357 }
356358
357359 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- });
360+ $ this ->invokeCallbacks ('catch ' , $ e );
363361 }
364362
365363 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- });
364+ $ this ->invokeCallbacks ('finally ' );
371365 }
372366 }
373367
@@ -392,6 +386,14 @@ public function hasCatchCallbacks()
392386 return isset ($ this ->options ['catch ' ]) && ! empty ($ this ->options ['catch ' ]);
393387 }
394388
389+ /**
390+ * Determine if the batch has "failure" callbacks.
391+ */
392+ public function hasFailureCallbacks (): bool
393+ {
394+ return isset ($ this ->options ['failure ' ]) && ! empty ($ this ->options ['failure ' ]);
395+ }
396+
395397 /**
396398 * Determine if the batch has "finally" callbacks.
397399 *
0 commit comments