diff --git a/Classes/Command/JobCommandController.php b/Classes/Command/JobCommandController.php index faf1b8a..07a8b45 100644 --- a/Classes/Command/JobCommandController.php +++ b/Classes/Command/JobCommandController.php @@ -12,6 +12,7 @@ */ use Flowpack\JobQueue\Common\Exception as JobQueueException; +use Flowpack\JobQueue\Common\InterruptException; use Flowpack\JobQueue\Common\Job\JobManager; use Flowpack\JobQueue\Common\Queue\Message; use Flowpack\JobQueue\Common\Queue\QueueManager; @@ -74,6 +75,11 @@ public function workCommand($queue, $exitAfter = null, $limit = null, $verbose = } $this->outputLine('...'); } + if (function_exists('pcntl_signal')) { + pcntl_signal(SIGINT, static function () { + throw new InterruptException('Interrupted by SIGINT'); + }); + } $startTime = time(); $timeout = null; $numberOfJobExecutions = 0; @@ -84,6 +90,12 @@ public function workCommand($queue, $exitAfter = null, $limit = null, $verbose = } try { $message = $this->jobManager->waitAndExecute($queue, $timeout); + $this->jobManager->interruptMe(); + } catch (InterruptException $exception) { + if ($verbose) { + $this->outputLine('Quitting after %d seconds due to received interruption', [time() - $startTime]); + } + $this->quit(); } catch (JobQueueException $exception) { $numberOfJobExecutions ++; $this->outputLine('%s', [$exception->getMessage()]); diff --git a/Classes/InterruptException.php b/Classes/InterruptException.php new file mode 100644 index 0000000..84a04ef --- /dev/null +++ b/Classes/InterruptException.php @@ -0,0 +1,8 @@ +