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

cron: add a list command which lists all cron jobs

This shows a simple list of jobs, their class, name,
interval, and when they are going to run next.
parent e362b615
Branches
Tags
No related merge requests found
<?php
declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\Cron;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
final class CronListCommand extends Command implements LoggerAwareInterface
{
use LoggerAwareTrait;
// dbp:cron only for backwards compat
protected static $defaultName = 'dbp:relay:core:cron:list';
/**
* @var CronManager
*/
private $manager;
public function __construct(CronManager $manager)
{
parent::__construct();
$this->logger = new NullLogger();
$this->manager = $manager;
}
protected function configure()
{
$this->setDescription('Lists all registered cron jobs');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$currentTime = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
$jobs = $this->manager->getAllJobs();
foreach ($jobs as $job) {
$output->writeln('<fg=green;options=bold>['.get_class($job).']</>');
$output->writeln('<fg=blue;options=bold>Name:</> '.$job->getName());
$output->writeln('<fg=blue;options=bold>Cron:</> "'.$job->getInterval().'"');
$output->writeln('<fg=blue;options=bold>Now:</> '.$currentTime->format(\DateTime::ATOM));
$output->writeln('<fg=blue;options=bold>Next:</> '.$this->manager->getNextDate($job, $currentTime)->format(\DateTime::ATOM));
}
return 0;
}
}
......@@ -66,6 +66,19 @@ final class CronManager implements LoggerAwareInterface
return $shouldRun;
}
/**
* Returns the date and time the job is scheduled to run the next time.
*/
public static function getNextDate(CronJobInterface $job, \DateTimeInterface $currentTime): \DateTimeInterface
{
$cronExpression = $job->getInterval();
$cron = new CronExpression($cronExpression);
$nextDate = $cron->getNextRunDate($currentTime, 0, true);
$nextDate->setTimezone(new \DateTimeZone('UTC'));
return \DateTimeImmutable::createFromMutable($nextDate);
}
public function getPreviousRun(\DateTimeInterface $currentTime): ?\DateTimeInterface
{
$cachePool = $this->cachePool;
......
......@@ -14,6 +14,10 @@ services:
autowire: true
autoconfigure: true
Dbp\Relay\CoreBundle\Cron\CronListCommand:
autowire: true
autoconfigure: true
Dbp\Relay\CoreBundle\Cron\CronManager:
autowire: true
autoconfigure: true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment