Skip to content

Commit

Permalink
Not schedule job update if already queued
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed Jan 9, 2023
1 parent 0f631cc commit 134dde0
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 26 deletions.
3 changes: 1 addition & 2 deletions config/packages/okvpn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ okvpn_cron:
cron: '0 0 * * *'
-
command: 'packagist:update'
cron: '9,19,29,38,48,58 * * * *' # avoid running things at midnight etc
# cron: '*/1 * * * *'
cron: '*/15 * * * *'
arguments: {'--update-crawl-interval': '%env(int:PACKAGE_UPDATE_INTERVAL)%' }

default_policy:
Expand Down
2 changes: 1 addition & 1 deletion src/Command/UpdatePackagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$idsGroup = array_splice($ids, 0, 100);

foreach ($idsGroup as $id) {
$job = $this->scheduler->scheduleUpdate($id, $updateEqualRefs, $deleteBefore, $randomTimes ? new \DateTime('+'.rand(1, (int) ($interval/1.5)).'seconds') : null);
$job = $this->scheduler->scheduleUpdate($id, $updateEqualRefs, $deleteBefore, $randomTimes ? new \DateTime('+'.rand(1, (int) ($interval/1.5)).'seconds') : null, true);
if ($verbose) {
$output->writeln('Scheduled update job '.$job->getId().' for package '.$id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/PackageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public function updatePackageAction(Request $req, $name)

$update = $req->request->get('update', $req->query->get('update'));
$autoUpdated = $req->request->get('autoUpdated', $req->query->get('autoUpdated'));
$updateEqualRefs = $req->request->get('updateAll', $req->query->get('updateAll'));
$updateEqualRefs = (bool)$req->request->get('updateAll', $req->query->get('updateAll'));

$user = $this->getUser() ?: $doctrine
->getRepository(User::class)
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ public function setExecuteAfter(DateTimeInterface $executeAfter)
$this->executeAfter = $executeAfter;
}

public function getExecuteAfter()
public function getExecuteAfter(): ?DateTimeInterface
{
return $this->executeAfter;
}

public function getCompletedAt()
public function getCompletedAt(): ?DateTimeInterface
{
return $this->completedAt;
}
Expand Down
23 changes: 3 additions & 20 deletions src/Service/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ public function __construct(\Redis $redis, ManagerRegistry $doctrine)
$this->redis = $redis;
}

/**
* @param $packageOrId
* @param bool $updateEqualRefs
* @param bool $deleteBefore
* @param null $executeAfter
*
* @return Job|object
*/
public function scheduleUpdate($packageOrId, $updateEqualRefs = false, $deleteBefore = false, $executeAfter = null): Job
public function scheduleUpdate($packageOrId, bool $updateEqualRefs = false, bool $deleteBefore = false, \DateTimeInterface $executeAfter = null): Job
{
if ($packageOrId instanceof Package) {
$packageOrId = $packageOrId->getId();
Expand All @@ -36,18 +28,9 @@ public function scheduleUpdate($packageOrId, $updateEqualRefs = false, $deleteBe

$pendingJobId = $this->getPendingUpdateJob($packageOrId, $updateEqualRefs, $deleteBefore);
if ($pendingJobId) {
$pendingJob = $this->doctrine->getManager()->getRepository(Job::class)->findOneBy(['id' => $pendingJobId]);

// pending job will execute before the one we are trying to schedule so skip scheduling
if (
(!$pendingJob->getExecuteAfter() && $executeAfter)
|| ($pendingJob->getExecuteAfter() && $executeAfter && $pendingJob->getExecuteAfter() < $executeAfter)
) {
return $pendingJob;
}
$pendingJob = $this->doctrine->getRepository(Job::class)->findOneBy(['id' => $pendingJobId]);

// neither job has executeAfter, so the pending one is equivalent to the one we are trying to schedule and we can skip scheduling
if (!$pendingJob->getExecuteAfter() && !$executeAfter) {
if (!$pendingJob->getExecuteAfter() || $executeAfter) {
return $pendingJob;
}

Expand Down

0 comments on commit 134dde0

Please sign in to comment.