Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not clone jobs #222

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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