diff --git a/src/Illuminate/Queue/Worker.php b/src/Illuminate/Queue/Worker.php index 782a863c5c9f..37025d7784c8 100644 --- a/src/Illuminate/Queue/Worker.php +++ b/src/Illuminate/Queue/Worker.php @@ -5,12 +5,15 @@ use Exception; use Throwable; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Database\DetectsLostConnections; use Illuminate\Contracts\Debug\ExceptionHandler; use Symfony\Component\Debug\Exception\FatalThrowableError; use Illuminate\Contracts\Cache\Repository as CacheContract; class Worker { + use DetectsLostConnections; + /** * The queue manager instance. * @@ -46,6 +49,13 @@ class Worker */ public $shouldQuit = false; + /** + * Indicates if the worker should stop. + * + * @var bool + */ + public $shouldStop = false; + /** * Indicates if the worker is paused. * @@ -195,6 +205,8 @@ protected function stopIfNecessary(WorkerOptions $options, $lastRestart) $this->stop(12); } elseif ($this->queueShouldRestart($lastRestart)) { $this->stop(); + } elseif ($this->shouldStop) { + $this->stop(1); } } @@ -239,6 +251,8 @@ protected function getNextJob($connection, $queue) } } catch (Exception $e) { $this->exceptions->report($e); + + $this->handleException($e); } catch (Throwable $e) { $this->exceptions->report(new FatalThrowableError($e)); } @@ -258,11 +272,26 @@ protected function runJob($job, $connectionName, WorkerOptions $options) return $this->process($connectionName, $job, $options); } catch (Exception $e) { $this->exceptions->report($e); + + $this->handleException($e); } catch (Throwable $e) { $this->exceptions->report(new FatalThrowableError($e)); } } + /** + * Handle a serious job exception. + * + * @param \Exception $e + * @return void + */ + protected function handleException($e) + { + if ($this->causedByLostConnection($e)) { + $this->shouldStop = true; + } + } + /** * Process the given job from the queue. *