Skip to content
Snippets Groups Projects
Commit 436fda8b authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

cron: add a "--force" option to the cron command

This will force run all cron commands even if they aren't due.
This can be helpful in case of development/debugging.

And we want to change the default of cron not running at the
beginning when the state/cache is missing i.e. when the api
is re-deployed. This allows the admin to force a run in case
it is known that the api was offline for some time and cron
jobs are needed.
parent 1bdb01eb
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,7 @@ use Psr\Log\LoggerAwareTrait; ...@@ -10,6 +10,7 @@ use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
final class CronCommand extends Command implements LoggerAwareInterface final class CronCommand extends Command implements LoggerAwareInterface
...@@ -34,19 +35,25 @@ final class CronCommand extends Command implements LoggerAwareInterface ...@@ -34,19 +35,25 @@ final class CronCommand extends Command implements LoggerAwareInterface
protected function configure() protected function configure()
{ {
$this->setDescription('Runs various tasks which need to be executed periodically'); $this->setDescription('Runs various tasks which need to be executed periodically');
$this->addOption('force', null, InputOption::VALUE_NONE, 'Run the cron job even if it\'s not due');
} }
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
// We need to pass the prune command to CachePrune since I didn't find an alternative // We need to pass the prune command to CachePrune since I didn't find an alternative
$app = $this->getApplication(); $app = $this->getApplication();
$force = $input->getOption('force');
assert($app !== null); assert($app !== null);
$command = $app->find('cache:pool:prune'); $command = $app->find('cache:pool:prune');
CachePrune::setPruneCommand($command); CachePrune::setPruneCommand($command);
// Now run all jobs // Now run all jobs
$dueJobs = $this->manager->getDueJobs(); if ($force) {
foreach ($dueJobs as $job) { $jobsToRun = $this->manager->getAllJobs();
} else {
$jobsToRun = $this->manager->getDueJobs();
}
foreach ($jobsToRun as $job) {
$name = $job->getName(); $name = $job->getName();
$this->logger->info("cron: Running '$name'"); $this->logger->info("cron: Running '$name'");
try { try {
......
...@@ -85,6 +85,14 @@ final class CronManager implements LoggerAwareInterface ...@@ -85,6 +85,14 @@ final class CronManager implements LoggerAwareInterface
return $previousRun; return $previousRun;
} }
/**
* @return CronJobInterface[]
*/
public function getAllJobs(): array
{
return $this->jobs;
}
/** /**
* @return CronJobInterface[] * @return CronJobInterface[]
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment