Skip to content

Commit

Permalink
Updated to use yiisoft/yii2-queue, implemented sequention jobs exec…
Browse files Browse the repository at this point in the history
…ution limit
  • Loading branch information
SilverFire committed Sep 29, 2017
1 parent 2ba21ee commit 9f54f52
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/commands/AbstractPackageCommand.php
Expand Up @@ -14,7 +14,7 @@
use hiqdev\assetpackagist\repositories\PackageRepository;
use Yii;
use yii\base\Component;
use zhuravljov\yii\queue\Job;
use yii\queue\Job;

abstract class AbstractPackageCommand extends Component implements Job
{
Expand Down
5 changes: 4 additions & 1 deletion src/config/common.php
Expand Up @@ -29,7 +29,7 @@
'db' => 'db',
],
'queue' => [
'class' => \zhuravljov\yii\queue\db\Queue::class,
'class' => \yii\queue\db\Queue::class,
'db' => 'db',
'tableName' => '{{%queue}}',
'channel' => 'package',
Expand Down Expand Up @@ -58,6 +58,9 @@
\hiqdev\assetpackagist\components\StorageInterface::class => function () {
return Yii::$app->get('packageStorage');
},
\yii\queue\Queue::class => function () {
return Yii::$app->get('queue');
}
],
],
];
43 changes: 38 additions & 5 deletions src/console/QueueController.php
Expand Up @@ -13,17 +13,40 @@
use hiqdev\assetpackagist\commands\AbstractPackageCommand;
use Yii;
use yii\base\Event;
use yii\base\Module;
use yii\console\Controller;
use yii\helpers\Console;
use zhuravljov\yii\queue\ErrorEvent;
use zhuravljov\yii\queue\JobEvent;
use zhuravljov\yii\queue\Queue;
use yii\queue\cli\Signal;
use yii\queue\ErrorEvent;
use yii\queue\JobEvent;
use yii\queue\Queue;

/**
* Manages service Queue.
*/
class QueueController extends Controller
{
/**
* @var Queue|\yii\queue\db\Queue
*/
private $queue;

/**
* How many jobs will be executed without process restart
*/
const MAX_EXECUTED_JOBS = 100;

/**
* @var int
*/
private $executedJobsCount = 0;

public function __construct($id, Module $module, Queue $queue, array $config = [])
{
parent::__construct($id, $module, $config);
$this->queue = $queue;
}

public function init()
{
$this->attachEventHandlers();
Expand All @@ -34,7 +57,7 @@ public function init()
*/
public function actionRun()
{
Yii::$app->queue->run();
$this->queue->run();
}

/**
Expand All @@ -46,6 +69,15 @@ public function actionTestErrorHandler()
sleep(1);
}


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();
}
}

/**
* Attaches handlers on Queue events.
*/
Expand All @@ -58,14 +90,15 @@ private function attachEventHandlers()
Event::on(Queue::class, Queue::EVENT_BEFORE_EXEC, function ($event) use ($out) {
/** @var JobEvent $event */
$out("%GNew job%n '" . get_class($event->job) . "'\n");
$this->ensureLimits();
});

Event::on(Queue::class, Queue::EVENT_AFTER_EXEC, function ($event) use ($out) {
/** @var JobEvent $event */
$out("%GJob%n '" . get_class($event->job) . "' %Gis completed%n\n");
});

Event::on(Queue::class, Queue::EVENT_AFTER_EXEC_ERROR, function ($event) use ($out) {
Event::on(Queue::class, Queue::EVENT_AFTER_ERROR, function ($event) use ($out) {
/** @var ErrorEvent $event */
$out("%RJob '" . get_class($event->job) . "' finished with error:%n '" . $event->error . "'\n");
});
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/m170307_170300_queue_timeout.php
Expand Up @@ -10,7 +10,7 @@

namespace hiqdev\assetpackagist\migrations;

use zhuravljov\yii\queue\db\migrations\M170307170300Later;
use yii\queue\db\migrations\M170307170300Later;

/**
* Class m170307_170300_queue_timeout.
Expand Down

0 comments on commit 9f54f52

Please sign in to comment.