aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Job.php
diff options
context:
space:
mode:
authorwinter2024-12-14 16:26:30 +0000
committerwinter2024-12-14 16:26:30 +0000
commit4dc9d1a481417bb78606996fa75c791bb646f44f (patch)
tree1e5c1beff234999a0a0eb9819515644ec73d965d /Digitigrade/Job.php
parent86cf52e8d2701e13bd9fd4c847b35eba7506c473 (diff)
job queue and workers and loggers!
Diffstat (limited to 'Digitigrade/Job.php')
-rw-r--r--Digitigrade/Job.php39
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