Skip to content

Commit

Permalink
API Update API to reflect changes to CLI interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 8, 2024
1 parent ea772ee commit 139195d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
14 changes: 7 additions & 7 deletions docs/en/building_the_cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
`dev/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
```

42 changes: 23 additions & 19 deletions src/Task/StaticCacheFullBuildTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -71,33 +74,34 @@ 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'),
$dayWord
));
} 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(), [
'Building static cache for full site',
]);
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 : '<br>';
echo $message . $newLine;
return [
new InputOption('startAfter', null, InputOption::VALUE_REQUIRED, 'Delay execution until this time. Must be in 24hr format e.g. <comment>1300</comment>'),
];
}
}

0 comments on commit 139195d

Please sign in to comment.