Skip to content

Commit

Permalink
Merge pull request #222 from Icinga/do-not-clone-jobs
Browse files Browse the repository at this point in the history
Do not clone jobs
  • Loading branch information
yhabteab authored Nov 14, 2023
2 parents 146ad3c + ba5c6e3 commit b6d2c31
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
26 changes: 13 additions & 13 deletions application/clicommands/JobsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,10 +106,12 @@ public function runAction(): void
);

$scheduler->remove($job);

unset($scheduled[$job->getUuid()->toString()]);
}

$newJobs = array_diff_key($jobs, $scheduled);
foreach ($newJobs as $job) {
foreach ($newJobs as $key => $job) {
$job->setParallel($parallel);

/** @var stdClass $config */
Expand All @@ -131,9 +132,9 @@ public function runAction(): void
}

$scheduler->schedule($job, $frequency);
}

$scheduled = $jobs;
$scheduled[$key] = $job;
}

Loop::addTimer(5 * 60, $watchdog);
};
Expand Down Expand Up @@ -162,28 +163,27 @@ protected function fetchSchedules(?string $jobName, ?string $scheduleName): arra
foreach ($jobs as $jobConfig) {
$cidrs = $this->parseCIDRs($jobConfig->cidrs);
$ports = $this->parsePorts($jobConfig->ports);
$job = (new Job($jobConfig->name, $cidrs, $ports, $snimap))
->setId($jobConfig->id)
->setExcludes($this->parseExcludes($jobConfig->exclude_targets));

/** @var Query $schedules */
$schedules = $jobConfig->schedule;
if ($scheduleName) {
$schedules->filter(Filter::equal('name', $scheduleName));
}

$schedules = $schedules->execute();
$hasSchedules = $schedules->hasResult();

/** @var X509Schedule $scheduleModel */
foreach ($schedules as $scheduleModel) {
$schedule = Schedule::fromModel($scheduleModel);
$job = (clone $job)
->setSchedule($schedule)
->setUuid(Uuid::fromBytes($job->getChecksum()));
$job = (new Job($jobConfig->name, $cidrs, $ports, $snimap, Schedule::fromModel($scheduleModel)))
->setId($jobConfig->id)
->setExcludes($this->parseExcludes($jobConfig->exclude_targets));

$jobSchedules[$job->getUuid()->toString()] = $job;
}

if (! isset($jobSchedules[$job->getUuid()->toString()])) {
Logger::info('Skipping job %s because no schedules are configured', $job->getName());
if (! $hasSchedules) {
Logger::info('Skipping job %s because no schedules are configured', $jobConfig->name);
}
}

Expand Down
11 changes: 10 additions & 1 deletion library/X509/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,7 +111,6 @@ public function __construct(string $name, array $cidrs, array $ports, array $sni
}

$this->setName($name);
$this->setUuid(Uuid::fromBytes($this->getChecksum()));
}

/**
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit b6d2c31

Please sign in to comment.