public static function handleEvent(DeferredQueueCompleteEvent $event) { $queue = $event->queue; if ($queue->status == DeferredQueue::STATUS_SUCCESS_AND_NEXT && $queue->next_task_id != 0) { DeferredHelper::runImmediateTask($queue->next_task_id); } }
/** * Runs separated ReportingTask * * @param $command * @param $groupName * @return array * @throws ServerErrorHttpException */ private static function runTask($command, $groupName) { $task = ExtensionDataHelper::buildTask($command, $groupName); if ($task->registerTask()) { DeferredHelper::runImmediateTask($task->model()->id); Yii::$app->response->format = Response::FORMAT_JSON; return ['queueItemId' => $task->model()->id]; } else { throw new ServerErrorHttpException("Unable to start task"); } }
/** * Activates/Deactivates Extension * * @param $packageName * @param integer $state * @param integer $runMigrations * @return bool */ private function process($packageName, $state = 1, $runMigrations = 1) { $actionText = 0 === (int) $state ? 'deactivated' : 'activated'; if (true === isset($this->extensions[$packageName]['is_active'])) { if ($runMigrations === 1) { $chain = new ReportingChain(); ExtensionDataHelper::prepareMigrationTask($this->extensions[$packageName], $chain, (int) $state ? ExtensionsManager::MIGRATE_TYPE_UP : ExtensionsManager::MIGRATE_TYPE_DOWN, ExtensionsManager::EXTENSION_DEACTIVATE_DEFERRED_GROUP); if (null !== ($firstTaskId = $chain->registerChain())) { DeferredHelper::runImmediateTask($firstTaskId); } else { $this->stderr("Unable to run task chain\n"); } } $this->extensions[$packageName]['is_active'] = $state; if (true === $this->writeConfig()) { $this->stdout(str_replace('{actionText}', $actionText, 'Extension successfully {actionText}.') . PHP_EOL); //this means successfully termination. Because process starts in command line shell return 0; } } else { $this->stdout(str_replace('{actionText}', $actionText, 'You trying to {actionText} not existing extension!') . PHP_EOL); } return 1; }
public function testQueueCompleteEventHandler() { $files = ['task401', 'task402']; $testChain = new ReportingChain(); foreach ($files as $f) { if (file_exists("/tmp/{$f}")) { unlink("/tmp/{$f}"); } $testTask = new ReportingTask(); $testTask->cliCommand('touch', ["/tmp/{$f}"]); $testChain->addTask($testTask); } $firstTaskId = $testChain->registerChain(); DeferredHelper::runImmediateTask($firstTaskId); sleep(2); $this->assertTrue(file_exists('/tmp/task401')); /** @var DeferredQueue $queue */ $queue = DeferredQueue::findOne(['id' => $firstTaskId]); $process = new Process('pwd > /dev/null'); $process->run(); $queue->setProcess($process); $event = new DeferredQueueCompleteEvent($queue); QueueCompleteEventHandler::handleEvent($event); sleep(2); $this->assertTrue(file_exists('/tmp/task402')); }
/** * Builds ReportingTask and places it into certain group. Also if group is not exists yet, it will be created * with necessary parameters, such as group_notifications=0. * * @param array $command * @param string $groupName * @return ReportingTask */ public static function buildTask($command, $groupName) { $groupConfig = ['email_notification' => 0, 'allow_parallel_run' => 0, 'group_notifications' => 0, 'run_last_command_only' => 0]; if (null === ($group = DeferredGroup::findOne(['name' => $groupName]))) { $group = new DeferredGroup(); $group->loadDefaultValues(); $group->setAttributes($groupConfig); $group->name = $groupName; $group->save(); } if ((int) $group->group_notifications !== 0) { // otherwise DeferredController 'deferred-queue-complete' event will not trigger // and we'll unable to write config $group->setAttributes($groupConfig); $group->save(array_keys($groupConfig)); } $task = new ReportingTask(); $task->model()->deferred_group_id = $group->id; $task->cliCommand(DeferredHelper::getPhpBinary(), $command); return $task; }