|
|
@@ -5,6 +5,8 @@ namespace App\Commands;
|
|
|
use App\Service\Cron\CronjobInterface;
|
|
|
use App\Service\Cron\UI\ConsoleUI;
|
|
|
use App\Service\ServiceIterator\CronjobIterator;
|
|
|
+use Monolog\Formatter\LineFormatter;
|
|
|
+use Monolog\Handler\RotatingFileHandler;
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
use RuntimeException;
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
@@ -101,6 +103,8 @@ class CronCommand extends Command
|
|
|
{
|
|
|
$this->output = $output;
|
|
|
|
|
|
+ $this->configureLoggerFormatter();
|
|
|
+
|
|
|
/** @var FormatterHelper $formatter */
|
|
|
$formatter = $this->getHelper('formatter');
|
|
|
|
|
|
@@ -135,15 +139,13 @@ class CronCommand extends Command
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $this->logger->info(
|
|
|
- 'CronCommand will execute ' .
|
|
|
- implode(', ', array_map(static function($job) { return $job->name(); }, $jobs)) .
|
|
|
- ($preview ? ' [PREVIEW MODE]' : '')
|
|
|
- );
|
|
|
-
|
|
|
$results = [];
|
|
|
|
|
|
foreach ($jobs as $job) {
|
|
|
+ $this->logger->info(
|
|
|
+ 'CronCommand will execute `' . $job->name() . '`' . ($preview ? ' [PREVIEW MODE]' : '')
|
|
|
+ );
|
|
|
+
|
|
|
$results[] = $this->runJob($job, $preview);
|
|
|
}
|
|
|
|
|
|
@@ -196,24 +198,59 @@ class CronCommand extends Command
|
|
|
$ui = new ConsoleUI($this->output);
|
|
|
$job->setUI($ui);
|
|
|
|
|
|
+ $this->configureLoggerFormatter($job->name());
|
|
|
+
|
|
|
try {
|
|
|
if ($preview) {
|
|
|
$job->preview();
|
|
|
} else {
|
|
|
$job->execute();
|
|
|
}
|
|
|
+
|
|
|
+ $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;
|
|
|
+
|
|
|
+ } 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 {
|
|
|
+ // @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.u'));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Alias for `$this->configureLoggerFormatter(null)`
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ protected function resetLoggerFormatter(): void
|
|
|
+ {
|
|
|
+ $this->configureLoggerFormatter(null);
|
|
|
}
|
|
|
}
|