From 7a801f0690a7d00e31e4155cb286ceb93feb1262 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 30 May 2024 15:07:26 +0200 Subject: [PATCH 1/2] fix: delete background jobs by id when cleaning up Signed-off-by: Robin Appelman --- lib/private/BackgroundJob/JobList.php | 2 +- lib/public/BackgroundJob/IJobList.php | 8 ++++++++ lib/public/BackgroundJob/QueuedJob.php | 6 +++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php index 4e5d11604e636..851858245e963 100644 --- a/lib/private/BackgroundJob/JobList.php +++ b/lib/private/BackgroundJob/JobList.php @@ -135,7 +135,7 @@ public function remove($job, $argument = null): void { } } - protected function removeById(int $id): void { + public function removeById(int $id): void { $query = $this->connection->getQueryBuilder(); $query->delete('jobs') ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); diff --git a/lib/public/BackgroundJob/IJobList.php b/lib/public/BackgroundJob/IJobList.php index 07b5ebcf48bd0..19c705cafec43 100644 --- a/lib/public/BackgroundJob/IJobList.php +++ b/lib/public/BackgroundJob/IJobList.php @@ -79,6 +79,14 @@ public function scheduleAfter(string $job, int $runAfter, $argument = null): voi */ public function remove($job, $argument = null): void; + /** + * Remove a job from the list by id + * + * @param int $id + * @since 30.0.0 + */ + public function removeById(int $id): void; + /** * check if a job is in the list * diff --git a/lib/public/BackgroundJob/QueuedJob.php b/lib/public/BackgroundJob/QueuedJob.php index bac60f9be11df..da722d545464c 100644 --- a/lib/public/BackgroundJob/QueuedJob.php +++ b/lib/public/BackgroundJob/QueuedJob.php @@ -53,7 +53,11 @@ final public function execute($jobList, ?ILogger $logger = null) { * @since 25.0.0 */ final public function start(IJobList $jobList): void { - $jobList->remove($this, $this->argument); + if ($this->id) { + $jobList->removeById($this->id); + } else { + $jobList->remove($this, $this->argument); + } parent::start($jobList); } } From f3c3d890d69ea3bc6a1f6c34a6023085f6cac598 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 30 May 2024 15:07:39 +0200 Subject: [PATCH 2/2] test: update DummyJobList Signed-off-by: Robin Appelman --- lib/public/BackgroundJob/IJobList.php | 2 +- tests/lib/BackgroundJob/DummyJobList.php | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/public/BackgroundJob/IJobList.php b/lib/public/BackgroundJob/IJobList.php index 19c705cafec43..8b39a86b17892 100644 --- a/lib/public/BackgroundJob/IJobList.php +++ b/lib/public/BackgroundJob/IJobList.php @@ -83,7 +83,7 @@ public function remove($job, $argument = null): void; * Remove a job from the list by id * * @param int $id - * @since 30.0.0 + * @since 29.0.4 */ public function removeById(int $id): void; diff --git a/tests/lib/BackgroundJob/DummyJobList.php b/tests/lib/BackgroundJob/DummyJobList.php index 64c0cf8038e95..ccebb872a0ef4 100644 --- a/tests/lib/BackgroundJob/DummyJobList.php +++ b/tests/lib/BackgroundJob/DummyJobList.php @@ -27,6 +27,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList { private array $reserved = []; private int $last = 0; + private int $lastId = 0; public function __construct() { } @@ -41,6 +42,8 @@ public function add($job, $argument = null, ?int $firstCheck = null): void { $job = \OCP\Server::get($job); } $job->setArgument($argument); + $job->setId($this->lastId); + $this->lastId++; if (!$this->has($job, null)) { $this->jobs[] = $job; } @@ -55,9 +58,20 @@ public function scheduleAfter(string $job, int $runAfter, $argument = null): voi * @param mixed $argument */ public function remove($job, $argument = null): void { - $index = array_search($job, $this->jobs); - if ($index !== false) { - unset($this->jobs[$index]); + foreach ($this->jobs as $index => $listJob) { + if (get_class($job) === get_class($listJob) && $job->getArgument() == $listJob->getArgument()) { + unset($this->jobs[$index]); + return; + } + } + } + + public function removeById(int $id): void { + foreach ($this->jobs as $index => $listJob) { + if ($listJob->getId() === $id) { + unset($this->jobs[$index]); + return; + } } } @@ -127,7 +141,7 @@ public function setLastJob(IJob $job): void { } } - public function getById(int $id): IJob { + public function getById(int $id): ?IJob { foreach ($this->jobs as $job) { if ($job->getId() === $id) { return $job;