diff options
author | Joas Schilling <213943+nickvergessen@users.noreply.github.com> | 2023-07-28 13:15:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-28 13:15:10 +0200 |
commit | 53392861ff2a2786c9be565a9d5fb09eb652f29a (patch) | |
tree | 3e669e9dc698746300922540e43197fb9177e26f /lib | |
parent | ff2b36ad5258160a6c1cf3f95d78b2fec0f8c879 (diff) | |
parent | 7e790a3297708279f98d193ea704326139cf0483 (diff) | |
download | nextcloud-server-53392861ff2a2786c9be565a9d5fb09eb652f29a.tar.gz nextcloud-server-53392861ff2a2786c9be565a9d5fb09eb652f29a.zip |
Merge pull request #39473 from nextcloud/fix/parallel-aware-job
fix(IParallelAwareJob): Check for other reserved jobs before setting new ones as reserved
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/BackgroundJob/JobList.php | 32 | ||||
-rw-r--r-- | lib/public/BackgroundJob/Job.php | 5 |
2 files changed, 23 insertions, 14 deletions
diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php index 3cdfee51138..36cccbd4eab 100644 --- a/lib/private/BackgroundJob/JobList.php +++ b/lib/private/BackgroundJob/JobList.php @@ -35,6 +35,7 @@ use OCP\AppFramework\Utility\ITimeFactory; use OCP\AutoloadNotAllowedException; use OCP\BackgroundJob\IJob; use OCP\BackgroundJob\IJobList; +use OCP\BackgroundJob\IParallelAwareJob; use OCP\DB\Exception; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IConfig; @@ -218,19 +219,33 @@ class JobList implements IJobList { $query->andWhere($query->expr()->eq('time_sensitive', $query->createNamedParameter(IJob::TIME_SENSITIVE, IQueryBuilder::PARAM_INT))); } - $update = $this->connection->getQueryBuilder(); - $update->update('jobs') - ->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime())) - ->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime())) - ->where($update->expr()->eq('id', $update->createParameter('jobid'))) - ->andWhere($update->expr()->eq('reserved_at', $update->createParameter('reserved_at'))) - ->andWhere($update->expr()->eq('last_checked', $update->createParameter('last_checked'))); - $result = $query->executeQuery(); $row = $result->fetch(); $result->closeCursor(); if ($row) { + $job = $this->buildJob($row); + + if ($job instanceof IParallelAwareJob && !$job->getAllowParallelRuns() && $this->hasReservedJob(get_class($job))) { + $this->logger->debug('Skipping ' . get_class($job) . ' job with ID ' . $job->getId() . ' because another job with the same class is already running', ['app' => 'cron']); + + $update = $this->connection->getQueryBuilder(); + $update->update('jobs') + ->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime() + 1)) + ->where($update->expr()->eq('id', $update->createParameter('jobid'))); + $update->setParameter('jobid', $row['id']); + $update->executeStatement(); + + return $this->getNext($onlyTimeSensitive); + } + + $update = $this->connection->getQueryBuilder(); + $update->update('jobs') + ->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime())) + ->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime())) + ->where($update->expr()->eq('id', $update->createParameter('jobid'))) + ->andWhere($update->expr()->eq('reserved_at', $update->createParameter('reserved_at'))) + ->andWhere($update->expr()->eq('last_checked', $update->createParameter('last_checked'))); $update->setParameter('jobid', $row['id']); $update->setParameter('reserved_at', $row['reserved_at']); $update->setParameter('last_checked', $row['last_checked']); @@ -240,7 +255,6 @@ class JobList implements IJobList { // Background job already executed elsewhere, try again. return $this->getNext($onlyTimeSensitive); } - $job = $this->buildJob($row); if ($job === null) { // set the last_checked to 12h in the future to not check failing jobs all over again diff --git a/lib/public/BackgroundJob/Job.php b/lib/public/BackgroundJob/Job.php index 455fb3d42e7..a574e90e1a0 100644 --- a/lib/public/BackgroundJob/Job.php +++ b/lib/public/BackgroundJob/Job.php @@ -75,11 +75,6 @@ abstract class Job implements IJob, IParallelAwareJob { $jobList->setLastRun($this); $logger = $this->logger ?? \OCP\Server::get(LoggerInterface::class); - if (!$this->getAllowParallelRuns() && $jobList->hasReservedJob(get_class($this))) { - $logger->debug('Skipping ' . get_class($this) . ' job with ID ' . $this->getId() . ' because another job with the same class is already running', ['app' => 'cron']); - return; - } - try { $jobStartTime = $this->time->getTime(); $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']); |