diff --git a/src/Illuminate/Queue/Worker.php b/src/Illuminate/Queue/Worker.php index 18f5de854c41..49aab4a3221e 100644 --- a/src/Illuminate/Queue/Worker.php +++ b/src/Illuminate/Queue/Worker.php @@ -70,6 +70,8 @@ public function daemon($connectionName, $queue, WorkerOptions $options) $lastRestart = $this->getTimestampOfLastQueueRestart(); while (true) { + $this->registerTimeoutHandler($options); + if ($this->daemonShouldRun()) { $this->runNextJob($connectionName, $queue, $options); } else { @@ -83,6 +85,29 @@ public function daemon($connectionName, $queue, WorkerOptions $options) } } + /** + * Register the worker timeout handler (PHP 7.1+). + * + * @param WorkerOptions $options + * @return void + */ + protected function registerTimeoutHandler(WorkerOptions $options) + { + if (version_compare(PHP_VERSION, '7.1.0') < 0 || ! extension_loaded('pcntl')) { + return; + } + + pcntl_async_signals(true); + + pcntl_signal(SIGALRM, function () { + $this->exceptions->report(new TimeoutException("A queue worker timed out while processing a job.")); + + exit(1); + }); + + pcntl_alarm($options->timeout + $options->sleep); + } + /** * Determine if the daemon should process on this iteration. *