Merge pull request #45582 from nextcloud/joblist-cleanup-by-id

delete background jobs by id when cleaning up
This commit is contained in:
Robin Appelman 2024-06-17 13:21:21 +02:00 committed by GitHub
commit 642cffd4dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 6 deletions

View file

@ -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)));

View file

@ -60,6 +60,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
*

View file

@ -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);
}
}

View file

@ -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;