Merge pull request #45919 from nextcloud/backport/45582/stable29

[stable29] delete background jobs by id when cleaning up
pull/46091/head
Andy Scherzinger 2024-07-10 22:05:38 +07:00 committed by GitHub
commit db748b4652
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 6 deletions

@ -135,7 +135,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)));

@ -79,6 +79,14 @@ interface IJobList {
*/
public function remove($job, $argument = null): void;
/**
* Remove a job from the list by id
*
* @param int $id
* @since 29.0.4
*/
public function removeById(int $id): void;
/**
* check if a job is in the list
*

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

@ -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 @@ 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;
}
@ -55,9 +58,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;
}
}
}
@ -127,7 +141,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;