Skip to content

Commit

Permalink
Fix job release on exception
Browse files Browse the repository at this point in the history
Without this change the worker does not account for changes to the job state done in the JobExceptionOccurred event listener except for when the job was deleted. If there is a need to release the job back onto the queue with a different delay than the one set on the worker, the job would be released with the worker delay. If the job needs to be failed by the JobExceptionOccurred event listener that is not possible and will be ignored by the worker.

A job should not be released back onto the queue if the job has been handled in the JobExceptionOccurred event listener by failing, deleting or releasing it with a specific delay. The event listener should have full controller over what happens with the job for what concerns deleting, failing and deleting the job.
  • Loading branch information
cgrama-ac committed Apr 9, 2017
1 parent 73c673e commit f8f2540
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ protected function handleJobException($connectionName, $job, WorkerOptions $opti
// If we catch an exception, we will attempt to release the job back onto the queue
// so it is not lost entirely. This'll let the job be retried at a later time by
// another listener (or this same one). We will re-throw this exception after.
if (! $job->isDeleted()) {
// Do not release the job back onto the queue if the job is either deleted, failed
// or if the job has already been released from the JobExceptionOccurred event listener.
if (! ($job->isReleased() || $job->isDeleted() || $job->hasFailed())) {
$job->release($options->delay);
}
}
Expand Down
18 changes: 17 additions & 1 deletion tests/Queue/QueueWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,11 @@ class WorkerFakeJob
public $callback;
public $deleted = false;
public $releaseAfter;
public $released = false;
public $maxTries;
public $attempts = 0;
public $failedWith;
public $failed = false;
public $connectionName;

public function __construct($callback = null)
Expand Down Expand Up @@ -296,24 +298,38 @@ public function isDeleted()

public function release($delay)
{
$this->released = true;

$this->releaseAfter = $delay;
}

public function isReleased()
{
return $this->released;
}

public function attempts()
{
return $this->attempts;
}

public function markAsFailed()
{
//
$this->failed = true;
}

public function failed($e)
{
$this->markAsFailed();

$this->failedWith = $e;
}

public function hasFailed()
{
return $this->failed;
}

public function setConnectionName($name)
{
$this->connectionName = $name;
Expand Down

0 comments on commit f8f2540

Please sign in to comment.