Source of file AbstractRecordCachePurgeJob.php
Size: 4,446 Bytes - Last Modified: 2021-12-23T10:07:25+00:00
/var/www/docs.ssmods.com/process/src/src/Jobs/AbstractRecordCachePurgeJob.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 | <?php namespace NSWDPC\Utilities\Cloudflare; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\DataObject; use Symbiote\Cloudflare\CloudflareResult; use Symbiote\Cloudflare\Cloudflare; use Symbiote\QueuedJobs\Services\QueuedJobService; use Symbiote\QueuedJobs\Services\AbstractQueuedJob; use Symbiote\QueuedJobs\Services\QueuedJob; use DateTime; use DateTimeZone; use Exception; /** * Abstract record cache purge job * @author James Ellis <james.ellis@dpc.nsw.gov.au> */ abstract class AbstractRecordCachePurgeJob extends AbstractQueuedJob implements QueuedJob { protected $totalSteps = 1; const RECORD_NAME = 'PurgeRecord'; public function __construct($reason = null, DataObject $object = null) { if($reason) { $this->reason = $reason; } if($object) { $this->setObject($object, self::RECORD_NAME); } } /** * Opportunity to add some logging here */ public function addMessage($msg, $level = null) { return parent::addMessage($msg, $level); } public function getPurgeClient() { return Injector::inst()->get(Cloudflare::CLOUDFLARE_CLASS); } public function getTitle() { $object = $this->getObject(self::RECORD_NAME); if($object) { $type = $object->singular_name(); $title = "{$type}#{$object->ID}/{$object->Title}"; return $title; } return ""; } /** * Checks the provided record for existence and whether it can return values for the required purge type * @return array the values that shalle be purged * @param string $type the purge type e.g 'hosts' */ final protected function checkRecordForErrors($type) { $record = $this->getObject(self::RECORD_NAME); if(!$record) { throw new \Exception("Record not found"); } $values = $record->getPurgeValues(); if(empty($values[ $type ]) || !is_array($values[ $type ])) { throw new \Exception("Record has no '{$type}' values to purge"); } else { foreach($values[$type] as $k => $value) { $this->addMessage("Purging '{$value}' of type '{$type}'..."); } } return $values; } /** * Checks the result of the purge, if not an error the job is marked as complete * @return true * @throws \Exception */ final protected function checkPurgeResult($result) { if(!$result || !$result instanceof CloudflareResult) { throw new \Exception("Result is not a CloudflareResult instance"); } $errors = $result->getErrors(); if(!empty($errors)) { foreach($errors as $e => $file) { $this->addMessage('Error: ' . $file); } throw new \Exception("Purge had errors:" . json_encode($errors)); } $this->addMessage('Job completed without errors'); $successes = $result->getSuccesses(); if(!empty($successes)) { foreach($successes as $s => $file) { $this->addMessage('Success: ' . $file); } } else { $this->addMessage('Success: no success records found'); } $this->currentStep++; $this->isComplete = true; return true; } /** * If the record has a cache max-age... * Purge on that schedule by creating a job of the same type set to run then */ public function afterComplete() { $record = $this->getObject(self::RECORD_NAME); if(!$record) { // record no longer exists $this->addMessage("Cloudflare: record in job no longer exists or could not be found"); return; } if($record->CacheMaxAge && $record->CacheMaxAge > 0) { $next = new DateTime(); $next->modify('+' . $record->CacheMaxAge . ' seconds'); $next_formatted = $next->format('Y-m-d H:i:s'); $job = Injector::inst()->createWithArgs( get_class($this), [ $this->reason, $record ] ); $this->addMessage("Cloudflare: requeuing job for {$next_formatted}"); QueuedJobService::singleton()->queueJob($job, $next_formatted); } else { $this->addMessage("Cloudflare: record has no cache max-age, not recreating"); } } } |