addOption('task', null, InputOption::VALUE_REQUIRED, 'Nur diese Aufgabe, unabhängig vom Zeitplan.')
->addOption('limit', null, InputOption::VALUE_REQUIRED, 'Höchstens so viele Aufgaben je Durchlauf.', '10')
->addOption('budget', null, InputOption::VALUE_REQUIRED, 'Höchstens so viele Sekunden je Durchlauf.', '300')
->addOption('loop', null, InputOption::VALUE_REQUIRED, 'Als Dauerläufer: Sekunden zwischen zwei Durchläufen.')
->addOption('max-time', null, InputOption::VALUE_REQUIRED, 'Der Dauerläufer endet nach so vielen Sekunden.', '3600');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
if (null !== ($name = $input->getOption('task'))) {
$result = $this->scheduler->runNow((string) $name, 'konsole');
if (null === $result) {
$io->error('Diese Aufgabe gibt es nicht oder sie läuft gerade schon. `cms:tasks` zeigt, welche es gibt.');
return Command::FAILURE;
}
$this->report($io, [$result]);
return 'ok' === $result['status'] ? Command::SUCCESS : Command::FAILURE;
}
$limit = max(1, (int) $input->getOption('limit'));
$budget = max(1, (int) $input->getOption('budget'));
$loop = null !== $input->getOption('loop') ? max(5, (int) $input->getOption('loop')) : null;
$deadline = microtime(true) + max(0, (int) $input->getOption('max-time'));
$failed = false;
while (true) {
$results = $this->scheduler->runDue('konsole', $limit, $budget);
$failed = $failed || [] !== array_filter($results, static fn (array $r): bool => 'ok' !== $r['status']);
$this->report($io, $results);
if (null === $loop) {
break;
}
// Ein Dauerläufer, der nie endet, sammelt Speicher und hält eine Datenbankverbindung
// über Tage. Er endet nach `--max-time`; der Prozessverwalter startet ihn neu.
if (microtime(true) + $loop > $deadline) {
$io->writeln(sprintf('Ende nach %d Sekunden – neu starten lassen.', (int) $input->getOption('max-time')));
break;
}
sleep($loop);
}
return $failed ? Command::FAILURE : Command::SUCCESS;
}
/** @param list $results */
private function report(SymfonyStyle $io, array $results): void
{
if ([] === $results) {
$io->writeln(sprintf('%s nichts fällig.', gmdate('H:i:s')));
return;
}
foreach ($results as $result) {
$io->writeln(sprintf(
'%s %s %s (%d ms): %s',
gmdate('H:i:s'),
'ok' === $result['status'] ? 'ok ' : 'fehler',
$result['name'],
$result['ms'],
$result['message'],
));
}
}
}