diff --git a/src/Illuminate/Bus/Batch.php b/src/Illuminate/Bus/Batch.php index c28386f17e71..463053a6c948 100644 --- a/src/Illuminate/Bus/Batch.php +++ b/src/Illuminate/Bus/Batch.php @@ -164,7 +164,7 @@ public function add($jobs) $jobs->each->withBatchId($this->id); $this->repository->transaction(function () use ($jobs) { - $this->repository->incrementTotalJobs($this->id, count($jobs)); + $this->repository->incrementTotalJobs($this->id, $this->countJobs($jobs)); $this->queue->connection($this->options['connection'] ?? null)->bulk( $jobs->all(), @@ -427,4 +427,16 @@ public function jsonSerialize() { return $this->toArray(); } + + /** + * @param Collection $jobs + * + * @return int + */ + private function countJobs(Collection $jobs) + { + return $jobs->reduce(function (int $count, $job) { + return $count + count($job->chained ?? []) + 1; + }, 0); + } } diff --git a/src/Illuminate/Bus/Queueable.php b/src/Illuminate/Bus/Queueable.php index c2520b98c040..425992bb146e 100644 --- a/src/Illuminate/Bus/Queueable.php +++ b/src/Illuminate/Bus/Queueable.php @@ -191,6 +191,10 @@ public function dispatchNextJobInChain() { if (! empty($this->chained)) { dispatch(tap(unserialize(array_shift($this->chained)), function ($next) { + if (property_exists($this, 'batchId') && method_exists($next, 'withBatchId')) { + $next->withBatchId($this->batchId); + } + $next->chained = $this->chained; $next->onConnection($next->connection ?: $this->chainConnection); diff --git a/src/Illuminate/Foundation/Bus/PendingChain.php b/src/Illuminate/Foundation/Bus/PendingChain.php index 8965e9923fa1..6efbabb234da 100644 --- a/src/Illuminate/Foundation/Bus/PendingChain.php +++ b/src/Illuminate/Foundation/Bus/PendingChain.php @@ -114,6 +114,22 @@ public function catchCallbacks() * @return \Illuminate\Foundation\Bus\PendingDispatch */ public function dispatch() + { + return app(Dispatcher::class)->dispatch($this->prepareFirstJob()); + } + + /** + * @return CallQueuedClosure + */ + public function dispatchInBatch() + { + return $this->prepareFirstJob(); + } + + /** + * @return CallQueuedClosure|mixed + */ + public function prepareFirstJob() { if (is_string($this->job)) { $firstJob = new $this->job(...func_get_args()); @@ -128,6 +144,6 @@ public function dispatch() $firstJob->chain($this->chain); $firstJob->chainCatchCallbacks = $this->catchCallbacks(); - return app(Dispatcher::class)->dispatch($firstJob); + return $firstJob; } }