|
@@ -5,6 +5,8 @@ namespace App\Commands;
|
|
|
use App\Service\Cron\CronjobInterface;
|
|
use App\Service\Cron\CronjobInterface;
|
|
|
use App\Service\Cron\UI\ConsoleUI;
|
|
use App\Service\Cron\UI\ConsoleUI;
|
|
|
use App\Service\ServiceIterator\CronjobIterator;
|
|
use App\Service\ServiceIterator\CronjobIterator;
|
|
|
|
|
+use Monolog\Formatter\LineFormatter;
|
|
|
|
|
+use Monolog\Handler\RotatingFileHandler;
|
|
|
use Psr\Log\LoggerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Command\Command;
|
|
@@ -15,6 +17,7 @@ use Symfony\Component\Console\Input\InputInterface;
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
|
+use Throwable;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* CLI Command to run the cron-jobs.
|
|
* CLI Command to run the cron-jobs.
|
|
@@ -25,7 +28,7 @@ use Symfony\Contracts\Service\Attribute\Required;
|
|
|
* bin/console ot:cron run clean-db --preview
|
|
* bin/console ot:cron run clean-db --preview
|
|
|
* bin/console ot:cron run clean-db
|
|
* bin/console ot:cron run clean-db
|
|
|
*
|
|
*
|
|
|
- * @see ~/src/Service/Cron/Readme.md
|
|
|
|
|
|
|
+ * @see doc/cron.md
|
|
|
*/
|
|
*/
|
|
|
#[AsCommand(
|
|
#[AsCommand(
|
|
|
name: 'ot:cron',
|
|
name: 'ot:cron',
|
|
@@ -49,13 +52,11 @@ class CronCommand extends Command
|
|
|
private LoggerInterface $logger;
|
|
private LoggerInterface $logger;
|
|
|
private CronjobIterator $cronjobIterator;
|
|
private CronjobIterator $cronjobIterator;
|
|
|
|
|
|
|
|
|
|
+ /** @noinspection PhpUnused */
|
|
|
#[Required]
|
|
#[Required]
|
|
|
/** @see https://symfony.com/doc/current/logging/channels_handlers.html#how-to-autowire-logger-channels */
|
|
/** @see https://symfony.com/doc/current/logging/channels_handlers.html#how-to-autowire-logger-channels */
|
|
|
- public function setLoggerInterface(LoggerInterface $cronLogger): void
|
|
|
|
|
- {
|
|
|
|
|
- $this->logger = $cronLogger;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ public function setLoggerInterface(LoggerInterface $cronLogger): void { $this->logger = $cronLogger; }
|
|
|
|
|
+ /** @noinspection PhpUnused */
|
|
|
#[Required]
|
|
#[Required]
|
|
|
public function setCronjobIterator(CronjobIterator $cronjobIterator): void
|
|
public function setCronjobIterator(CronjobIterator $cronjobIterator): void
|
|
|
{
|
|
{
|
|
@@ -100,6 +101,8 @@ class CronCommand extends Command
|
|
|
{
|
|
{
|
|
|
$this->output = $output;
|
|
$this->output = $output;
|
|
|
|
|
|
|
|
|
|
+ $this->configureLoggerFormatter();
|
|
|
|
|
+
|
|
|
/** @var FormatterHelper $formatter */
|
|
/** @var FormatterHelper $formatter */
|
|
|
$formatter = $this->getHelper('formatter');
|
|
$formatter = $this->getHelper('formatter');
|
|
|
|
|
|
|
@@ -128,7 +131,7 @@ class CronCommand extends Command
|
|
|
foreach (explode(',', $jobNames) as $name) {
|
|
foreach (explode(',', $jobNames) as $name) {
|
|
|
try {
|
|
try {
|
|
|
$jobs[] = $this->cronjobIterator->getByName($name);
|
|
$jobs[] = $this->cronjobIterator->getByName($name);
|
|
|
- } catch (\RuntimeException $e) {
|
|
|
|
|
|
|
+ } catch (RuntimeException $e) {
|
|
|
$this->output->writeln($e->getMessage());
|
|
$this->output->writeln($e->getMessage());
|
|
|
$this->listJobs();
|
|
$this->listJobs();
|
|
|
|
|
|
|
@@ -137,15 +140,13 @@ class CronCommand extends Command
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $this->logger->info(
|
|
|
|
|
- 'CronCommand will '.
|
|
|
|
|
- ($preview ? 'preview' : 'execute').' '.
|
|
|
|
|
- implode(', ', array_map(static function ($job) { return $job->name(); }, $jobs))
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
$results = [];
|
|
$results = [];
|
|
|
|
|
|
|
|
foreach ($jobs as $job) {
|
|
foreach ($jobs as $job) {
|
|
|
|
|
+ $this->logger->info(
|
|
|
|
|
+ 'CronCommand will execute `' . $job->name() . '`' . ($preview ? ' [PREVIEW MODE]' : '')
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
$results[] = $this->runJob($job, $preview);
|
|
$results[] = $this->runJob($job, $preview);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -197,25 +198,59 @@ class CronCommand extends Command
|
|
|
$ui = new ConsoleUI($this->output);
|
|
$ui = new ConsoleUI($this->output);
|
|
|
$job->setUI($ui);
|
|
$job->setUI($ui);
|
|
|
|
|
|
|
|
|
|
+ $this->configureLoggerFormatter($job->name());
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
if ($preview) {
|
|
if ($preview) {
|
|
|
$job->preview();
|
|
$job->preview();
|
|
|
} else {
|
|
} else {
|
|
|
$job->execute();
|
|
$job->execute();
|
|
|
}
|
|
}
|
|
|
- } catch (\RuntimeException $e) {
|
|
|
|
|
- $this->logger->critical($e);
|
|
|
|
|
- $this->output->write('An error happened while running the process : '.$e);
|
|
|
|
|
|
|
|
|
|
|
|
+ $t1 = microtime(true);
|
|
|
|
|
+
|
|
|
|
|
+ $msg = "Job has been successfully executed (" . round($t1 - $t0, 2) . " sec.)" . ($preview ? ' [PREVIEW MODE]' : '');
|
|
|
|
|
+ $this->output->writeln($formatter->formatSection($job->name(), $msg));
|
|
|
|
|
+ $this->logger->info($job->name() . ' - ' . $msg);
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Throwable $e) {
|
|
|
|
|
+ $this->logger->critical($e);
|
|
|
|
|
+ $this->output->write("An error happened while running the process : " . $e);
|
|
|
return Command::FAILURE;
|
|
return Command::FAILURE;
|
|
|
|
|
+
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ $this->resetLoggerFormatter();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $t1 = microtime(true);
|
|
|
|
|
|
|
+ return Command::SUCCESS;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- $msg = 'Job has been successfully executed ('.round($t1 - $t0, 2).' sec.)'.($preview ? ' [PREVIEW MODE]' : '');
|
|
|
|
|
- $this->output->writeln($formatter->formatSection($job->name(), $msg));
|
|
|
|
|
- $this->logger->info($job->name().' - '.$msg);
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Modify the RotatingFile logger line format to match the display the current job's name (if any)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string|null $jobName
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function configureLoggerFormatter(string | null $jobName = null): void {
|
|
|
|
|
+ /** @noinspection PhpPossiblePolymorphicInvocationInspection @phpstan-ignore-next-line */
|
|
|
|
|
+ foreach ($this->logger->getHandlers() as $handler) {
|
|
|
|
|
+ if ($handler instanceof RotatingFileHandler) {
|
|
|
|
|
|
|
|
- return Command::SUCCESS;
|
|
|
|
|
|
|
+ $format = "[%datetime%] " .
|
|
|
|
|
+ ($jobName !== null ? "[" . $jobName . "] " : "") .
|
|
|
|
|
+ "%channel%.%level_name%: %message% %context% %extra%\n";
|
|
|
|
|
+
|
|
|
|
|
+ $handler->setFormatter(new LineFormatter($format, 'Y-m-d H:i:s.v'));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Alias for `$this->configureLoggerFormatter(null)`
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function resetLoggerFormatter(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->configureLoggerFormatter();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|