Skip to content

Commit

Permalink
Fix manually failing jobs.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 25, 2016
1 parent a333324 commit 707a3bc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Illuminate/Queue/InteractsWithQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Queue;

use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Queue\Job as JobContract;

trait InteractsWithQueue
Expand Down Expand Up @@ -43,8 +45,18 @@ public function delete()
*/
public function fail($exception = null)
{
if ($this->job) {
return $this->job->failed($exception ?: new ManuallyFailedException);
if (! $this->job || $this->job->isDeleted()) {
return;
}

try {
$this->job->delete();

$this->job->failed($e);
} finally {
Container::getInstance()->make(Dispatcher::class)->fire(new Events\JobFailed(
$this->job->getConnectionName(), $this->job, $exception ?: new ManuallyFailedException
));
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/Illuminate/Queue/Jobs/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ abstract class Job
*/
protected $container;

/**
* The name of the connection the job belongs to.
*/
protected $connectionName;

/**
* The name of the queue the job belongs to.
*
Expand Down Expand Up @@ -254,6 +259,27 @@ public function timeout()
return array_get($this->payload(), 'timeout');
}

/**
* Get the name of the connection the job belongs to.
*
* @return string
*/
public function getConnectionName()
{
return $this->connectionName;
}

/**
* Set the name of the connection the job belongs to.
*
* @param string $name
* @return void
*/
public function setConnectionName($name)
{
$this->connectionName = $name;
}

/**
* Get the name of the queue the job belongs to.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public function runNextJob($connectionName, $queue, WorkerOptions $options)
// from this method. If there is no job on the queue, we will "sleep" the worker
// for the specified number of seconds, then keep processing jobs after sleep.
if ($job) {
$job->setConnectionName($connectionName);

return $this->runJob($job, $connectionName, $options);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/Queue/QueueWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class WorkerFakeJob
public $maxTries;
public $attempts = 0;
public $failedWith;
public $connectionName;

public function __construct($callback = null)
{
Expand Down Expand Up @@ -290,6 +291,11 @@ public function failed($e)
$this->failedWith = $e;
}

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

public function testJobSleepsWhenAnExceptionIsThrownForADaemonWorker()
{
$exceptionHandler = m::mock('Illuminate\Contracts\Debug\ExceptionHandler');
Expand Down

0 comments on commit 707a3bc

Please sign in to comment.