Add the background job list to the public server container
This commit is contained in:
parent
72f134cfce
commit
a6399f9cef
9 changed files with 179 additions and 38 deletions
4
cron.php
4
cron.php
|
@ -97,7 +97,7 @@ try {
|
|||
touch(TemporaryCronClass::$lockfile);
|
||||
|
||||
// Work
|
||||
$jobList = new \OC\BackgroundJob\JobList();
|
||||
$jobList = \OC::$server->getJobList();
|
||||
$jobs = $jobList->getAll();
|
||||
foreach ($jobs as $job) {
|
||||
$job->execute($jobList, $logger);
|
||||
|
@ -109,7 +109,7 @@ try {
|
|||
OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
|
||||
} else {
|
||||
// Work and success :-)
|
||||
$jobList = new \OC\BackgroundJob\JobList();
|
||||
$jobList = \OC::$server->getJobList();
|
||||
$job = $jobList->getNext();
|
||||
$job->execute($jobList, $logger);
|
||||
$jobList->setLastJob($job);
|
||||
|
|
|
@ -8,7 +8,9 @@
|
|||
|
||||
namespace OC\BackgroundJob;
|
||||
|
||||
abstract class Job {
|
||||
use OCP\BackgroundJob\IJob;
|
||||
|
||||
abstract class Job implements IJob {
|
||||
/**
|
||||
* @var int $id
|
||||
*/
|
||||
|
|
|
@ -8,14 +8,26 @@
|
|||
|
||||
namespace OC\BackgroundJob;
|
||||
|
||||
/**
|
||||
* Class QueuedJob
|
||||
*
|
||||
* create a background job that is to be executed once
|
||||
*
|
||||
* @package OC\BackgroundJob
|
||||
*/
|
||||
class JobList {
|
||||
/**
|
||||
* @var \OCP\IDBConnection
|
||||
*/
|
||||
private $conn;
|
||||
|
||||
/**
|
||||
* @var \OCP\IConfig $config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @param \OCP\IDBConnection $conn
|
||||
* @param \OCP\IConfig $config
|
||||
*/
|
||||
public function __construct($conn, $config) {
|
||||
$this->conn = $conn;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Job|string $job
|
||||
* @param mixed $argument
|
||||
|
@ -28,7 +40,7 @@ class JobList {
|
|||
$class = $job;
|
||||
}
|
||||
$argument = json_encode($argument);
|
||||
$query = \OC_DB::prepare('INSERT INTO `*PREFIX*jobs`(`class`, `argument`, `last_run`) VALUES(?, ?, 0)');
|
||||
$query = $this->conn->prepare('INSERT INTO `*PREFIX*jobs`(`class`, `argument`, `last_run`) VALUES(?, ?, 0)');
|
||||
$query->execute(array($class, $argument));
|
||||
}
|
||||
}
|
||||
|
@ -45,10 +57,10 @@ class JobList {
|
|||
}
|
||||
if (!is_null($argument)) {
|
||||
$argument = json_encode($argument);
|
||||
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*jobs` WHERE `class` = ? AND `argument` = ?');
|
||||
$query = $this->conn->prepare('DELETE FROM `*PREFIX*jobs` WHERE `class` = ? AND `argument` = ?');
|
||||
$query->execute(array($class, $argument));
|
||||
} else {
|
||||
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*jobs` WHERE `class` = ?');
|
||||
$query = $this->conn->prepare('DELETE FROM `*PREFIX*jobs` WHERE `class` = ?');
|
||||
$query->execute(array($class));
|
||||
}
|
||||
}
|
||||
|
@ -67,9 +79,9 @@ class JobList {
|
|||
$class = $job;
|
||||
}
|
||||
$argument = json_encode($argument);
|
||||
$query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*jobs` WHERE `class` = ? AND `argument` = ?');
|
||||
$result = $query->execute(array($class, $argument));
|
||||
return (bool)$result->fetchRow();
|
||||
$query = $this->conn->prepare('SELECT `id` FROM `*PREFIX*jobs` WHERE `class` = ? AND `argument` = ?');
|
||||
$query->execute(array($class, $argument));
|
||||
return (bool)$query->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,10 +90,10 @@ class JobList {
|
|||
* @return Job[]
|
||||
*/
|
||||
public function getAll() {
|
||||
$query = \OC_DB::prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs`');
|
||||
$result = $query->execute();
|
||||
$query = $this->conn->prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs`');
|
||||
$query->execute();
|
||||
$jobs = array();
|
||||
while ($row = $result->fetchRow()) {
|
||||
while ($row = $query->fetch()) {
|
||||
$jobs[] = $this->buildJob($row);
|
||||
}
|
||||
return $jobs;
|
||||
|
@ -94,15 +106,15 @@ class JobList {
|
|||
*/
|
||||
public function getNext() {
|
||||
$lastId = $this->getLastJob();
|
||||
$query = \OC_DB::prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` WHERE `id` > ? ORDER BY `id` ASC', 1);
|
||||
$result = $query->execute(array($lastId));
|
||||
if ($row = $result->fetchRow()) {
|
||||
$query = $this->conn->prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` WHERE `id` > ? ORDER BY `id` ASC', 1);
|
||||
$query->execute(array($lastId));
|
||||
if ($row = $query->fetch()) {
|
||||
return $this->buildJob($row);
|
||||
} else {
|
||||
//begin at the start of the queue
|
||||
$query = \OC_DB::prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` ORDER BY `id` ASC', 1);
|
||||
$result = $query->execute();
|
||||
if ($row = $result->fetchRow()) {
|
||||
$query = $this->conn->prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` ORDER BY `id` ASC', 1);
|
||||
$query->execute();
|
||||
if ($row = $query->fetch()) {
|
||||
return $this->buildJob($row);
|
||||
} else {
|
||||
return null; //empty job list
|
||||
|
@ -115,9 +127,9 @@ class JobList {
|
|||
* @return Job
|
||||
*/
|
||||
public function getById($id) {
|
||||
$query = \OC_DB::prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` WHERE `id` = ?');
|
||||
$result = $query->execute(array($id));
|
||||
if ($row = $result->fetchRow()) {
|
||||
$query = $this->conn->prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` WHERE `id` = ?');
|
||||
$query->execute(array($id));
|
||||
if ($row = $query->fetch()) {
|
||||
return $this->buildJob($row);
|
||||
} else {
|
||||
return null;
|
||||
|
@ -148,7 +160,7 @@ class JobList {
|
|||
* @param Job $job
|
||||
*/
|
||||
public function setLastJob($job) {
|
||||
\OC_Appconfig::setValue('backgroundjob', 'lastjob', $job->getId());
|
||||
$this->config->setAppValue('backgroundjob', 'lastjob', $job->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -157,7 +169,7 @@ class JobList {
|
|||
* @return int
|
||||
*/
|
||||
public function getLastJob() {
|
||||
return \OC_Appconfig::getValue('backgroundjob', 'lastjob', 0);
|
||||
$this->config->getAppValue('backgroundjob', 'lastjob', 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -166,7 +178,7 @@ class JobList {
|
|||
* @param Job $job
|
||||
*/
|
||||
public function setLastRun($job) {
|
||||
$query = \OC_DB::prepare('UPDATE `*PREFIX*jobs` SET `last_run` = ? WHERE `id` = ?');
|
||||
$query = $this->conn->prepare('UPDATE `*PREFIX*jobs` SET `last_run` = ? WHERE `id` = ?');
|
||||
$query->execute(array(time(), $job->getId()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -148,6 +148,13 @@ class Server extends SimpleContainer implements IServerContainer {
|
|||
$this->registerService('AvatarManager', function($c) {
|
||||
return new AvatarManager();
|
||||
});
|
||||
$this->registerService('JobList', function ($c) {
|
||||
/**
|
||||
* @var Server $c
|
||||
*/
|
||||
$config = $c->getConfig();
|
||||
return new \OC\BackgroundJob\JobList($c->getDatabaseConnection(), $config);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -336,4 +343,13 @@ class Server extends SimpleContainer implements IServerContainer {
|
|||
function getActivityManager() {
|
||||
return $this->query('ActivityManager');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an job list for controlling background jobs
|
||||
*
|
||||
* @return \OCP\BackgroundJob\IJobList
|
||||
*/
|
||||
function getJobList(){
|
||||
return $this->query('JobList');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ use \OC\BackgroundJob\JobList;
|
|||
/**
|
||||
* This class provides functions to register backgroundjobs in ownCloud
|
||||
*
|
||||
* To create a new backgroundjob create a new class that inharits from either \OC\BackgroundJob\Job,
|
||||
* To create a new backgroundjob create a new class that inherits from either \OC\BackgroundJob\Job,
|
||||
* \OC\BackgroundJob\QueuedJob or \OC\BackgroundJob\TimedJob and register it using
|
||||
* \OCP\BackgroundJob->registerJob($job, $argument), $argument will be passed to the run() function
|
||||
* of the job when the job is executed.
|
||||
|
@ -73,7 +73,7 @@ class BackgroundJob {
|
|||
* @param mixed $argument
|
||||
*/
|
||||
public static function registerJob($job, $argument = null) {
|
||||
$jobList = new JobList();
|
||||
$jobList = \OC::$server->getJobList();
|
||||
$jobList->add($job, $argument);
|
||||
}
|
||||
|
||||
|
@ -99,7 +99,7 @@ class BackgroundJob {
|
|||
* key is string "$klass-$method", value is array( $klass, $method )
|
||||
*/
|
||||
static public function allRegularTasks() {
|
||||
$jobList = new JobList();
|
||||
$jobList = \OC::$server->getJobList();
|
||||
$allJobs = $jobList->getAll();
|
||||
$regularJobs = array();
|
||||
foreach ($allJobs as $job) {
|
||||
|
@ -118,7 +118,7 @@ class BackgroundJob {
|
|||
* @return associative array
|
||||
*/
|
||||
public static function findQueuedTask($id) {
|
||||
$jobList = new JobList();
|
||||
$jobList = \OC::$server->getJobList();
|
||||
return $jobList->getById($id);
|
||||
}
|
||||
|
||||
|
@ -128,7 +128,7 @@ class BackgroundJob {
|
|||
* @return array with associative arrays
|
||||
*/
|
||||
public static function allQueuedTasks() {
|
||||
$jobList = new JobList();
|
||||
$jobList = \OC::$server->getJobList();
|
||||
$allJobs = $jobList->getAll();
|
||||
$queuedJobs = array();
|
||||
foreach ($allJobs as $job) {
|
||||
|
@ -148,7 +148,7 @@ class BackgroundJob {
|
|||
* @return array with associative arrays
|
||||
*/
|
||||
public static function queuedTaskWhereAppIs($app) {
|
||||
$jobList = new JobList();
|
||||
$jobList = \OC::$server->getJobList();
|
||||
$allJobs = $jobList->getAll();
|
||||
$queuedJobs = array();
|
||||
foreach ($allJobs as $job) {
|
||||
|
@ -186,7 +186,7 @@ class BackgroundJob {
|
|||
* Deletes a report
|
||||
*/
|
||||
public static function deleteQueuedTask($id) {
|
||||
$jobList = new JobList();
|
||||
$jobList = \OC::$server->getJobList();
|
||||
$job = $jobList->getById($id);
|
||||
if ($job) {
|
||||
$jobList->remove($job);
|
||||
|
|
29
lib/public/backgroundjob/ijob.php
Normal file
29
lib/public/backgroundjob/ijob.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?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 OCP\BackgroundJob;
|
||||
|
||||
interface IJob {
|
||||
/**
|
||||
* @param \OCP\BackgroundJob\IJobList $jobList
|
||||
* @param \OC\Log $logger
|
||||
*/
|
||||
public function execute($jobList, $logger = null);
|
||||
|
||||
public function setId($id);
|
||||
|
||||
public function setLastRun($lastRun);
|
||||
|
||||
public function setArgument($argument);
|
||||
|
||||
public function getId();
|
||||
|
||||
public function getLastRun();
|
||||
|
||||
public function getArgument();
|
||||
}
|
73
lib/public/backgroundjob/ijoblist.php
Normal file
73
lib/public/backgroundjob/ijoblist.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?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 OCP\BackgroundJob;
|
||||
|
||||
interface IJobList {
|
||||
/**
|
||||
* @param \OCP\BackgroundJob\IJob |string $job
|
||||
* @param mixed $argument
|
||||
*/
|
||||
public function add($job, $argument = null);
|
||||
|
||||
/**
|
||||
* @param \OCP\BackgroundJob\IJob|string $job
|
||||
* @param mixed $argument
|
||||
*/
|
||||
public function remove($job, $argument = null);
|
||||
|
||||
/**
|
||||
* check if a job is in the list
|
||||
*
|
||||
* @param $job
|
||||
* @param mixed $argument
|
||||
* @return bool
|
||||
*/
|
||||
public function has($job, $argument);
|
||||
|
||||
/**
|
||||
* get all jobs in the list
|
||||
*
|
||||
* @return \OCP\BackgroundJob\IJob[]
|
||||
*/
|
||||
public function getAll();
|
||||
|
||||
/**
|
||||
* get the next job in the list
|
||||
*
|
||||
* @return \OCP\BackgroundJob\IJob
|
||||
*/
|
||||
public function getNext();
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return \OCP\BackgroundJob\IJob
|
||||
*/
|
||||
public function getById($id);
|
||||
|
||||
/**
|
||||
* set the job that was last ran
|
||||
*
|
||||
* @param \OCP\BackgroundJob\IJob $job
|
||||
*/
|
||||
public function setLastJob($job);
|
||||
|
||||
/**
|
||||
* get the id of the last ran job
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastJob();
|
||||
|
||||
/**
|
||||
* set the lastRun of $job to now
|
||||
*
|
||||
* @param \OCP\BackgroundJob\IJob $job
|
||||
*/
|
||||
public function setLastRun($job);
|
||||
}
|
|
@ -176,4 +176,11 @@ interface IServerContainer {
|
|||
*/
|
||||
function getAvatarManager();
|
||||
|
||||
/**
|
||||
* Returns an job list for controlling background jobs
|
||||
*
|
||||
* @return \OCP\BackgroundJob\IJobList
|
||||
*/
|
||||
function getJobList();
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
|
|||
|
||||
private $last = 0;
|
||||
|
||||
public function __construct(){}
|
||||
|
||||
/**
|
||||
* @param \OC\BackgroundJob\Job|string $job
|
||||
* @param mixed $argument
|
||||
|
|
Loading…
Reference in a new issue