Skip to content

Commit 85c5d58

Browse files
authored
Improved cron command logging (#637)
1 parent 68224f5 commit 85c5d58

File tree

1 file changed

+53
-42
lines changed

1 file changed

+53
-42
lines changed

src/Goteo/Console/Command/CronCommand.php

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,27 @@
2424
use Symfony\Component\Process\PhpExecutableFinder;
2525
use Symfony\Component\Yaml\Yaml;
2626

27-
class CronCommand extends AbstractCommand {
27+
class CronCommand extends AbstractCommand
28+
{
2829
protected static $resolver = null;
2930
protected static $crontabLines = [];
3031

31-
public static function setResolver(ResolverInterface $resolver) {
32+
public static function setResolver(ResolverInterface $resolver)
33+
{
3234
static::$resolver = $resolver;
3335
}
3436

35-
public static function getResolver() {
37+
public static function getResolver()
38+
{
3639
// Initialize resolver if null
37-
if(!static::$resolver instanceOf ResolverInterface) {
40+
if (!static::$resolver instanceof ResolverInterface) {
3841
static::setResolver(new ArrayResolver());
3942
}
4043
return static::$resolver;
4144
}
4245

43-
public static function addJob(JobInterface $job) {
46+
public static function addJob(JobInterface $job)
47+
{
4448
return static::getResolver()->addJob($job);
4549
}
4650

@@ -49,7 +53,8 @@ public static function addJob(JobInterface $job) {
4953
* @param [type] $command the shell executable program ()
5054
* @param [type] $schedule the crontab time line (ex: 5 * * * *)
5155
*/
52-
public static function addSchedule($command, $schedule) {
56+
public static function addSchedule($command, $schedule)
57+
{
5358
chdir(GOTEO_PATH);
5459
$job = new ShellJob();
5560
$job->setCommand($command);
@@ -67,12 +72,13 @@ public static function addSchedule($command, $schedule) {
6772
* 'nice' => true|false
6873
* )
6974
*/
70-
public static function addCrontabLine(array $job) {
75+
public static function addCrontabLine(array $job)
76+
{
7177
$executable = $job['command'];
72-
if($job['type'] == 'php') {
78+
if ($job['type'] == 'php') {
7379
$executable = (new PhpExecutableFinder())->find() . " $executable";
7480
}
75-
if($job['nice']) {
81+
if ($job['nice']) {
7682
$executable = "nice $executable";
7783
}
7884
static::$crontabLines[] = [$executable, $job['schedule']];
@@ -88,11 +94,11 @@ public function __construct($name = null)
8894
// Change to Goteo dir
8995
chdir(GOTEO_PATH);
9096
// Initializes crontab from yml resource file
91-
if(is_file(GOTEO_PATH . 'Resources/crontab.yml')) {
97+
if (is_file(GOTEO_PATH . 'Resources/crontab.yml')) {
9298
$crontab = Yaml::parse(file_get_contents(GOTEO_PATH . 'Resources/crontab.yml'));
9399
$env = Config::get('env');
94-
if(isset($crontab[$env])) {
95-
foreach($crontab[$env] as $job) {
100+
if (isset($crontab[$env])) {
101+
foreach ($crontab[$env] as $job) {
96102
static::addCrontabLine($job);
97103
}
98104
}
@@ -102,12 +108,13 @@ public function __construct($name = null)
102108
protected function configure()
103109
{
104110
$this->setName("cron")
105-
->setDescription("The cron program executor")
106-
->setDefinition(array(
107-
new InputOption('crontab', 'c', InputOption::VALUE_NONE, 'List the crontab defined and exit'),
108-
new InputOption('jobs', 'j', InputOption::VALUE_NONE, 'List the jobs that will be executed now and exit'),
109-
))
110-
->setHelp(<<<EOT
111+
->setDescription("The cron program executor")
112+
->setDefinition(array(
113+
new InputOption('crontab', 'c', InputOption::VALUE_NONE, 'List the crontab defined and exit'),
114+
new InputOption('jobs', 'j', InputOption::VALUE_NONE, 'List the jobs that will be executed now and exit'),
115+
))
116+
->setHelp(
117+
<<<EOT
111118
Centralizes the execution of scheduled tasks.
112119
113120
Put in your crontab:
@@ -141,30 +148,30 @@ protected function configure()
141148
+------------------------- min (0 - 59)
142149
</>
143150
EOT
144-
);
151+
);
145152
}
146153

147154
protected function execute(InputInterface $input, OutputInterface $output)
148155
{
149-
if(count(static::$crontabLines) == 0) {
156+
if (count(static::$crontabLines) == 0) {
150157
$output->writeln("<fg=red>No crontab at all!</>");
151158
return;
152159
}
153-
if($input->getOption('crontab')) {
154-
foreach(static::$crontabLines as $line) {
160+
if ($input->getOption('crontab')) {
161+
foreach (static::$crontabLines as $line) {
155162
$output->writeln($line[1] . ' ' . $line[0]);
156163
}
157164
return;
158165
}
159166

160-
if($input->getOption('jobs')) {
167+
if ($input->getOption('jobs')) {
161168
$jobs = static::getResolver()->resolve();
162-
foreach($jobs as $i => $job) {
169+
foreach ($jobs as $i => $job) {
163170
$schedule = $job->getSchedule()->getPattern();
164171
$task = $job->getProcess()->getCommandLine();
165172
$output->writeln("$schedule $task");
166173
}
167-
if($i == 0) {
174+
if ($i == 0) {
168175
$output->writeln("<fg=cyan>No jobs now</>");
169176
}
170177
return;
@@ -175,48 +182,52 @@ protected function execute(InputInterface $input, OutputInterface $output)
175182
$cron->setResolver(static::getResolver());
176183
$cron_report = $cron->run();
177184
$num = count($cron_report->getReports());
185+
186+
$this->info("---CRON COMMAND---");
187+
$this->info(sprintf("Date %s", (new \DateTime())->format(\DateTime::ISO8601_EXPANDED)));
178188
$this->info("Running $num processes", ['processes' => $num]);
189+
179190
// wait
180-
while($cron->isRunning()) {
191+
while ($cron->isRunning()) {
181192
$output->write(".");
182193
usleep(100000);
183194
}
184-
$output->writeln("\n");
185195

186196
$i = 0;
187-
foreach($cron_report->getReports() as $report) {
197+
foreach ($cron_report->getReports() as $report) {
188198
$process = $report->getJob()->getProcess();
189199
$task = $report->getJob()->getProcess()->getCommandLine();
190200
$time = $report->getEndTime() - $report->getStartTime();
191201
$schedule = $report->getJob()->getSchedule()->getPattern();
192-
if($output->isVerbose()) {
193-
$output->writeln('[<fg=cyan>'. $task . '</>] <option=bold;fg=' . ($report->isSuccessful() ? 'green>OK' : 'red>ERROR') . '</>');
202+
203+
if ($output->isVerbose()) {
204+
$output->writeln('[<fg=cyan>' . $task . '</>] <option=bold;fg=' . ($report->isSuccessful() ? 'green>OK' : 'red>ERROR') . '</>');
194205
}
195-
if($report->isSuccessful()) {
206+
207+
if ($report->isSuccessful()) {
196208
$this->info("Completed [$task] " . round($time, 3) . " seconds", ['time' => $time, 'job' => $task, 'schedule' => $schedule]);
197-
if($output->isVerbose()) {
198-
$output->writeln('<fg=blue>'. implode("\n", $report->getOutput()) . '</>');
209+
if ($output->isVerbose()) {
210+
$output->writeln('<fg=blue>' . implode("\n", $report->getOutput()) . '</>');
199211
}
200-
201212
} else {
202213
$errors = [];
203-
foreach($report->getError() as $err) {
214+
foreach ($report->getError() as $err) {
204215
$errors = array_merge($errors, explode("\n", $err));
205216
}
206217
$this->error("Failed [$task] " . round($time, 3) . " seconds", ['time' => $time, 'job' => $task, 'schedule' => $schedule, 'error' => $errors]);
207-
if($output->isVerbose()) {
208-
$output->writeln('<fg=red>'. implode("\n", $report->getError()) . '</>');
218+
if ($output->isVerbose()) {
219+
$output->writeln('<fg=red>' . implode("\n", $report->getError()) . '</>');
209220
}
210221
}
222+
211223
$i++;
212224
}
213-
if($i == 0) {
225+
226+
if ($i == 0) {
214227
$output->writeln("<fg=cyan>No jobs now</>");
215-
}
216-
elseif($cron_report->isSuccessful()) {
228+
} elseif ($cron_report->isSuccessful()) {
217229
$output->writeln("<info>All done</info>");
218-
}
219-
else {
230+
} else {
220231
$output->writeln("<error>Completed with errors</error>");
221232
}
222233
}

0 commit comments

Comments
 (0)