aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2024-06-17 13:21:21 +0200
committerGitHub <noreply@github.com>2024-06-17 13:21:21 +0200
commit642cffd4ddeab35eb8c8fe00c4bea5dea33409e6 (patch)
tree3eb7fa8cdcd7377d043fa2886cd524fafb440ed3
parent0b805eab382e0235fa560c703f942fd5bc925062 (diff)
parent85b9552617a1989a56d1252e683b632cc002bf4c (diff)
downloadnextcloud-server-642cffd4ddeab35eb8c8fe00c4bea5dea33409e6.tar.gz
nextcloud-server-642cffd4ddeab35eb8c8fe00c4bea5dea33409e6.zip
Merge pull request #45582 from nextcloud/joblist-cleanup-by-id
delete background jobs by id when cleaning up
-rw-r--r--lib/private/BackgroundJob/JobList.php2
-rw-r--r--lib/public/BackgroundJob/IJobList.php8
-rw-r--r--lib/public/BackgroundJob/QueuedJob.php6
-rw-r--r--tests/lib/BackgroundJob/DummyJobList.php22
4 files changed, 32 insertions, 6 deletions
diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php
index 3df9be7558c..61c48b0eab2 100644
--- a/lib/private/BackgroundJob/JobList.php
+++ b/lib/private/BackgroundJob/JobList.php
@@ -113,7 +113,7 @@ class JobList implements IJobList {
}
}
- 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 dbe154ff5f8..c082ef22f2f 100644
--- a/lib/public/BackgroundJob/IJobList.php
+++ b/lib/public/BackgroundJob/IJobList.php
@@ -61,6 +61,14 @@ interface IJobList {
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
*
* @param IJob|class-string<IJob> $job
diff --git a/lib/public/BackgroundJob/QueuedJob.php b/lib/public/BackgroundJob/QueuedJob.php
index c75948d1a11..caacff42b1a 100644
--- a/lib/public/BackgroundJob/QueuedJob.php
+++ b/lib/public/BackgroundJob/QueuedJob.php
@@ -35,7 +35,11 @@ abstract class QueuedJob extends Job {
* @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 fc80786d28b..7b7fce7e9e8 100644
--- a/tests/lib/BackgroundJob/DummyJobList.php
+++ b/tests/lib/BackgroundJob/DummyJobList.php
@@ -26,6 +26,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
private array $reserved = [];
private int $last = 0;
+ private int $lastId = 0;
public function __construct() {
}
@@ -40,6 +41,8 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
$job = \OCP\Server::get($job);
}
$job->setArgument($argument);
+ $job->setId($this->lastId);
+ $this->lastId++;
if (!$this->has($job, null)) {
$this->jobs[] = $job;
}
@@ -54,9 +57,20 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
* @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;
+ }
}
}
@@ -126,7 +140,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
}
}
- public function getById(int $id): IJob {
+ public function getById(int $id): ?IJob {
foreach ($this->jobs as $job) {
if ($job->getId() === $id) {
return $job;