Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-31967: Added logging to ezplatform:cron:run command #14

Merged
merged 10 commits into from
Oct 28, 2020
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `cronjob` to your configuration.

### Example
```yml
monolog:
channels: [...,'cronjob']
handlers:
cronjob:
bubble: false
level: info
type: stream
path: '%kernel.logs_dir%/cronjob.log'
channels: [cronjob]
```
44 changes: 42 additions & 2 deletions src/bundle/Command/CronRunCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformCronBundle\Command;
Expand All @@ -13,9 +13,24 @@
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
{
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;

/**
* @injectService $logger @logger
*/
alongosz marked this conversation as resolved.
Show resolved Hide resolved
public function __construct(LoggerInterface $logger = null)
alongosz marked this conversation as resolved.
Show resolved Hide resolved
{
parent::__construct();
$this->logger = $logger;
}

protected function configure()
{
$this
Expand Down Expand Up @@ -46,6 +61,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
$cron->setExecutor(new Executor());
$cron->setResolver($resolver);

$cron->run();
$reports = $cron->run();

while ($cron->isRunning()) {
usleep(10000);
}

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);
}
}
}
}
}
}
}
17 changes: 15 additions & 2 deletions src/bundle/DependencyInjection/EzPlatformCronExtension.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformCronBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Yaml\Yaml;

/**
* Loads and manages bundle configuration.
*/
class EzPlatformCronExtension extends Extension
class EzPlatformCronExtension extends Extension implements PrependExtensionInterface
{
/**
* {@inheritdoc}
Expand All @@ -24,4 +26,15 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->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);
}
}
1 change: 1 addition & 0 deletions src/bundle/Resources/config/monolog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
channels: ['cronjob']
alongosz marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
parameters:
ezplatform.cron.registry.cronjobs.class: EzSystems\EzPlatformCronBundle\Registry\CronJobsRegistry
ezplatform.console.command.run.class: EzSystems\EzPlatformCronBundle\Command\CronRunCommand
alongosz marked this conversation as resolved.
Show resolved Hide resolved

services:
ezplatform.cron.registry.cronjobs:
class: '%ezplatform.cron.registry.cronjobs.class%'
arguments:
- '%kernel.environment%'
- '@ezpublish.siteaccess'

ezplatform.console.command.run:
class: '%ezplatform.console.command.run.class%'
alongosz marked this conversation as resolved.
Show resolved Hide resolved
arguments:
$logger: "@monolog.logger.cronjob"
alongosz marked this conversation as resolved.
Show resolved Hide resolved
tags:
- { name: console.command }