diff --git a/src/Illuminate/Queue/InteractsWithQueue.php b/src/Illuminate/Queue/InteractsWithQueue.php index bddd216f36ba..02d48478f22b 100644 --- a/src/Illuminate/Queue/InteractsWithQueue.php +++ b/src/Illuminate/Queue/InteractsWithQueue.php @@ -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 @@ -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 + )); } } diff --git a/src/Illuminate/Queue/Jobs/Job.php b/src/Illuminate/Queue/Jobs/Job.php index a5e1e108c7a5..a506fb251a20 100755 --- a/src/Illuminate/Queue/Jobs/Job.php +++ b/src/Illuminate/Queue/Jobs/Job.php @@ -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. * @@ -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. * diff --git a/src/Illuminate/Queue/Worker.php b/src/Illuminate/Queue/Worker.php index 958aa415107a..8871bef04ab9 100644 --- a/src/Illuminate/Queue/Worker.php +++ b/src/Illuminate/Queue/Worker.php @@ -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); } diff --git a/tests/Queue/QueueWorkerTest.php b/tests/Queue/QueueWorkerTest.php index 7918f7729d13..18f4a1e15526 100755 --- a/tests/Queue/QueueWorkerTest.php +++ b/tests/Queue/QueueWorkerTest.php @@ -242,6 +242,7 @@ class WorkerFakeJob public $maxTries; public $attempts = 0; public $failedWith; + public $connectionName; public function __construct($callback = null) { @@ -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');