From 4763bda6c929f311c53be2258fda50b66d7a3c30 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:18:23 +1200 Subject: [PATCH] API Update API to reflect changes to CLI interaction (#201) --- docs/en/basic_configuration.md | 2 +- docs/en/building_the_cache.md | 16 +++--- src/Task/StaticCacheFullBuildTask.php | 77 ++++++++++++--------------- 3 files changed, 43 insertions(+), 52 deletions(-) diff --git a/docs/en/basic_configuration.md b/docs/en/basic_configuration.md index f11b8ed7..f3afaf17 100644 --- a/docs/en/basic_configuration.md +++ b/docs/en/basic_configuration.md @@ -7,7 +7,7 @@ be ready to go. You'll need to configure a cron job or equivalent to process the queue (if you haven't already): ```bash -* * * * * php /path/to/silverstripe/vendor/bin/sake dev/tasks/ProcessJobQueueTask +* * * * * php /path/to/silverstripe/vendor/bin/sake tasks:ProcessJobQueueTask ``` Which will ensure that the `GenerateStaticCacheJob`s are processed quickly. diff --git a/docs/en/building_the_cache.md b/docs/en/building_the_cache.md index 73c47da2..a948effd 100644 --- a/docs/en/building_the_cache.md +++ b/docs/en/building_the_cache.md @@ -4,27 +4,27 @@ There are two main ways the cache is built: ## OnAfterPublish Hook -This calls `urlsToCache()` on a page after it is published. By default, it will cache -only its own URL and the ones of its parents. These URLs are added to a +This calls `urlsToCache()` on a page after it is published. By default, it will cache +only its own URL and the ones of its parents. These URLs are added to a `GenerateStaticCacheJob` to be executed on the next run. -To alter which URLs are sent to be cached, you should override the `urlsToCache()` +To alter which URLs are sent to be cached, you should override the `urlsToCache()` method. ## Full Site Build You can generate cache files for the entire site via the `StaticCacheFullBuildJob`. This can either be queued up from the QueuedJobs interface or via the task -`dev/tasks/SilverStripe-StaticPublishQueue-Task-StaticCacheFullBuildTask`. This task also takes a parameter `?startAfter` -which can delay the execution of the job. The parameter should be in HHMM format, -e.g. to start after 2pm, pass `?startAfter=1400`. If it's already after the proposed +`sake tasks:static-cache-full-build`. This task also takes a parameter `--startAfter` +which can delay the execution of the job. The parameter should be in HHMM format, +e.g. to start after 2pm, pass `--startAfter=1400`. If it's already after the proposed time on the current day, it will push it to the next day. If you want to do a full site build on a cron, you should do so via the task. An example configuration would be: ```bash -# build the cache at 1am every day -0 1 * * * www-data /path/to/sake dev/tasks/SilverStripe-StaticPublishQueue-Task-StaticCacheFullBuildTask +# build the cache at 1am every day +0 1 * * * www-data vendor/bin/sake tasks:static-cache-full-build ``` diff --git a/src/Task/StaticCacheFullBuildTask.php b/src/Task/StaticCacheFullBuildTask.php index 63e1ee42..43cb2815 100644 --- a/src/Task/StaticCacheFullBuildTask.php +++ b/src/Task/StaticCacheFullBuildTask.php @@ -3,30 +3,32 @@ namespace SilverStripe\StaticPublishQueue\Task; use DateTime; -use SilverStripe\Control\Director; -use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\BuildTask; -use SilverStripe\Dev\Deprecation; +use SilverStripe\PolyExecution\PolyOutput; use SilverStripe\ORM\DataList; use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\StaticPublishQueue\Job\StaticCacheFullBuildJob; use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; use Symbiote\QueuedJobs\Services\QueuedJob; use Symbiote\QueuedJobs\Services\QueuedJobService; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; class StaticCacheFullBuildTask extends BuildTask { - protected $title = 'Static Cache Full Build'; + protected static string $commandName = 'static-cache-full-build'; + + protected string $title = 'Static Cache Full Build'; + + protected static string $description = 'Fully build the static cache for the whole site'; /** * Queue up a StaticCacheFullBuildJob * Check for startAfter param and do some sanity checking - * - * @param HTTPRequest $request - * @return bool */ - public function run($request) + protected function execute(InputInterface $input, PolyOutput $output): int { $job = Injector::inst()->create(StaticCacheFullBuildJob::class); $signature = $job->getSignature(); @@ -43,22 +45,20 @@ public function run($request) $existing = DataList::create(QueuedJobDescriptor::class)->filter($filter)->first(); if ($existing && $existing->exists()) { - Deprecation::withSuppressedNotice(function () use ($existing) { - $this->log(sprintf( - 'There is already a %s in the queue, added %s %s', - StaticCacheFullBuildJob::class, - $existing->Created, - $existing->StartAfter ? 'and set to start after ' . $existing->StartAfter : '' - )); - }); - - return false; + $output->writeln(sprintf( + 'There is already a %s in the queue, added %s %s', + StaticCacheFullBuildJob::class, + $existing->Created, + $existing->StartAfter ? 'and set to start after ' . $existing->StartAfter : '' + )); + + return Command::FAILURE; } - if ($request->getVar('startAfter')) { + if ($input->getOption('startAfter')) { $now = DBDatetime::now(); $today = $now->Date(); - $startTime = $request->getVar('startAfter'); + $startTime = $input->getOption('startAfter'); // move to tomorrow if the starttime has passed today if ($now->Time24() > $startTime) { @@ -74,26 +74,20 @@ public function run($request) // sanity check that we are in the next 24 hours - prevents some weird stuff sneaking through if ($startAfter->getTimestamp() > $thisTimeTomorrow || $startAfter->getTimestamp() < $now->getTimestamp()) { - Deprecation::withSuppressedNotice(function () { - $this->log('Invalid startAfter parameter passed. Please ensure the time format is HHmm e.g. 1300'); - }); + $output->writeln('Invalid startAfter parameter passed. Please ensure the time format is HHmm e.g. 1300'); - return false; + return Command::INVALID; } - Deprecation::withSuppressedNotice(function () use ($startAfter, $dayWord) { - $this->log(sprintf( - '%s queued for %s %s.', - StaticCacheFullBuildJob::class, - $startAfter->format('H:m'), - $dayWord - )); - }); + $output->writeln(sprintf( + '%s queued for %s %s.', + StaticCacheFullBuildJob::class, + $startAfter->format('H:m'), + $dayWord + )); } else { $startAfter = null; - Deprecation::withSuppressedNotice(function () { - $this->log(StaticCacheFullBuildJob::class . ' added to the queue for immediate processing'); - }); + $output->writeln(StaticCacheFullBuildJob::class . ' added to the queue for immediate processing'); } $job->setJobData(0, 0, false, new \stdClass(), [ @@ -101,16 +95,13 @@ public function run($request) ]); QueuedJobService::singleton()->queueJob($job, $startAfter ? $startAfter->format('Y-m-d H:i:s') : null); - return true; + return Command::SUCCESS; } - /** - * @deprecated 6.3.0 Will be replaced with new $output parameter in the run() method - */ - protected function log($message) + public function getOptions(): array { - Deprecation::notice('6.3.0', 'Will be replaced with new $output parameter in the run() method'); - $newLine = Director::is_cli() ? PHP_EOL : '
'; - echo $message . $newLine; + return [ + new InputOption('startAfter', null, InputOption::VALUE_REQUIRED, 'Delay execution until this time. Must be in 24hr format e.g. 1300'), + ]; } }