Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[12.x] :feat: Add Job Failure Callbacks to Batch #55916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions 63 src/Illuminate/Bus/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,32 +242,33 @@ public function recordSuccessfulJob(string $jobId)
$counts = $this->decrementPendingJobs($jobId);

if ($this->hasProgressCallbacks()) {
$batch = $this->fresh();

(new Collection($this->options['progress']))->each(function ($handler) use ($batch) {
$this->invokeHandlerCallback($handler, $batch);
});
$this->invokeCallbacks('progress');
}

if ($counts->pendingJobs === 0) {
$this->repository->markAsFinished($this->id);
}

if ($counts->pendingJobs === 0 && $this->hasThenCallbacks()) {
$batch = $this->fresh();

(new Collection($this->options['then']))->each(function ($handler) use ($batch) {
$this->invokeHandlerCallback($handler, $batch);
});
$this->invokeCallbacks('then');
}

if ($counts->allJobsHaveRanExactlyOnce() && $this->hasFinallyCallbacks()) {
$batch = $this->fresh();
$this->invokeCallbacks('finally');
}
}

(new Collection($this->options['finally']))->each(function ($handler) use ($batch) {
$this->invokeHandlerCallback($handler, $batch);
/**
* Invoke the callbacks of the given type.
*/
public function invokeCallbacks(string $type, ?\Throwable $e = null): void
{
$batch = $this->fresh();

(new Collection($this->options[$type]))
->each(function ($handler) use ($batch, $e) {
$this->invokeHandlerCallback($handler, $batch, $e);
});
}
}

/**
Expand Down Expand Up @@ -346,28 +347,22 @@ public function recordFailedJob(string $jobId, $e)
$this->cancel();
}

if ($this->hasProgressCallbacks() && $this->allowsFailures()) {
$batch = $this->fresh();
if ($this->allowsFailures()) {
if ($this->hasProgressCallbacks()) {
$this->invokeCallbacks('progress', $e);
}

(new Collection($this->options['progress']))->each(function ($handler) use ($batch, $e) {
$this->invokeHandlerCallback($handler, $batch, $e);
});
if ($this->hasFailureCallbacks()) {
$this->invokeCallbacks('failure', $e);
}
}

if ($counts->failedJobs === 1 && $this->hasCatchCallbacks()) {
$batch = $this->fresh();

(new Collection($this->options['catch']))->each(function ($handler) use ($batch, $e) {
$this->invokeHandlerCallback($handler, $batch, $e);
});
$this->invokeCallbacks('catch', $e);
}

if ($counts->allJobsHaveRanExactlyOnce() && $this->hasFinallyCallbacks()) {
$batch = $this->fresh();

(new Collection($this->options['finally']))->each(function ($handler) use ($batch, $e) {
$this->invokeHandlerCallback($handler, $batch, $e);
});
$this->invokeCallbacks('finally');
}
}

Expand Down Expand Up @@ -402,6 +397,16 @@ public function hasFinallyCallbacks()
return isset($this->options['finally']) && ! empty($this->options['finally']);
}

/**
* Determine if the batch has "failure" callbacks.
*
* @return bool
*/
public function hasFailureCallbacks()
{
return isset($this->options['failure']) && ! empty($this->options['failure']);
}

/**
* Cancel the batch.
*
Expand Down
29 changes: 23 additions & 6 deletions 29 src/Illuminate/Bus/PendingBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,23 @@ public function finallyCallbacks()
}

/**
* Indicate that the batch should not be cancelled when a job within the batch fails.
*
* @param bool $allowFailures
* @return $this
* Indicate that the batch should not be cancelled when a job within the batch fails;
* optionally add callbacks to be executed upon each job failure (this may be useful for
* running other jobs when a job fails).
*/
public function allowFailures($allowFailures = true)
public function allowFailures(bool|callable|Closure|array $param = true): self
{
$this->options['allowFailures'] = $allowFailures;
if (! is_bool($param)) {
$param = Arr::wrap($param);

foreach ($param as $callback) {
$this->options['failure'][] = $callback instanceof Closure
? new SerializableClosure($callback)
: $callback;
}
}

$this->options['allowFailures'] = (bool) $param;

return $this;
}
Expand All @@ -259,6 +268,14 @@ public function allowsFailures()
return Arr::get($this->options, 'allowFailures', false) === true;
}

/**
* Get the "failure" callbacks that have been registered with the pending batch.
*/
public function failureCallbacks(): array
{
return $this->options['failure'] ?? [];
}

/**
* Set the name for the batch.
*
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.