Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 7, 2020
1 parent eb4f307 commit 7b4a9ec
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/Illuminate/Bus/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,37 +161,28 @@ public function fresh()
*/
public function add($jobs)
{
$jobTotal = 0;
$jobs = Collection::wrap($jobs)->map(function ($job) use (&$jobTotal) {
if ($job instanceof Closure) {
$job = CallQueuedClosure::create($job);
}

if (is_array($job)) {
$jobChain = $job;
$batchId = $this->id;
array_walk($jobChain, function (&$job) use ($batchId) {
if ($job instanceof Closure) {
$job = CallQueuedClosure::create($job);
}
$job->withBatchId($batchId);
});
$count = 0;

$jobTotal += count($jobChain);
$jobs = Collection::wrap($jobs)->map(function ($job) use (&$count) {
$job = $job instanceof Closure ? CallQueuedClosure::create($job) : $job;

$chainHead = array_shift($jobChain);
if (is_array($job)) {
$count += count($job);

return $chainHead->chain($jobChain);
return with($this->prepareBatchedChain($job), function ($chain) {
return $chain->first()->chain($chain->slice(1)->values()->all());
});
} else {
$job->withBatchId($this->id);
$jobTotal++;

$count++;
}

return $job;
});

$this->repository->transaction(function () use ($jobs, $jobTotal) {
$this->repository->incrementTotalJobs($this->id, $jobTotal);
$this->repository->transaction(function () use ($jobs, $count) {
$this->repository->incrementTotalJobs($this->id, $count);

$this->queue->connection($this->options['connection'] ?? null)->bulk(
$jobs->all(),
Expand All @@ -203,6 +194,21 @@ public function add($jobs)
return $this->fresh();
}

/**
* Prepare a chain that exists within the jobs being added.
*
* @param array $chain
* @return \Illuminate\Support\Collection
*/
protected function prepareBatchedChain(array $chain)
{
return collect($chain)->map(function ($job) {
$job = $job instanceof Closure ? CallQueuedClosure::create($job) : $job;

return $job->withBatchId($this->id);
});
}

/**
* Get the total number of jobs that have been processed by the batch thus far.
*
Expand Down

0 comments on commit 7b4a9ec

Please sign in to comment.