Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete background jobs by id when cleaning up #45582

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/private/BackgroundJob/JobList.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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)));
Expand Down
8 changes: 8 additions & 0 deletions lib/public/BackgroundJob/IJobList.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/**
* check if a job is in the list
*
Expand Down
6 changes: 5 additions & 1 deletion lib/public/BackgroundJob/QueuedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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);
}
}
22 changes: 18 additions & 4 deletions tests/lib/BackgroundJob/DummyJobList.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
private array $reserved = [];

private int $last = 0;
private int $lastId = 0;

public function __construct() {
}
Expand All @@ -40,6 +41,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;
}
Expand All @@ -54,9 +57,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;
}
}
}

Expand Down Expand Up @@ -126,7 +140,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;
Expand Down
Loading