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 9c9c58c3..6f725f07 100644 --- a/src/Task/StaticCacheFullBuildTask.php +++ b/src/Task/StaticCacheFullBuildTask.php @@ -3,29 +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\HybridExecution\HybridOutput; 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, HybridOutput $output): int { $job = Injector::inst()->create(StaticCacheFullBuildJob::class); $signature = $job->getSignature(); @@ -42,20 +45,20 @@ public function run($request) $existing = DataList::create(QueuedJobDescriptor::class)->filter($filter)->first(); if ($existing && $existing->exists()) { - $this->log(sprintf( + $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 false; + 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) { @@ -71,12 +74,12 @@ 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()) { - $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; } - $this->log(sprintf( + $output->writeln(sprintf( '%s queued for %s %s.', StaticCacheFullBuildJob::class, $startAfter->format('H:m'), @@ -84,7 +87,7 @@ public function run($request) )); } else { $startAfter = null; - $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(), [ @@ -92,12 +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; } - protected function log($message) + public function getOptions(): array { - $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'), + ]; } }