From 2b1738a036c419a971633730a13a520182c947c0 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Fri, 7 Feb 2020 18:05:04 +0200 Subject: [PATCH 1/2] handle jobs timeouts --- src/Console/Commands/VaporWorkCommand.php | 3 ++- src/Queue/VaporWorker.php | 11 +++++++++++ src/Runtime/Handlers/QueueHandler.php | 3 ++- src/VaporJobTimedOutException.php | 19 +++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/VaporJobTimedOutException.php diff --git a/src/Console/Commands/VaporWorkCommand.php b/src/Console/Commands/VaporWorkCommand.php index 8b2f55e..33cf17b 100644 --- a/src/Console/Commands/VaporWorkCommand.php +++ b/src/Console/Commands/VaporWorkCommand.php @@ -21,6 +21,7 @@ class VaporWorkCommand extends Command protected $signature = 'vapor:work {message : The Base64 encoded message payload} {--delay=0 : The number of seconds to delay failed jobs} + {--timeout=0 : The number of seconds a child process can run} {--tries=0 : Number of times to attempt a job before logging it failed} {--force : Force the worker to run even in maintenance mode}'; @@ -167,7 +168,7 @@ protected function gatherWorkerOptions() { return new WorkerOptions( $this->option('delay'), $memory = 512, - $timeout = 0, $sleep = 0, + $this->option('timeout'), $sleep = 0, $this->option('tries'), $this->option('force'), $stopWhenEmpty = false ); diff --git a/src/Queue/VaporWorker.php b/src/Queue/VaporWorker.php index e3a9178..c7f962e 100644 --- a/src/Queue/VaporWorker.php +++ b/src/Queue/VaporWorker.php @@ -4,6 +4,7 @@ use Illuminate\Queue\Worker; use Illuminate\Queue\WorkerOptions; +use Laravel\Vapor\VaporJobTimedOutException; class VaporWorker extends Worker { @@ -17,6 +18,16 @@ class VaporWorker extends Worker */ public function runVaporJob($job, $connectionName, WorkerOptions $options) { + pcntl_async_signals(true); + + pcntl_signal(SIGALRM, function (){ + throw new VaporJobTimedOutException(); + }); + + pcntl_alarm( + max($this->timeoutForJob($job, $options), 0) + ); + return $this->runJob($job, $connectionName, $options); } } diff --git a/src/Runtime/Handlers/QueueHandler.php b/src/Runtime/Handlers/QueueHandler.php index ffd4dc9..e9e2e06 100644 --- a/src/Runtime/Handlers/QueueHandler.php +++ b/src/Runtime/Handlers/QueueHandler.php @@ -40,8 +40,9 @@ public function __construct() public function handle(array $event) { $commandOptions = trim(sprintf( - '--delay=%s --tries=%s %s', + '--delay=%s --timeout=%s --tries=%s %s', $_ENV['SQS_DELAY'] ?? 3, + $_ENV['QUEUE_TIMEOUT'] ?? 0, $_ENV['SQS_TRIES'] ?? 3, ($_ENV['SQS_FORCE'] ?? false) ? '--force' : '' )); diff --git a/src/VaporJobTimedOutException.php b/src/VaporJobTimedOutException.php new file mode 100644 index 0000000..7292eee --- /dev/null +++ b/src/VaporJobTimedOutException.php @@ -0,0 +1,19 @@ + Date: Fri, 7 Feb 2020 18:11:09 +0200 Subject: [PATCH 2/2] better exception message --- src/Queue/VaporWorker.php | 4 ++-- src/VaporJobTimedOutException.php | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Queue/VaporWorker.php b/src/Queue/VaporWorker.php index c7f962e..908bf7f 100644 --- a/src/Queue/VaporWorker.php +++ b/src/Queue/VaporWorker.php @@ -20,8 +20,8 @@ public function runVaporJob($job, $connectionName, WorkerOptions $options) { pcntl_async_signals(true); - pcntl_signal(SIGALRM, function (){ - throw new VaporJobTimedOutException(); + pcntl_signal(SIGALRM, function () use ($job) { + throw new VaporJobTimedOutException($job->resolveName()); }); pcntl_alarm( diff --git a/src/VaporJobTimedOutException.php b/src/VaporJobTimedOutException.php index 7292eee..e61cda6 100644 --- a/src/VaporJobTimedOutException.php +++ b/src/VaporJobTimedOutException.php @@ -10,10 +10,11 @@ class VaporJobTimedOutException extends Exception /** * Create a new exception instance. * + * @param string $name * @param Throwable|null $previous */ - public function __construct(Throwable $previous = null) + public function __construct($name, Throwable $previous = null) { - parent::__construct("A queued job has timed out. It will be retried again.", 0, $previous); + parent::__construct($name. ' has timed out. It will be retried again.', 0, $previous); } }