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 23, 2024
1 parent 7289ce6 commit 30375ea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 52 deletions.
2 changes: 1 addition & 1 deletion docs/en/basic_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 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,
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
```

77 changes: 34 additions & 43 deletions src/Task/StaticCacheFullBuildTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -43,22 +45,20 @@ public function run($request)
$existing = DataList::create(QueuedJobDescriptor::class)->filter($filter)->first();

if ($existing && $existing->exists()) {
Deprecation::withNoReplacement(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) {
Expand All @@ -74,43 +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()) {
Deprecation::withNoReplacement(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::withNoReplacement(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::withNoReplacement(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(), [
'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;
}

/**
* @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 : '<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 30375ea

Please sign in to comment.