Skip to content

Commit

Permalink
Merge pull request #28 from laravel/pr7428
Browse files Browse the repository at this point in the history
Handle jobs timeouts
  • Loading branch information
taylorotwell authored Feb 7, 2020
2 parents 66e9978 + 7d1f571 commit bfd7755
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Console/Commands/VaporWorkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}';

Expand Down Expand Up @@ -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
);
Expand Down
11 changes: 11 additions & 0 deletions src/Queue/VaporWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Queue\Worker;
use Illuminate\Queue\WorkerOptions;
use Laravel\Vapor\VaporJobTimedOutException;

class VaporWorker extends Worker
{
Expand All @@ -17,6 +18,16 @@ class VaporWorker extends Worker
*/
public function runVaporJob($job, $connectionName, WorkerOptions $options)
{
pcntl_async_signals(true);

pcntl_signal(SIGALRM, function () use ($job) {
throw new VaporJobTimedOutException($job->resolveName());
});

pcntl_alarm(
max($this->timeoutForJob($job, $options), 0)
);

return $this->runJob($job, $connectionName, $options);
}
}
3 changes: 2 additions & 1 deletion src/Runtime/Handlers/QueueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' : ''
));
Expand Down
20 changes: 20 additions & 0 deletions src/VaporJobTimedOutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Laravel\Vapor;

use Throwable;
use Exception;

class VaporJobTimedOutException extends Exception
{
/**
* Create a new exception instance.
*
* @param string $name
* @param Throwable|null $previous
*/
public function __construct($name, Throwable $previous = null)
{
parent::__construct($name. ' has timed out. It will be retried again.', 0, $previous);
}
}

0 comments on commit bfd7755

Please sign in to comment.