/**
* @param InputInterface $input The input to inject into the command.
* @param OutputInterface $output The output to inject into the command.
*
* @return integer The command exit code.
*
* @since 4.1.0
* @throws \RunTimeException
* @throws InvalidArgumentException
*/
protected function doExecute(InputInterface $input, OutputInterface $output) : int
{
/**
* Not as a class constant because of some the autoload order doesn't let us
* load the namespace when it's time to do that (why?)
*/
static $outTextMap = [Status::OK => 'Task#%1$02d \'%2$s\' processed in %3$.2f seconds.', Status::WILL_RESUME => '<notice>Task#%1$02d \'%2$s\' ran for %3$.2f seconds, will resume next time.</notice>', Status::NO_RUN => '<warning>Task#%1$02d \'%2$s\' failed to run. Is it already running?</warning>', Status::NO_ROUTINE => '<error>Task#%1$02d \'%2$s\' is orphaned! Visit the backend to resolve.</error>', 'N/A' => '<error>Task#%1$02d \'%2$s\' exited with code %4$d in %3$.2f seconds.</error>'];
$this->configureIo($input, $output);
$this->ioStyle->title('Run tasks');
$scheduler = new Scheduler();
$id = $input->getOption('id');
$all = $input->getOption('all');
if ($id) {
$records[] = $scheduler->fetchTaskRecord($id);
} else {
$filters = $scheduler::TASK_QUEUE_FILTERS;
$listConfig = $scheduler::TASK_QUEUE_LIST_CONFIG;
$listConfig['limit'] = $all ? null : 1;
$records = $scheduler->fetchTaskRecords($filters, $listConfig);
}
if ($id && !$records[0]) {
$this->ioStyle->writeln('<error>No matching task found!</error>');
return Status::NO_TASK;
} elseif (!$records) {
$this->ioStyle->writeln('<error>No tasks due!</error>');
return Status::NO_TASK;
}
$status = ['startTime' => microtime(true)];
$taskCount = \count($records);
$exit = Status::OK;
foreach ($records as $record) {
$cStart = microtime(true);
$task = $scheduler->runTask(['id' => $record->id, 'allowDisabled' => true, 'allowConcurrent' => true]);
$exit = empty($task) ? Status::NO_RUN : $task->getContent()['status'];
$duration = microtime(true) - $cStart;
$key = \array_key_exists($exit, $outTextMap) ? $exit : 'N/A';
$this->ioStyle->writeln(sprintf($outTextMap[$key], $record->id, $record->title, $duration, $exit));
}
$netTime = round(microtime(true) - $status['startTime'], 2);
$this->ioStyle->newLine();
$this->ioStyle->writeln("<info>Finished running {$taskCount} tasks in {$netTime} seconds.</info>");
return $taskCount === 1 ? $exit : Status::OK;
}