2013-04-21 01:27:46 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\BackgroundJob;
|
2020-04-09 12:48:10 +03:00
|
|
|
|
2017-04-25 18:39:58 +03:00
|
|
|
use OCP\BackgroundJob\IJob;
|
2013-04-21 01:27:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DummyJobList
|
|
|
|
*
|
|
|
|
* in memory job list for testing purposes
|
|
|
|
*/
|
|
|
|
class DummyJobList extends \OC\BackgroundJob\JobList {
|
|
|
|
/**
|
2017-04-25 18:39:58 +03:00
|
|
|
* @var IJob[]
|
2013-04-21 01:27:46 +04:00
|
|
|
*/
|
2020-03-26 11:30:18 +03:00
|
|
|
private $jobs = [];
|
2013-04-21 01:27:46 +04:00
|
|
|
|
|
|
|
private $last = 0;
|
|
|
|
|
2015-02-17 18:49:14 +03:00
|
|
|
public function __construct() {
|
|
|
|
}
|
2014-02-11 17:00:24 +04:00
|
|
|
|
2013-04-21 01:27:46 +04:00
|
|
|
/**
|
2017-04-25 18:39:58 +03:00
|
|
|
* @param IJob|string $job
|
2013-04-21 01:51:58 +04:00
|
|
|
* @param mixed $argument
|
2013-04-21 01:27:46 +04:00
|
|
|
*/
|
2013-04-21 01:51:58 +04:00
|
|
|
public function add($job, $argument = null) {
|
2015-02-17 18:49:14 +03:00
|
|
|
if (is_string($job)) {
|
|
|
|
/** @var \OC\BackgroundJob\Job $job */
|
|
|
|
$job = new $job;
|
|
|
|
}
|
2013-04-21 01:51:58 +04:00
|
|
|
$job->setArgument($argument);
|
|
|
|
if (!$this->has($job, null)) {
|
2013-04-21 01:27:46 +04:00
|
|
|
$this->jobs[] = $job;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 18:39:58 +03:00
|
|
|
* @param IJob|string $job
|
2013-04-21 01:51:58 +04:00
|
|
|
* @param mixed $argument
|
2013-04-21 01:27:46 +04:00
|
|
|
*/
|
2013-04-21 01:51:58 +04:00
|
|
|
public function remove($job, $argument = null) {
|
2013-04-21 01:27:46 +04:00
|
|
|
$index = array_search($job, $this->jobs);
|
|
|
|
if ($index !== false) {
|
|
|
|
unset($this->jobs[$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if a job is in the list
|
|
|
|
*
|
|
|
|
* @param $job
|
2013-04-21 01:51:58 +04:00
|
|
|
* @param mixed $argument
|
2013-04-21 01:27:46 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-04-21 01:51:58 +04:00
|
|
|
public function has($job, $argument) {
|
2013-04-21 01:27:46 +04:00
|
|
|
return array_search($job, $this->jobs) !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all jobs in the list
|
|
|
|
*
|
2017-04-25 18:39:58 +03:00
|
|
|
* @return IJob[]
|
2013-04-21 01:27:46 +04:00
|
|
|
*/
|
|
|
|
public function getAll() {
|
|
|
|
return $this->jobs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the next job in the list
|
|
|
|
*
|
2017-04-25 18:39:58 +03:00
|
|
|
* @return IJob|null
|
2013-04-21 01:27:46 +04:00
|
|
|
*/
|
|
|
|
public function getNext() {
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param \OC\BackgroundJob\Job $job
|
|
|
|
*/
|
2017-04-25 18:39:58 +03:00
|
|
|
public function setLastJob(IJob $job) {
|
2013-04-21 01:27:46 +04:00
|
|
|
$i = array_search($job, $this->jobs);
|
|
|
|
if ($i !== false) {
|
|
|
|
$this->last = $i;
|
|
|
|
} else {
|
|
|
|
$this->last = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 16:38:41 +04:00
|
|
|
/**
|
|
|
|
* @param int $id
|
2017-04-25 18:39:58 +03:00
|
|
|
* @return IJob
|
2013-04-24 16:38:41 +04:00
|
|
|
*/
|
|
|
|
public function getById($id) {
|
|
|
|
foreach ($this->jobs as $job) {
|
|
|
|
if ($job->getId() === $id) {
|
|
|
|
return $job;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-04-21 01:27:46 +04:00
|
|
|
/**
|
|
|
|
* set the lastRun of $job to now
|
|
|
|
*
|
2017-04-25 18:39:58 +03:00
|
|
|
* @param IJob $job
|
2013-04-21 01:27:46 +04:00
|
|
|
*/
|
2017-04-25 18:39:58 +03:00
|
|
|
public function setLastRun(IJob $job) {
|
2013-04-21 01:27:46 +04:00
|
|
|
$job->setLastRun(time());
|
|
|
|
}
|
2017-04-25 18:39:58 +03:00
|
|
|
|
|
|
|
public function setExecutionTime(IJob $job, $timeTaken) {
|
|
|
|
}
|
2013-04-21 01:27:46 +04:00
|
|
|
}
|