diff options
Diffstat (limited to 'Digitigrade/Job.php')
| -rw-r--r-- | Digitigrade/Job.php | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Digitigrade/Job.php b/Digitigrade/Job.php new file mode 100644 index 0000000..4fc799a --- /dev/null +++ b/Digitigrade/Job.php @@ -0,0 +1,39 @@ +<?php +namespace Digitigrade; + +abstract class Job implements \JsonSerializable { + public int $remainingTries; + public int $doneTries = 0; + + public function jsonSerialize(): array { + $array = ['jobSubtype' => static::class]; + foreach ((new \ReflectionObject($this))->getProperties() as $prop) { + assert($prop instanceof \ReflectionProperty); // for intellisense + $value = $prop->getValue($this); + $array[$prop->getName()] = is_object($value) ? json_encode($value) : $value; + } + return $array; + } + + public static final function fromJson(string $data): ?self { + $obj = json_decode($data); + if (!property_exists($obj, 'jobSubtype')) { + return null; + } + return (new \JsonMapper())->map($obj, $obj->jobSubtype); + } + + /** + * Runs the job. Returns nothing on success, or throws some error/exception. + * @return void + */ + public abstract function run(); + + /** + * Submits the job to the global job queue with default options. + * @return void + */ + public function submit() { + JobQueue::getInstance()->submitNormal($this); + } +}
\ No newline at end of file |
