From 1e070a7a0097c74f8ac30678d39267f19c76f9f6 Mon Sep 17 00:00:00 2001 From: winter Date: Thu, 19 Dec 2024 18:01:29 +0000 Subject: process pushes in job + fix token expiry job --- Digitigrade/Job.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'Digitigrade/Job.php') diff --git a/Digitigrade/Job.php b/Digitigrade/Job.php index 146e4b2..d460b7e 100644 --- a/Digitigrade/Job.php +++ b/Digitigrade/Job.php @@ -2,15 +2,28 @@ namespace Digitigrade; abstract class Job implements \JsonSerializable { + private static \JsonMapper $mapper; + public int $remainingTries; public int $doneTries = 0; + public static function __initStatic() { + self::$mapper = new \JsonMapper(); + // to parse DateTime(Immutable)s properly + self::$mapper->bStrictObjectTypeChecking = false; + self::$mapper->classMap[\DateTimeInterface::class] = \DateTimeImmutable::class; + } + 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; + if ($value instanceof \DateTimeInterface) { + $array[$prop->getName()] = $value->format('c'); + } else { + $array[$prop->getName()] = $value; + } } return $array; } @@ -20,7 +33,7 @@ abstract class Job implements \JsonSerializable { if (!property_exists($obj, 'jobSubtype')) { return null; } - return (new \JsonMapper())->map($obj, $obj->jobSubtype); + return self::$mapper->map($obj, $obj->jobSubtype); } /** @@ -36,4 +49,6 @@ abstract class Job implements \JsonSerializable { public function submit() { JobQueue::getInstance()->submitNormal($this); } -} \ No newline at end of file +} + +Job::__initStatic(); \ No newline at end of file -- cgit v1.3