diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php index ae214f695095d..2f98dd4c8969a 100644 --- a/lib/private/BackgroundJob/JobList.php +++ b/lib/private/BackgroundJob/JobList.php @@ -127,7 +127,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 a0f32cb0359bb..b66ed817238dc 100644 --- a/lib/public/BackgroundJob/IJobList.php +++ b/lib/public/BackgroundJob/IJobList.php @@ -66,6 +66,14 @@ public function add($job, $argument = null): void; */ public function remove($job, $argument = null): void; + /** + * Remove a job from the list by id + * + * @param int $id + * @since 27.1.12 + */ + 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 e93db3420b807..2b5b67f2b66c5 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); } } diff --git a/tests/lib/BackgroundJob/DummyJobList.php b/tests/lib/BackgroundJob/DummyJobList.php index 8574f462ca76e..419e34b752372 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): void { $job = \OCP\Server::get($job); } $job->setArgument($argument); + $job->setId($this->lastId); + $this->lastId++; if (!$this->has($job, null)) { $this->jobs[] = $job; } @@ -51,9 +54,20 @@ public function add($job, $argument = null): void { * @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; + } } } @@ -123,7 +137,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;