2013-04-20 17:27:46 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2013-04-20 17:27:46 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\BackgroundJob;
|
2020-04-09 05:48:10 -04:00
|
|
|
|
2017-04-25 11:39:58 -04:00
|
|
|
use OCP\BackgroundJob\IJob;
|
2013-04-20 17:27:46 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class DummyJobList
|
|
|
|
|
*
|
|
|
|
|
* in memory job list for testing purposes
|
|
|
|
|
*/
|
|
|
|
|
class DummyJobList extends \OC\BackgroundJob\JobList {
|
|
|
|
|
/**
|
2017-04-25 11:39:58 -04:00
|
|
|
* @var IJob[]
|
2013-04-20 17:27:46 -04:00
|
|
|
*/
|
2022-06-27 06:06:23 -04:00
|
|
|
private array $jobs = [];
|
2013-04-20 17:27:46 -04:00
|
|
|
|
2023-04-20 07:18:28 -04:00
|
|
|
/**
|
|
|
|
|
* @var bool[]
|
|
|
|
|
*/
|
|
|
|
|
private array $reserved = [];
|
|
|
|
|
|
2022-06-27 06:06:23 -04:00
|
|
|
private int $last = 0;
|
2024-05-30 09:07:39 -04:00
|
|
|
private int $lastId = 0;
|
2013-04-20 17:27:46 -04:00
|
|
|
|
2015-02-17 10:49:14 -05:00
|
|
|
public function __construct() {
|
|
|
|
|
}
|
2014-02-11 08:00:24 -05:00
|
|
|
|
2013-04-20 17:27:46 -04:00
|
|
|
/**
|
2022-06-27 06:06:23 -04:00
|
|
|
* @param IJob|class-string<IJob> $job
|
2013-04-20 17:51:58 -04:00
|
|
|
* @param mixed $argument
|
2013-04-20 17:27:46 -04:00
|
|
|
*/
|
2024-03-28 11:13:19 -04:00
|
|
|
public function add($job, $argument = null, ?int $firstCheck = null): void {
|
2015-02-17 10:49:14 -05:00
|
|
|
if (is_string($job)) {
|
2022-06-27 06:06:23 -04:00
|
|
|
/** @var IJob $job */
|
2022-06-28 06:55:26 -04:00
|
|
|
$job = \OCP\Server::get($job);
|
2015-02-17 10:49:14 -05:00
|
|
|
}
|
2013-04-20 17:51:58 -04:00
|
|
|
$job->setArgument($argument);
|
2024-05-30 09:07:39 -04:00
|
|
|
$job->setId($this->lastId);
|
|
|
|
|
$this->lastId++;
|
2013-04-20 17:51:58 -04:00
|
|
|
if (!$this->has($job, null)) {
|
2013-04-20 17:27:46 -04:00
|
|
|
$this->jobs[] = $job;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 08:53:04 -04:00
|
|
|
public function scheduleAfter(string $job, int $runAfter, $argument = null): void {
|
|
|
|
|
$this->add($job, $argument, $runAfter);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-20 17:27:46 -04:00
|
|
|
/**
|
2017-04-25 11:39:58 -04:00
|
|
|
* @param IJob|string $job
|
2013-04-20 17:51:58 -04:00
|
|
|
* @param mixed $argument
|
2013-04-20 17:27:46 -04:00
|
|
|
*/
|
2022-06-27 06:06:23 -04:00
|
|
|
public function remove($job, $argument = null): void {
|
2024-05-30 09:07:39 -04:00
|
|
|
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;
|
|
|
|
|
}
|
2013-04-20 17:27:46 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* check if a job is in the list
|
|
|
|
|
*
|
|
|
|
|
* @param $job
|
2013-04-20 17:51:58 -04:00
|
|
|
* @param mixed $argument
|
2013-04-20 17:27:46 -04:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2022-06-27 06:06:23 -04:00
|
|
|
public function has($job, $argument): bool {
|
2013-04-20 17:27:46 -04:00
|
|
|
return array_search($job, $this->jobs) !== false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get all jobs in the list
|
|
|
|
|
*
|
2017-04-25 11:39:58 -04:00
|
|
|
* @return IJob[]
|
2013-04-20 17:27:46 -04:00
|
|
|
*/
|
2022-07-12 08:27:02 -04:00
|
|
|
public function getAll(): array {
|
2013-04-20 17:27:46 -04:00
|
|
|
return $this->jobs;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 11:18:59 -05:00
|
|
|
public function getJobsIterator($job, ?int $limit, int $offset): array {
|
2022-06-27 06:06:23 -04:00
|
|
|
if ($job instanceof IJob) {
|
|
|
|
|
$jobClass = get_class($job);
|
|
|
|
|
} else {
|
|
|
|
|
$jobClass = $job;
|
|
|
|
|
}
|
|
|
|
|
return array_slice(
|
|
|
|
|
array_filter(
|
|
|
|
|
$this->jobs,
|
|
|
|
|
fn ($job) => ($jobClass === null) || (get_class($job) == $jobClass)
|
|
|
|
|
),
|
|
|
|
|
$offset,
|
|
|
|
|
$limit
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-20 17:27:46 -04:00
|
|
|
/**
|
|
|
|
|
* get the next job in the list
|
|
|
|
|
*/
|
2024-04-08 11:25:51 -04:00
|
|
|
public function getNext(bool $onlyTimeSensitive = false, ?array $jobClasses = null): ?IJob {
|
2013-04-20 17:27:46 -04:00
|
|
|
if (count($this->jobs) > 0) {
|
|
|
|
|
if ($this->last < (count($this->jobs) - 1)) {
|
|
|
|
|
$i = $this->last + 1;
|
|
|
|
|
} else {
|
|
|
|
|
$i = 0;
|
|
|
|
|
}
|
|
|
|
|
return $this->jobs[$i];
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set the job that was last ran
|
|
|
|
|
*
|
2024-02-08 05:52:40 -05:00
|
|
|
* @param \OCP\BackgroundJob\Job $job
|
2013-04-20 17:27:46 -04:00
|
|
|
*/
|
2022-06-27 06:06:23 -04:00
|
|
|
public function setLastJob(IJob $job): void {
|
2013-04-20 17:27:46 -04:00
|
|
|
$i = array_search($job, $this->jobs);
|
|
|
|
|
if ($i !== false) {
|
|
|
|
|
$this->last = $i;
|
|
|
|
|
} else {
|
|
|
|
|
$this->last = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 09:07:39 -04:00
|
|
|
public function getById(int $id): ?IJob {
|
2013-04-24 08:38:41 -04:00
|
|
|
foreach ($this->jobs as $job) {
|
|
|
|
|
if ($job->getId() === $id) {
|
|
|
|
|
return $job;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 11:15:26 -04:00
|
|
|
public function getDetailsById(int $id): ?array {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-27 06:06:23 -04:00
|
|
|
public function setLastRun(IJob $job): void {
|
2013-04-20 17:27:46 -04:00
|
|
|
$job->setLastRun(time());
|
|
|
|
|
}
|
2017-04-25 11:39:58 -04:00
|
|
|
|
2023-04-20 07:18:28 -04:00
|
|
|
public function hasReservedJob(?string $className = null): bool {
|
2023-06-29 11:07:31 -04:00
|
|
|
return isset($this->reserved[$className ?? '']) && $this->reserved[$className ?? ''];
|
2023-04-20 07:18:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setHasReservedJob(?string $className, bool $hasReserved): void {
|
2023-04-24 06:35:52 -04:00
|
|
|
$this->reserved[$className ?? ''] = $hasReserved;
|
2023-04-20 07:18:28 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-27 06:06:23 -04:00
|
|
|
public function setExecutionTime(IJob $job, $timeTaken): void {
|
2017-04-25 11:39:58 -04:00
|
|
|
}
|
2021-05-31 11:15:26 -04:00
|
|
|
|
|
|
|
|
public function resetBackgroundJob(IJob $job): void {
|
|
|
|
|
}
|
2013-04-20 17:27:46 -04:00
|
|
|
}
|