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