From ba5c6e3594fce9e40f1871dc92f222011b2a21b3 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Tue, 14 Nov 2023 10:17:24 +0100 Subject: [PATCH] Lazily initialize `Job` uuid --- application/clicommands/JobsCommand.php | 6 ------ library/X509/Job.php | 11 ++++++++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/application/clicommands/JobsCommand.php b/application/clicommands/JobsCommand.php index 5945786a..ab3a47bb 100644 --- a/application/clicommands/JobsCommand.php +++ b/application/clicommands/JobsCommand.php @@ -22,7 +22,6 @@ use ipl\Scheduler\Contract\Frequency; use ipl\Scheduler\Scheduler; use ipl\Stdlib\Filter; -use Ramsey\Uuid\Uuid; use React\EventLoop\Loop; use React\Promise\ExtendedPromiseInterface; use stdClass; @@ -180,11 +179,6 @@ protected function fetchSchedules(?string $jobName, ?string $scheduleName): arra ->setId($jobConfig->id) ->setExcludes($this->parseExcludes($jobConfig->exclude_targets)); - // The Job class sets the uuid in its constructor, but since the excluded targets are also hashed as - // part of the job's uuid and the excluded targets are set after the job construction, we have to - // reset the uuid afterwards. Otherwise, it won't notice when updating the "exclude_targets" column. - $job->setUuid(Uuid::fromBytes($job->getChecksum())); - $jobSchedules[$job->getUuid()->toString()] = $job; } diff --git a/library/X509/Job.php b/library/X509/Job.php index 1a0f0f4a..ffc0306a 100644 --- a/library/X509/Job.php +++ b/library/X509/Job.php @@ -24,6 +24,7 @@ use ipl\Stdlib\Filter; use LogicException; use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\UuidInterface; use React\EventLoop\Loop; use React\Promise; use React\Socket\ConnectionInterface; @@ -110,7 +111,6 @@ public function __construct(string $name, array $cidrs, array $ports, array $sni } $this->setName($name); - $this->setUuid(Uuid::fromBytes($this->getChecksum())); } /** @@ -207,6 +207,15 @@ public function setId(int $id): self return $this; } + public function getUuid(): UuidInterface + { + if (! $this->uuid) { + $this->setUuid(Uuid::fromBytes($this->getChecksum())); + } + + return $this->uuid; + } + /** * Get the configured job CIDRS *