Source of file AbstractJob.php
Size: 3,249 Bytes - Last Modified: 2021-12-23T10:38:27+00:00
/var/www/docs.ssmods.com/process/src/src/Jobs/AbstractJob.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 | <?php namespace StudioBonito\SilverStripe\Queue\Jobs; use DateTime; abstract class AbstractJob { /** * The injector instance. * * @var \Injector */ protected $injector; /** * @var mixed */ protected $instance; /** * @var string */ protected $queue; /** * Indicates if the job has been deleted. * * @var bool */ protected $deleted = false; /** * Run the job. * * @codeCoverageIgnore * * @return void */ abstract public function run(); /** * Delete the job from the queue. * * @return void */ public function delete() { $this->deleted = true; } /** * Determine if the job has been deleted. * * @return bool */ public function isDeleted() { return $this->deleted; } /** * Release the job back into the queue. * * @codeCoverageIgnore * * @param int $delay * * @return void */ abstract public function release($delay = 0); /** * Get the number of times the job has been attempted. * * @codeCoverageIgnore * * @return int */ abstract public function attempts(); /** * Get the raw payload string for the job. * * @codeCoverageIgnore * * @return string */ abstract public function getRawPayload(); /** * Resolve and fire the job handler method. * * @param array $payload * * @return void */ protected function resolveAndRun(array $payload) { list($class, $method) = $this->parseJob($payload['job']); $this->instance = $this->resolve($class); $this->instance->{$method}($this, $payload['data']); } /** * Resolve the given job handler. * * @param string $class * * @return mixed */ protected function resolve($class) { return $this->injector->get($class); } /** * Parse the job declaration into class and method. * * @param string $job * * @return array */ protected function parseJob($job) { $segments = explode('@', $job); return count($segments) > 1 ? $segments : array($segments[0], 'run'); } /** * Determine if job should be auto-deleted. * * @return bool */ public function autoDelete() { return isset($this->instance->delete); } /** * Calculate the number of seconds with the given delay. * * @param \DateTime|int $delay * * @return int */ protected function getSeconds($delay) { if ($delay instanceof DateTime) { return max(0, $delay->getTimestamp() - $this->getTime()); } else { return intval($delay); } } /** * Get the current UNIX timestamp. * * @codeCoverageIgnore * * @return int */ public function getTime() { return time(); } /** * Get the name of the queue the job belongs to. * * @return string */ public function getQueue() { return $this->queue; } } |