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 6, 2024
1 parent ea772ee commit 23cc077
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/Task/StaticCacheFullBuildTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@
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 string $title = 'Static Cache Full Build';

/**
* 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 +41,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 +70,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 23cc077

Please sign in to comment.