From ba77daedefd005decc5dff38dcbfdce4de3a0e30 Mon Sep 17 00:00:00 2001 From: Holger Marx Date: Tue, 29 Sep 2020 09:32:35 +0200 Subject: [PATCH 01/10] Added logging for run command --- src/bundle/Command/CronRunCommand.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/bundle/Command/CronRunCommand.php b/src/bundle/Command/CronRunCommand.php index f5be35a..39a1d81 100644 --- a/src/bundle/Command/CronRunCommand.php +++ b/src/bundle/Command/CronRunCommand.php @@ -13,6 +13,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Psr\Log\LoggerInterface; class CronRunCommand extends ContainerAwareCommand { @@ -46,6 +47,28 @@ protected function execute(InputInterface $input, OutputInterface $output) $cron->setExecutor(new Executor()); $cron->setResolver($resolver); - $cron->run(); + $reports = $cron->run(); + + while ($cron->isRunning()) {} + + if($this->getContainer()->has('monolog.logger.cronjobs')){ + $logger = $this->getContainer()->get('monolog.logger.cronjobs'); + if($logger){ + foreach ($reports->getReports() as $report) { + $extraInfo = array( + 'command' => $report->getJob()->getProcess()->getCommandLine(), + 'exitCode' => $report->getJob()->getProcess()->getExitCode() + ); + foreach($report->getOutput() as $reportOutput){ + $logger->info($reportOutput, $extraInfo); + } + if(!$report->isSuccessful()){ + foreach($report->getError() as $reportError) { + $logger->error($reportError, $extraInfo); + } + } + } + } + } } } From ef3b9a3db6649e1bc7ad768c0bd4f4623fc9436b Mon Sep 17 00:00:00 2001 From: Holger Marx Date: Tue, 29 Sep 2020 09:38:54 +0200 Subject: [PATCH 02/10] Extending readme --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index fda9412..3d3c1e0 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,19 @@ The tag takes the following arguments: - { name: console.command } - { name: ezplatform.cron.job, schedule: '* * * * *' } ``` + +## Logging run command +If you want to log outputs of commands processed by run command you have to add the monolog channel `cronjobs` to your configuration. + +### Example +```yml + monolog: + channels: [...,'cronjobs'] + handlers: + cronjobs: + bubble: false + level: info + type: stream + path: '%kernel.logs_dir%/cronjobs.log' + channels: [cronjobs] +``` From 4034719400b318b4e277d62a18322338826c8d0a Mon Sep 17 00:00:00 2001 From: Holger Marx Date: Tue, 29 Sep 2020 10:26:31 +0200 Subject: [PATCH 03/10] Optimizing error logging --- src/bundle/Command/CronRunCommand.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bundle/Command/CronRunCommand.php b/src/bundle/Command/CronRunCommand.php index 39a1d81..cb71a63 100644 --- a/src/bundle/Command/CronRunCommand.php +++ b/src/bundle/Command/CronRunCommand.php @@ -63,8 +63,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $logger->info($reportOutput, $extraInfo); } if(!$report->isSuccessful()){ - foreach($report->getError() as $reportError) { - $logger->error($reportError, $extraInfo); + foreach($report->getError() as $reportError){ + if(!empty(trim($reportError))){ + $logger->error(trim($reportError), $extraInfo); + } } } } From bb10ce825ea0bf5102410de884c5b4a9ff2a2e65 Mon Sep 17 00:00:00 2001 From: Holger Marx Date: Tue, 29 Sep 2020 10:50:06 +0200 Subject: [PATCH 04/10] Renaming monolog channel from cronjobs to cronjob --- README.md | 12 ++++++------ src/bundle/Command/CronRunCommand.php | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3d3c1e0..a72687d 100644 --- a/README.md +++ b/README.md @@ -41,17 +41,17 @@ The tag takes the following arguments: ``` ## Logging run command -If you want to log outputs of commands processed by run command you have to add the monolog channel `cronjobs` to your configuration. +If you want to log outputs of commands processed by run command you have to add the monolog channel `cronjob` to your configuration. ### Example ```yml monolog: - channels: [...,'cronjobs'] + channels: [...,'cronjob'] handlers: - cronjobs: + cronjob: bubble: false level: info type: stream - path: '%kernel.logs_dir%/cronjobs.log' - channels: [cronjobs] -``` + path: '%kernel.logs_dir%/cronjob.log' + channels: [cronjob] +``` \ No newline at end of file diff --git a/src/bundle/Command/CronRunCommand.php b/src/bundle/Command/CronRunCommand.php index cb71a63..2ee23de 100644 --- a/src/bundle/Command/CronRunCommand.php +++ b/src/bundle/Command/CronRunCommand.php @@ -51,8 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output) while ($cron->isRunning()) {} - if($this->getContainer()->has('monolog.logger.cronjobs')){ - $logger = $this->getContainer()->get('monolog.logger.cronjobs'); + if($this->getContainer()->has('monolog.logger.cronjob')){ + $logger = $this->getContainer()->get('monolog.logger.cronjob'); if($logger){ foreach ($reports->getReports() as $report) { $extraInfo = array( From 5fc9af204d80ad67ac010270bc6f634e4df72866 Mon Sep 17 00:00:00 2001 From: Holger Marx Date: Tue, 29 Sep 2020 17:45:14 +0200 Subject: [PATCH 05/10] Fixing change requests --- src/bundle/Command/CronRunCommand.php | 48 +++++++++++++++--------- src/bundle/Resources/config/services.yml | 7 ++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/bundle/Command/CronRunCommand.php b/src/bundle/Command/CronRunCommand.php index 2ee23de..b53cbb6 100644 --- a/src/bundle/Command/CronRunCommand.php +++ b/src/bundle/Command/CronRunCommand.php @@ -1,7 +1,7 @@ logger = null !== $cronjobLogger ? $cronjobLogger : new NullLogger(); + } + protected function configure() { $this @@ -49,24 +61,24 @@ protected function execute(InputInterface $input, OutputInterface $output) $reports = $cron->run(); - while ($cron->isRunning()) {} + while ($cron->isRunning()) { + } - if($this->getContainer()->has('monolog.logger.cronjob')){ - $logger = $this->getContainer()->get('monolog.logger.cronjob'); - if($logger){ - foreach ($reports->getReports() as $report) { - $extraInfo = array( - 'command' => $report->getJob()->getProcess()->getCommandLine(), - 'exitCode' => $report->getJob()->getProcess()->getExitCode() - ); - foreach($report->getOutput() as $reportOutput){ - $logger->info($reportOutput, $extraInfo); - } - if(!$report->isSuccessful()){ - foreach($report->getError() as $reportError){ - if(!empty(trim($reportError))){ - $logger->error(trim($reportError), $extraInfo); - } + if ($this->logger) { + foreach ($reports->getReports() as $report) { + $process = $report->getJob()->getProcess(); + $extraInfo = [ + 'command' => $process->getCommandLine(), + 'exitCode' => $process->getExitCode(), + ]; + foreach ($report->getOutput() as $reportOutput) { + $this->logger->info($reportOutput, $extraInfo); + } + if (!$report->isSuccessful()) { + foreach ($report->getError() as $reportError) { + $reportError = trim($reportError); + if (!empty($reportError)) { + $this->logger->error($reportError, $extraInfo); } } } diff --git a/src/bundle/Resources/config/services.yml b/src/bundle/Resources/config/services.yml index bffec5f..896dc99 100644 --- a/src/bundle/Resources/config/services.yml +++ b/src/bundle/Resources/config/services.yml @@ -1,5 +1,6 @@ parameters: ezplatform.cron.registry.cronjobs.class: EzSystems\EzPlatformCronBundle\Registry\CronJobsRegistry + ezplatform.console.command.run.class: EzSystems\EzPlatformCronBundle\Command\CronRunCommand services: ezplatform.cron.registry.cronjobs: @@ -7,3 +8,9 @@ services: arguments: - '%kernel.environment%' - '@ezpublish.siteaccess' + - + ezplatform.console.command.run: + class: '%ezplatform.console.command.run.class%' + tags: + - { name: console.command } + - { name: monolog.logger, channel: cronjob } From 05fac86a98a4073ed77cda010459aec2303ddd7a Mon Sep 17 00:00:00 2001 From: Holger Marx Date: Wed, 30 Sep 2020 11:32:22 +0200 Subject: [PATCH 06/10] EZP-31967: Remove unnecessary char in service.yml, add usleep in while statement --- src/bundle/Command/CronRunCommand.php | 1 + src/bundle/Resources/config/services.yml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bundle/Command/CronRunCommand.php b/src/bundle/Command/CronRunCommand.php index b53cbb6..e117b38 100644 --- a/src/bundle/Command/CronRunCommand.php +++ b/src/bundle/Command/CronRunCommand.php @@ -62,6 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $reports = $cron->run(); while ($cron->isRunning()) { + usleep(10000); } if ($this->logger) { diff --git a/src/bundle/Resources/config/services.yml b/src/bundle/Resources/config/services.yml index 896dc99..44e51a7 100644 --- a/src/bundle/Resources/config/services.yml +++ b/src/bundle/Resources/config/services.yml @@ -8,7 +8,7 @@ services: arguments: - '%kernel.environment%' - '@ezpublish.siteaccess' - - + ezplatform.console.command.run: class: '%ezplatform.console.command.run.class%' tags: From b1de2454738f952c4011d4181dc1976ba76e2d4e Mon Sep 17 00:00:00 2001 From: Holger Marx Date: Wed, 7 Oct 2020 10:25:10 +0200 Subject: [PATCH 07/10] EZP-31967: Inject logger as service argument, added monolog channel cronjob --- src/bundle/Command/CronRunCommand.php | 10 ++++++---- .../EzPlatformCronExtension.php | 17 +++++++++++++++-- src/bundle/Resources/config/monolog.yml | 1 + src/bundle/Resources/config/services.yml | 3 ++- 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 src/bundle/Resources/config/monolog.yml diff --git a/src/bundle/Command/CronRunCommand.php b/src/bundle/Command/CronRunCommand.php index e117b38..d04d137 100644 --- a/src/bundle/Command/CronRunCommand.php +++ b/src/bundle/Command/CronRunCommand.php @@ -14,19 +14,21 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Psr\Log\LoggerInterface; -use Psr\Log\NullLogger; class CronRunCommand extends ContainerAwareCommand { /** * @var \Psr\Log\LoggerInterface */ - private $cronjobLogger; + private $logger; - public function __construct(LoggerInterface $cronjobLogger = null) + /** + * @injectService $logger @logger + */ + public function __construct(LoggerInterface $logger = null) { parent::__construct(); - $this->logger = null !== $cronjobLogger ? $cronjobLogger : new NullLogger(); + $this->logger = $logger; } protected function configure() diff --git a/src/bundle/DependencyInjection/EzPlatformCronExtension.php b/src/bundle/DependencyInjection/EzPlatformCronExtension.php index 856af2f..41f5778 100644 --- a/src/bundle/DependencyInjection/EzPlatformCronExtension.php +++ b/src/bundle/DependencyInjection/EzPlatformCronExtension.php @@ -1,20 +1,22 @@ load('services.yml'); } + + /** + * Allow an extension to prepend the extension configurations. + * + * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container + */ + public function prepend(ContainerBuilder $container) + { + $config = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/monolog.yml')); + $container->prependExtensionConfig('monolog', $config); + } } diff --git a/src/bundle/Resources/config/monolog.yml b/src/bundle/Resources/config/monolog.yml new file mode 100644 index 0000000..8a6e38b --- /dev/null +++ b/src/bundle/Resources/config/monolog.yml @@ -0,0 +1 @@ +channels: ['cronjob'] \ No newline at end of file diff --git a/src/bundle/Resources/config/services.yml b/src/bundle/Resources/config/services.yml index 44e51a7..7c7f8d1 100644 --- a/src/bundle/Resources/config/services.yml +++ b/src/bundle/Resources/config/services.yml @@ -11,6 +11,7 @@ services: ezplatform.console.command.run: class: '%ezplatform.console.command.run.class%' + arguments: + $logger: "@monolog.logger.cronjob" tags: - { name: console.command } - - { name: monolog.logger, channel: cronjob } From 0b39848b21434bbbe33ae25416c16682a4945310 Mon Sep 17 00:00:00 2001 From: Holger Marx Date: Mon, 26 Oct 2020 09:53:47 +0100 Subject: [PATCH 08/10] EZP-31967: Added newline to config file, removed default value for logger variable in constructor --- src/bundle/Command/CronRunCommand.php | 2 +- src/bundle/Resources/config/monolog.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bundle/Command/CronRunCommand.php b/src/bundle/Command/CronRunCommand.php index d04d137..a239622 100644 --- a/src/bundle/Command/CronRunCommand.php +++ b/src/bundle/Command/CronRunCommand.php @@ -25,7 +25,7 @@ class CronRunCommand extends ContainerAwareCommand /** * @injectService $logger @logger */ - public function __construct(LoggerInterface $logger = null) + public function __construct(LoggerInterface $logger) { parent::__construct(); $this->logger = $logger; diff --git a/src/bundle/Resources/config/monolog.yml b/src/bundle/Resources/config/monolog.yml index 8a6e38b..86545e5 100644 --- a/src/bundle/Resources/config/monolog.yml +++ b/src/bundle/Resources/config/monolog.yml @@ -1 +1 @@ -channels: ['cronjob'] \ No newline at end of file +channels: ['cronjob'] From 61ed2ddcdd6450693229a1e12ed527dbaa4cca8e Mon Sep 17 00:00:00 2001 From: Holger Marx Date: Mon, 26 Oct 2020 13:30:24 +0100 Subject: [PATCH 09/10] EZP-31967: Removed class parameter from service.yml --- src/bundle/Resources/config/services.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/bundle/Resources/config/services.yml b/src/bundle/Resources/config/services.yml index 7c7f8d1..6bd212b 100644 --- a/src/bundle/Resources/config/services.yml +++ b/src/bundle/Resources/config/services.yml @@ -1,6 +1,5 @@ parameters: ezplatform.cron.registry.cronjobs.class: EzSystems\EzPlatformCronBundle\Registry\CronJobsRegistry - ezplatform.console.command.run.class: EzSystems\EzPlatformCronBundle\Command\CronRunCommand services: ezplatform.cron.registry.cronjobs: @@ -10,8 +9,8 @@ services: - '@ezpublish.siteaccess' ezplatform.console.command.run: - class: '%ezplatform.console.command.run.class%' + class: EzSystems\EzPlatformCronBundle\Command\CronRunCommand arguments: - $logger: "@monolog.logger.cronjob" + $logger: '@monolog.logger.cronjob' tags: - { name: console.command } From dcb07b97ef0450bb764ce6d677445bd9ffe3538e Mon Sep 17 00:00:00 2001 From: Holger Marx Date: Tue, 27 Oct 2020 14:17:54 +0100 Subject: [PATCH 10/10] EZP-31967: Removed unnecessary annotation --- src/bundle/Command/CronRunCommand.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/bundle/Command/CronRunCommand.php b/src/bundle/Command/CronRunCommand.php index a239622..279ec89 100644 --- a/src/bundle/Command/CronRunCommand.php +++ b/src/bundle/Command/CronRunCommand.php @@ -22,9 +22,6 @@ class CronRunCommand extends ContainerAwareCommand */ private $logger; - /** - * @injectService $logger @logger - */ public function __construct(LoggerInterface $logger) { parent::__construct();