Skip to content

Commit

Permalink
Fixed executed jobs limit to follow yiisoft/yii2-queue API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed May 29, 2018
1 parent 482bf32 commit c98c9fa
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/console/QueueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use yii\queue\ErrorEvent;
use yii\queue\JobEvent;
use yii\queue\Queue;
use yii\queue\cli\Queue as CliQueue;

/**
* Manages service Queue.
Expand Down Expand Up @@ -71,10 +72,11 @@ public function actionTestErrorHandler()

private function ensureLimits()
{
if (++$this->executedJobsCount >= static::MAX_EXECUTED_JOBS && function_exists('posix_kill')) {
$this->stdout('Reached limit of ' . static::MAX_EXECUTED_JOBS . " executed jobs. Stopping process.\n");
Signal::setExitFlag();
if ($this->executedJobsCount++ > static::MAX_EXECUTED_JOBS && function_exists('posix_kill')) {
return 15; // SIGTERM
}

return null;
}

/**
Expand Down Expand Up @@ -102,6 +104,14 @@ private function attachEventHandlers()
$out("%RJob '" . get_class($event->job) . "' finished with error:%n '" . $event->error . "'\n");
});

Event::on(Queue::class, CliQueue::EVENT_WORKER_LOOP, function (\yii\queue\cli\WorkerEvent $event) use ($out) {
$exitCode = $this->ensureLimits();
if ($exitCode !== null) {
$out('Reached limit of ' . static::MAX_EXECUTED_JOBS . " executed jobs. Stopping process.\n");
$event->exitCode = $exitCode;
}
});

Event::on(AbstractPackageCommand::class, AbstractPackageCommand::EVENT_BEFORE_RUN, function ($event) use ($out) {
/** @var AbstractPackageCommand $command */
$command = $event->sender;
Expand Down

0 comments on commit c98c9fa

Please sign in to comment.