From 2e8be3c67ff12ca2e6b7a0c3c7fa38440e304f34 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 24 Oct 2019 15:29:25 -0600 Subject: [PATCH] Cron improvements --- CHANGELOG.md | 1 + system/src/Grav/Common/Scheduler/Scheduler.php | 17 +++++++++++++---- .../src/Grav/Console/Cli/SchedulerCommand.php | 9 ++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34a31e92d..3d70b9902 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Added `array_diff()` twig function * Added `template_from_string()` twig function 1. [](#improved) + * Improved `Scheduler` cron command check and more useful CLI information * Improved `Flex Users`: obey blueprints and allow Flex to be used in admin only * Improved `Flex` to support custom site template paths * Changed Twig `{% cache %}` tag to not need unique key, and `lifetime` is now optional diff --git a/system/src/Grav/Common/Scheduler/Scheduler.php b/system/src/Grav/Common/Scheduler/Scheduler.php index 7192fefd1..8adf2b0e2 100644 --- a/system/src/Grav/Common/Scheduler/Scheduler.php +++ b/system/src/Grav/Common/Scheduler/Scheduler.php @@ -244,13 +244,20 @@ class Scheduler */ public function getCronCommand() { - $phpBinaryFinder = new PhpExecutableFinder(); - $php = $phpBinaryFinder->find(); - $command = 'cd ' . str_replace(' ', '\ ', GRAV_ROOT) . ';' . $php . ' bin/grav scheduler'; + $command = $this->getSchedulerCommand(); return "(crontab -l; echo \"* * * * * {$command} 1>> /dev/null 2>&1\") | crontab -"; } + public function getSchedulerCommand($php = null) + { + $phpBinaryFinder = new PhpExecutableFinder(); + $php = $php ?? $phpBinaryFinder->find(); + $command = 'cd ' . str_replace(' ', '\ ', GRAV_ROOT) . ';' . $php . ' bin/grav scheduler'; + + return $command; + } + /** * Helper to determine if cron job is setup * @@ -263,8 +270,10 @@ class Scheduler if ($process->isSuccessful()) { $output = $process->getOutput(); + $command = str_replace('/', '\/', $this->getSchedulerCommand('.*')); + $full_command = '/^(?!#).* .* .* .* .* ' . $command . '/m'; - return preg_match('$bin\/grav schedule$', $output) ? 1 : 0; + return preg_match($full_command, $output) ? 1 : 0; } $error = $process->getErrorOutput(); diff --git a/system/src/Grav/Console/Cli/SchedulerCommand.php b/system/src/Grav/Console/Cli/SchedulerCommand.php index ac237905e..96af69f29 100644 --- a/system/src/Grav/Console/Cli/SchedulerCommand.php +++ b/system/src/Grav/Console/Cli/SchedulerCommand.php @@ -152,17 +152,20 @@ class SchedulerCommand extends ConsoleCommand } elseif ($this->input->getOption('install')) { $io->title('Install Scheduler'); + $verb = 'install'; + if ($scheduler->isCrontabSetup()) { - $io->success('All Ready! You have already set up Grav\'s Scheduler in your crontab'); + $io->success('All Ready! You have already set up Grav\'s Scheduler in your crontab. You can validate this by running "crontab -l" to list your current crontab entries.'); + $verb = 'reinstall'; } else { $io->error('You still need to set up Grav\'s Scheduler in your crontab'); } if (!Utils::isWindows()) { - $io->note('To install, run the following command from your terminal:'); + $io->note("To $verb, run the following command from your terminal:"); $io->newLine(); $io->text(trim($scheduler->getCronCommand())); } else { - $io->note('To install, create a scheduled task in Windows.'); + $io->note("To $verb, create a scheduled task in Windows."); $io->text('Learn more at https://learn.getgrav.org/advanced/scheduler'); } } else {