Skip to content

Instantly share code, notes, and snippets.

@boscho87
Created May 23, 2018 14:05
Show Gist options
  • Save boscho87/5b8852bc9ced7e9199a7f593acc85903 to your computer and use it in GitHub Desktop.
Save boscho87/5b8852bc9ced7e9199a7f593acc85903 to your computer and use it in GitHub Desktop.
class TaskRunnerCommand extends ContainerAwareCommand
{
use FlockTrait;
const UUID = 'uuid';
protected function configure()
{
$this->setName('kub:task:runner')
->setDescription('executes a task')
->addArgument(self::UUID, InputArgument::REQUIRED, 'task uuid');
}
/**
* @param InputInterface $input
* @param OutputInterface $stdout
* @return int
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $stdout)
{
ini_set('memory_limit', Option::CLI_MEMORY_LIMIT);
/** @var OutputInterface $stderr */
$stderr = $stdout->getErrorOutput();
$uuid = $input->getArgument(self::UUID);
$returnState = 0;
if ($this->lock($uuid) === false) {
$stderr->writeln('<error>This command is already running in another process.</error>');
return -1;
}
$taskManager = $this->getContainer()->get(TaskManager::class);
if ($task = $taskManager->getNextToExecuteByUuid($uuid)) {
$stdout->writeln('<comment>executing '. $task->getActionLiteral() . ' task ' . $uuid . ' ...</comment>');
try {
$taskManager->execute($task);
$stdout->writeln('<info>succeeded</info>');
} catch (\Throwable $e) {
if ($stdout->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
$this->release();
throw $e;
}
$stderr->writeln('<info>failed</info>');
$returnState = -1;
}
} else {
$stderr->writeln('<error>can not execute task with uuid ' . $uuid . '</error>');
}
$this->release();
return $returnState;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment