From d0a5fe1f4a39f21c596293232be19d70ad30652e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 20 Apr 2013 21:26:47 +0200 Subject: [PATCH 01/15] code style --- cron.php | 98 +++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/cron.php b/cron.php index 7c875843c7..07ce45ac22 100644 --- a/cron.php +++ b/cron.php @@ -1,24 +1,24 @@ . -* -*/ + * ownCloud + * + * @author Jakob Sack + * @copyright 2012 Jakob Sack owncloud@jakobsack.de + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see . + * + */ // Unfortunately we need this class for shutdown function class TemporaryCronClass { @@ -30,17 +30,16 @@ class TemporaryCronClass { // We use this function to handle (unexpected) shutdowns function handleUnexpectedShutdown() { // Delete lockfile - if( !TemporaryCronClass::$keeplock && file_exists( TemporaryCronClass::$lockfile )) { - unlink( TemporaryCronClass::$lockfile ); + if (!TemporaryCronClass::$keeplock && file_exists(TemporaryCronClass::$lockfile)) { + unlink(TemporaryCronClass::$lockfile); } - + // Say goodbye if the app did not shutdown properly - if( !TemporaryCronClass::$sent ) { - if( OC::$CLI ) { - echo 'Unexpected error!'.PHP_EOL; - } - else{ - OC_JSON::error( array( 'data' => array( 'message' => 'Unexpected error!'))); + if (!TemporaryCronClass::$sent) { + if (OC::$CLI) { + echo 'Unexpected error!' . PHP_EOL; + } else { + OC_JSON::error(array('data' => array('message' => 'Unexpected error!'))); } } } @@ -51,8 +50,8 @@ require_once 'lib/base.php'; session_write_close(); // Don't do anything if ownCloud has not been installed -if( !OC_Config::getValue( 'installed', false )) { - exit( 0 ); +if (!OC_Config::getValue('installed', false)) { + exit(0); } // Handle unexpected errors @@ -63,48 +62,45 @@ OC_Helper::cleanTmpNoClean(); // Exit if background jobs are disabled! $appmode = OC_BackgroundJob::getExecutionType(); -if( $appmode == 'none' ) { +if ($appmode == 'none') { TemporaryCronClass::$sent = true; - if( OC::$CLI ) { - echo 'Background Jobs are disabled!'.PHP_EOL; + if (OC::$CLI) { + echo 'Background Jobs are disabled!' . PHP_EOL; + } else { + OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!'))); } - else{ - OC_JSON::error( array( 'data' => array( 'message' => 'Background jobs disabled!'))); - } - exit( 1 ); + exit(1); } -if( OC::$CLI ) { +if (OC::$CLI) { // Create lock file first - TemporaryCronClass::$lockfile = OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ).'/cron.lock'; - + TemporaryCronClass::$lockfile = OC_Config::getValue("datadirectory", OC::$SERVERROOT . '/data') . '/cron.lock'; + // We call ownCloud from the CLI (aka cron) - if( $appmode != 'cron' ) { + if ($appmode != 'cron') { // Use cron in feature! - OC_BackgroundJob::setExecutionType('cron' ); + OC_BackgroundJob::setExecutionType('cron'); } // check if backgroundjobs is still running - if( file_exists( TemporaryCronClass::$lockfile )) { + if (file_exists(TemporaryCronClass::$lockfile)) { TemporaryCronClass::$keeplock = true; TemporaryCronClass::$sent = true; echo "Another instance of cron.php is still running!"; - exit( 1 ); + exit(1); } // Create a lock file - touch( TemporaryCronClass::$lockfile ); + touch(TemporaryCronClass::$lockfile); // Work OC_BackgroundJob_Worker::doAllSteps(); -} -else{ +} else { // We call cron.php from some website - if( $appmode == 'cron' ) { + if ($appmode == 'cron') { // Cron is cron :-P - OC_JSON::error( array( 'data' => array( 'message' => 'Backgroundjobs are using system cron!'))); - } - else{ + OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!'))); + } else { // Work and success :-) OC_BackgroundJob_Worker::doNextStep(); OC_JSON::success(); From 7948341a86fb08d236bb53f8aece809ae10ba5f2 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 20 Apr 2013 23:27:46 +0200 Subject: [PATCH 02/15] Rework background job system --- apps/user_ldap/appinfo/app.php | 2 +- apps/user_ldap/lib/jobs.php | 19 ++- cron.php | 11 +- lib/backgroundjob/job.php | 45 +++++++ lib/backgroundjob/joblist.php | 158 +++++++++++++++++++++++ lib/backgroundjob/queuedjob.php | 28 ++++ lib/backgroundjob/queuedtask.php | 105 --------------- lib/backgroundjob/regulartask.php | 52 -------- lib/backgroundjob/timedjob.php | 41 ++++++ lib/backgroundjob/worker.php | 118 ----------------- lib/base.php | 2 +- lib/cache/fileglobalgc.php | 8 ++ lib/public/backgroundjob.php | 71 +--------- tests/lib/backgroundjob/dummyjoblist.php | 114 ++++++++++++++++ tests/lib/backgroundjob/queuedjob.php | 42 ++++++ tests/lib/backgroundjob/timedjob.php | 68 ++++++++++ 16 files changed, 530 insertions(+), 354 deletions(-) create mode 100644 lib/backgroundjob/job.php create mode 100644 lib/backgroundjob/joblist.php create mode 100644 lib/backgroundjob/queuedjob.php delete mode 100644 lib/backgroundjob/queuedtask.php delete mode 100644 lib/backgroundjob/regulartask.php create mode 100644 lib/backgroundjob/timedjob.php delete mode 100644 lib/backgroundjob/worker.php create mode 100644 lib/cache/fileglobalgc.php create mode 100644 tests/lib/backgroundjob/dummyjoblist.php create mode 100644 tests/lib/backgroundjob/queuedjob.php create mode 100644 tests/lib/backgroundjob/timedjob.php diff --git a/apps/user_ldap/appinfo/app.php b/apps/user_ldap/appinfo/app.php index 89410b5ef0..5c8b3ddfb6 100644 --- a/apps/user_ldap/appinfo/app.php +++ b/apps/user_ldap/appinfo/app.php @@ -49,7 +49,7 @@ $entry = array( 'name' => 'LDAP' ); -OCP\Backgroundjob::addRegularTask('OCA\user_ldap\lib\Jobs', 'updateGroups'); +OCP\Backgroundjob::registerJob('OCA\user_ldap\lib\Jobs'); if(OCP\App::isEnabled('user_webdavauth')) { OCP\Util::writeLog('user_ldap', 'user_ldap and user_webdavauth are incompatible. You may experience unexpected behaviour', diff --git a/apps/user_ldap/lib/jobs.php b/apps/user_ldap/lib/jobs.php index 094d11db3d..60ecc0da33 100644 --- a/apps/user_ldap/lib/jobs.php +++ b/apps/user_ldap/lib/jobs.php @@ -23,20 +23,22 @@ namespace OCA\user_ldap\lib; -class Jobs { +class Jobs extends \OC\BackgroundJob\TimedJob { static private $groupsFromDB; static private $groupBE; static private $connector; + public function __construct(){ + $this->interval = self::getRefreshInterval(); + } + + public function run($argument){ + Jobs::updateGroups(); + } + static public function updateGroups() { \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', \OCP\Util::DEBUG); - $lastUpdate = \OCP\Config::getAppValue('user_ldap', 'bgjUpdateGroupsLastRun', 0); - if((time() - $lastUpdate) < self::getRefreshInterval()) { - \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – last run too fresh, aborting.', \OCP\Util::DEBUG); - //komm runter Werner die Maurer geben ein aus - return; - } $knownGroups = array_keys(self::getKnownGroups()); $actualGroups = self::getGroupBE()->getGroups(); @@ -45,7 +47,6 @@ class Jobs { \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.', \OCP\Util::INFO); - \OCP\Config::setAppValue('user_ldap', 'bgjUpdateGroupsLastRun', time()); return; } @@ -53,8 +54,6 @@ class Jobs { self::handleCreatedGroups(array_diff($actualGroups, $knownGroups)); self::handleRemovedGroups(array_diff($knownGroups, $actualGroups)); - \OCP\Config::setAppValue('user_ldap', 'bgjUpdateGroupsLastRun', time()); - \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', \OCP\Util::DEBUG); } diff --git a/cron.php b/cron.php index 07ce45ac22..c8dd6fcc88 100644 --- a/cron.php +++ b/cron.php @@ -94,7 +94,11 @@ if (OC::$CLI) { touch(TemporaryCronClass::$lockfile); // Work - OC_BackgroundJob_Worker::doAllSteps(); + $jobList = new \OC\BackgroundJob\JobList(); + $jobs = $jobList->getAll(); + foreach ($jobs as $job) { + $job->execute($jobList); + } } else { // We call cron.php from some website if ($appmode == 'cron') { @@ -102,7 +106,10 @@ if (OC::$CLI) { OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!'))); } else { // Work and success :-) - OC_BackgroundJob_Worker::doNextStep(); + $jobList = new \OC\BackgroundJob\JobList(); + $job = $jobList->getNext(); + $job->execute($jobList); + $jobList->setLastJob($job); OC_JSON::success(); } } diff --git a/lib/backgroundjob/job.php b/lib/backgroundjob/job.php new file mode 100644 index 0000000000..ff647c7329 --- /dev/null +++ b/lib/backgroundjob/job.php @@ -0,0 +1,45 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\BackgroundJob; + +abstract class Job { + protected $id; + protected $lastRun; + protected $argument; + + /** + * @param JobList $jobList + */ + public function execute($jobList) { + $jobList->setLastRun($this); + $this->run($this->argument); + } + + abstract protected function run($argument); + + public function setId($id) { + $this->id = $id; + } + + public function setLastRun($lastRun) { + $this->lastRun = $lastRun; + } + + public function setArgument($argument) { + $this->argument = $argument; + } + + public function getId() { + return $this->id; + } + + public function getLastRun() { + return $this->lastRun(); + } +} diff --git a/lib/backgroundjob/joblist.php b/lib/backgroundjob/joblist.php new file mode 100644 index 0000000000..7471f84153 --- /dev/null +++ b/lib/backgroundjob/joblist.php @@ -0,0 +1,158 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\BackgroundJob; + +/** + * Class QueuedJob + * + * create a background job that is to be executed once + * + * @package OC\BackgroundJob + */ +class JobList { + /** + * @param Job|string $job + * @param mixed $argument + */ + public function add($job, $argument = null) { + if (!$this->has($job, $argument)) { + if ($job instanceof Job) { + $class = get_class($job); + } else { + $class = $job; + } + $argument = json_encode($argument); + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*jobs`(`class`, `argument`, `last_run`) VALUES(?, ?, 0)'); + $query->execute(array($class, $argument)); + } + } + + /** + * @param Job|string $job + * @param mixed $argument + */ + public function remove($job, $argument = null) { + if ($job instanceof Job) { + $class = get_class($job); + } else { + $class = $job; + } + if (!is_null($argument)) { + $argument = json_encode($argument); + $query = \OC_DB::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->execute(array($class)); + } + } + + /** + * check if a job is in the list + * + * @param $job + * @param mixed $argument + * @return bool + */ + public function has($job, $argument) { + if ($job instanceof Job) { + $class = get_class($job); + } else { + $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(); + } + + /** + * get all jobs in the list + * + * @return Job[] + */ + public function getAll() { + $query = \OC_DB::prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs`'); + $result = $query->execute(); + $jobs = array(); + while ($row = $result->fetchRow()) { + $jobs[] = $this->buildJob($row); + } + return $jobs; + } + + /** + * get the next job in the list + * + * @return Job + */ + 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()) { + 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()) { + return $this->buildJob($row); + } else { + return null; //empty job list + } + } + } + + /** + * get the job object from a row in the db + * + * @param array $row + * @return Job + */ + private function buildJob($row) { + $class = $row['class']; + /** + * @var Job $job + */ + $job = new $class(); + $job->setId($row['id']); + $job->setLastRun($row['last_run']); + $job->setArgument(json_decode($row['argument'])); + return $job; + } + + /** + * set the job that was last ran + * + * @param Job $job + */ + public function setLastJob($job) { + \OC_Appconfig::setValue('backgroundjob', 'lastjob', $job->getId()); + } + + /** + * get the id of the last ran job + * + * @return int + */ + public function getLastJob() { + return \OC_Appconfig::getValue('backgroundjob', 'lastjob', 0); + } + + /** + * set the lastRun of $job to now + * + * @param Job $job + */ + public function setLastRun($job) { + $query = \OC_DB::prepare('UPDATE `*PREFIX*jobs` SET `last_run` = ? WHERE `id` = ?'); + $query->execute(array(time(), $job->getId())); + } +} diff --git a/lib/backgroundjob/queuedjob.php b/lib/backgroundjob/queuedjob.php new file mode 100644 index 0000000000..1714182820 --- /dev/null +++ b/lib/backgroundjob/queuedjob.php @@ -0,0 +1,28 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\BackgroundJob; + +/** + * Class QueuedJob + * + * create a background job that is to be executed once + * + * @package OC\BackgroundJob + */ +abstract class QueuedJob extends Job { + /** + * run the job, then remove it from the joblist + * + * @param JobList $jobList + */ + public function execute($jobList) { + $jobList->remove($this); + $this->run($this->argument); + } +} diff --git a/lib/backgroundjob/queuedtask.php b/lib/backgroundjob/queuedtask.php deleted file mode 100644 index b2ce6f39ed..0000000000 --- a/lib/backgroundjob/queuedtask.php +++ /dev/null @@ -1,105 +0,0 @@ -. -* -*/ - -/** - * This class manages our queued tasks. - */ -class OC_BackgroundJob_QueuedTask{ - /** - * @brief Gets one queued task - * @param $id ID of the task - * @return associative array - */ - public static function find( $id ) { - $stmt = OC_DB::prepare( 'SELECT * FROM `*PREFIX*queuedtasks` WHERE `id` = ?' ); - $result = $stmt->execute(array($id)); - return $result->fetchRow(); - } - - /** - * @brief Gets all queued tasks - * @return array with associative arrays - */ - public static function all() { - // Array for objects - $return = array(); - - // Get Data - $stmt = OC_DB::prepare( 'SELECT * FROM `*PREFIX*queuedtasks`' ); - $result = $stmt->execute(array()); - while( $row = $result->fetchRow()) { - $return[] = $row; - } - - return $return; - } - - /** - * @brief Gets all queued tasks of a specific app - * @param $app app name - * @return array with associative arrays - */ - public static function whereAppIs( $app ) { - // Array for objects - $return = array(); - - // Get Data - $stmt = OC_DB::prepare( 'SELECT * FROM `*PREFIX*queuedtasks` WHERE `app` = ?' ); - $result = $stmt->execute(array($app)); - while( $row = $result->fetchRow()) { - $return[] = $row; - } - - // Und weg damit - return $return; - } - - /** - * @brief queues a task - * @param $app app name - * @param $klass class name - * @param $method method name - * @param $parameters all useful data as text - * @return id of task - */ - public static function add( $app, $klass, $method, $parameters ) { - $stmt = OC_DB::prepare( 'INSERT INTO `*PREFIX*queuedtasks` (`app`, `klass`, `method`, `parameters`)' - .' VALUES(?,?,?,?)' ); - $result = $stmt->execute(array($app, $klass, $method, $parameters )); - - return OC_DB::insertid(); - } - - /** - * @brief deletes a queued task - * @param $id id of task - * @return true/false - * - * Deletes a report - */ - public static function delete( $id ) { - $stmt = OC_DB::prepare( 'DELETE FROM `*PREFIX*queuedtasks` WHERE `id` = ?' ); - $result = $stmt->execute(array($id)); - - return true; - } -} diff --git a/lib/backgroundjob/regulartask.php b/lib/backgroundjob/regulartask.php deleted file mode 100644 index 9976872ee1..0000000000 --- a/lib/backgroundjob/regulartask.php +++ /dev/null @@ -1,52 +0,0 @@ -. -* -*/ - -/** - * This class manages the regular tasks. - */ -class OC_BackgroundJob_RegularTask{ - static private $registered = array(); - - /** - * @brief creates a regular task - * @param $klass class name - * @param $method method name - * @return true - */ - static public function register( $klass, $method ) { - // Create the data structure - self::$registered["$klass-$method"] = array( $klass, $method ); - - // No chance for failure ;-) - return true; - } - - /** - * @brief gets all regular tasks - * @return associative array - * - * key is string "$klass-$method", value is array( $klass, $method ) - */ - static public function all() { - return self::$registered; - } -} diff --git a/lib/backgroundjob/timedjob.php b/lib/backgroundjob/timedjob.php new file mode 100644 index 0000000000..ae9f33505a --- /dev/null +++ b/lib/backgroundjob/timedjob.php @@ -0,0 +1,41 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\BackgroundJob; + +/** + * Class QueuedJob + * + * create a background job that is to be executed at an interval + * + * @package OC\BackgroundJob + */ +abstract class TimedJob extends Job { + protected $interval = 0; + + /** + * set the interval for the job + * + * @param int $interval + */ + public function setInterval($interval) { + $this->interval = $interval; + } + + /** + * run the job if + * + * @param JobList $jobList + */ + public function execute($jobList) { + if ((time() - $this->lastRun) > $this->interval) { + $jobList->setLastRun($this); + $this->run($this->argument); + } + } +} diff --git a/lib/backgroundjob/worker.php b/lib/backgroundjob/worker.php deleted file mode 100644 index e966ac9647..0000000000 --- a/lib/backgroundjob/worker.php +++ /dev/null @@ -1,118 +0,0 @@ -. -* -*/ - -/** - * This class does the dirty work. - * - * TODO: locking in doAllSteps - */ -class OC_BackgroundJob_Worker{ - /** - * @brief executes all tasks - * @return boolean - * - * This method executes all regular tasks and then all queued tasks. - * This method should be called by cli scripts that do not let the user - * wait. - */ - public static function doAllSteps() { - // Do our regular work - $lasttask = OC_Appconfig::getValue( 'core', 'backgroundjobs_task', '' ); - - $regular_tasks = OC_BackgroundJob_RegularTask::all(); - ksort( $regular_tasks ); - foreach( $regular_tasks as $key => $value ) { - if( strcmp( $key, $lasttask ) > 0 ) { - // Set "restart here" config value - OC_Appconfig::setValue( 'core', 'backgroundjobs_task', $key ); - call_user_func( $value ); - } - } - // Reset "start here" config value - OC_Appconfig::setValue( 'core', 'backgroundjobs_task', '' ); - - // Do our queued tasks - $queued_tasks = OC_BackgroundJob_QueuedTask::all(); - foreach( $queued_tasks as $task ) { - OC_BackgroundJob_QueuedTask::delete( $task['id'] ); - call_user_func( array( $task['klass'], $task['method'] ), $task['parameters'] ); - } - - return true; - } - - /** - * @brief does a single task - * @return boolean - * - * This method executes one task. It saves the last state and continues - * with the next step. This method should be used by webcron and ajax - * services. - */ - public static function doNextStep() { - $laststep = OC_Appconfig::getValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); - - if( $laststep == 'regular_tasks' ) { - // get last app - $lasttask = OC_Appconfig::getValue( 'core', 'backgroundjobs_task', '' ); - - // What's the next step? - $regular_tasks = OC_BackgroundJob_RegularTask::all(); - ksort( $regular_tasks ); - $done = false; - - // search for next background job - foreach( $regular_tasks as $key => $value ) { - if( strcmp( $key, $lasttask ) > 0 ) { - OC_Appconfig::setValue( 'core', 'backgroundjobs_task', $key ); - $done = true; - call_user_func( $value ); - break; - } - } - - if( $done == false ) { - // Next time load queued tasks - OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'queued_tasks' ); - } - } - else{ - $tasks = OC_BackgroundJob_QueuedTask::all(); - if( count( $tasks )) { - $task = $tasks[0]; - // delete job before we execute it. This prevents endless loops - // of failing jobs. - OC_BackgroundJob_QueuedTask::delete($task['id']); - - // execute job - call_user_func( array( $task['klass'], $task['method'] ), $task['parameters'] ); - } - else{ - // Next time load queued tasks - OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); - OC_Appconfig::setValue( 'core', 'backgroundjobs_task', '' ); - } - } - - return true; - } -} diff --git a/lib/base.php b/lib/base.php index 7b0967df9f..fda65a221c 100644 --- a/lib/base.php +++ b/lib/base.php @@ -561,7 +561,7 @@ class OC { */ public static function registerCacheHooks() { // register cache cleanup jobs - OC_BackgroundJob_RegularTask::register('OC_Cache_FileGlobal', 'gc'); + \OCP\BackgroundJob::registerJob('OC_Cache_FileGlobalGC'); OC_Hook::connect('OC_User', 'post_login', 'OC_Cache_File', 'loginListener'); } diff --git a/lib/cache/fileglobalgc.php b/lib/cache/fileglobalgc.php new file mode 100644 index 0000000000..a29c31f906 --- /dev/null +++ b/lib/cache/fileglobalgc.php @@ -0,0 +1,8 @@ +add($job, $argument); } - /** - * @brief gets all regular tasks - * @return associative array - * - * key is string "$klass-$method", value is array( $klass, $method ) - */ - static public function allRegularTasks() { - return \OC_BackgroundJob_RegularTask::all(); - } - - /** - * @brief Gets one queued task - * @param $id ID of the task - * @return associative array - */ - public static function findQueuedTask( $id ) { - return \OC_BackgroundJob_QueuedTask::find( $id ); - } - - /** - * @brief Gets all queued tasks - * @return array with associative arrays - */ - public static function allQueuedTasks() { - return \OC_BackgroundJob_QueuedTask::all(); - } - - /** - * @brief Gets all queued tasks of a specific app - * @param $app app name - * @return array with associative arrays - */ - public static function queuedTaskWhereAppIs( $app ) { - return \OC_BackgroundJob_QueuedTask::whereAppIs( $app ); - } - - /** - * @brief queues a task - * @param $app app name - * @param $klass class name - * @param $method method name - * @param $parameters all useful data as text - * @return id of task - */ - public static function addQueuedTask( $app, $klass, $method, $parameters ) { - return \OC_BackgroundJob_QueuedTask::add( $app, $klass, $method, $parameters ); - } - - /** - * @brief deletes a queued task - * @param $id id of task - * @return true/false - * - * Deletes a report - */ - public static function deleteQueuedTask( $id ) { - return \OC_BackgroundJob_QueuedTask::delete( $id ); - } } diff --git a/tests/lib/backgroundjob/dummyjoblist.php b/tests/lib/backgroundjob/dummyjoblist.php new file mode 100644 index 0000000000..c4ccfc22c9 --- /dev/null +++ b/tests/lib/backgroundjob/dummyjoblist.php @@ -0,0 +1,114 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\BackgroundJob; + +class JobRun extends \Exception { +} + +/** + * Class DummyJobList + * + * in memory job list for testing purposes + */ +class DummyJobList extends \OC\BackgroundJob\JobList { + /** + * @var \OC\BackgroundJob\Job[] + */ + private $jobs = array(); + + private $last = 0; + + /** + * @param \OC\BackgroundJob\Job|string $job + */ + public function add($job) { + if (!$this->has($job)) { + $this->jobs[] = $job; + } + } + + /** + * @param \OC\BackgroundJob\Job|string $job + */ + public function remove($job) { + $index = array_search($job, $this->jobs); + if ($index !== false) { + unset($this->jobs[$index]); + } + } + + /** + * check if a job is in the list + * + * @param $job + * @return bool + */ + public function has($job) { + return array_search($job, $this->jobs) !== false; + } + + /** + * get all jobs in the list + * + * @return \OC\BackgroundJob\Job[] + */ + public function getAll() { + return $this->jobs; + } + + /** + * get the next job in the list + * + * @return \OC\BackgroundJob\Job + */ + 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 + */ + public function setLastJob($job) { + $i = array_search($job, $this->jobs); + if ($i !== false) { + $this->last = $i; + } else { + $this->last = 0; + } + } + + /** + * get the id of the last ran job + * + * @return int + */ + public function getLastJob() { + return $this->last; + } + + /** + * set the lastRun of $job to now + * + * @param \OC\BackgroundJob\Job $job + */ + public function setLastRun($job) { + $job->setLastRun(time()); + } +} diff --git a/tests/lib/backgroundjob/queuedjob.php b/tests/lib/backgroundjob/queuedjob.php new file mode 100644 index 0000000000..62d631a3ef --- /dev/null +++ b/tests/lib/backgroundjob/queuedjob.php @@ -0,0 +1,42 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\BackgroundJob; + +class TestQueuedJob extends \OC\BackgroundJob\QueuedJob { + public function run() { + throw new JobRun(); //throw an exception so we can detect if this function is called + } +} + +class QueuedJob extends \PHPUnit_Framework_TestCase { + /** + * @var DummyJobList $jobList + */ + private $jobList; + /** + * @var \OC\BackgroundJob\TimedJob $job + */ + private $job; + + public function setup() { + $this->jobList = new DummyJobList(); + $this->job = new TestQueuedJob(); + $this->jobList->add($this->job); + } + + public function testJobShouldBeRemoved() { + try { + $this->assertTrue($this->jobList->has($this->job)); + $this->job->execute($this->jobList); + $this->fail("job should have been run"); + } catch (JobRun $e) { + $this->assertFalse($this->jobList->has($this->job)); + } + } +} diff --git a/tests/lib/backgroundjob/timedjob.php b/tests/lib/backgroundjob/timedjob.php new file mode 100644 index 0000000000..4147b82cbb --- /dev/null +++ b/tests/lib/backgroundjob/timedjob.php @@ -0,0 +1,68 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\BackgroundJob; + +class TestTimedJob extends \OC\BackgroundJob\TimedJob { + public function __construct() { + $this->setInterval(10); + } + + public function run() { + throw new JobRun(); //throw an exception so we can detect if this function is called + } +} + +class TimedJob extends \PHPUnit_Framework_TestCase { + /** + * @var DummyJobList $jobList + */ + private $jobList; + /** + * @var \OC\BackgroundJob\TimedJob $job + */ + private $job; + + public function setup() { + $this->jobList = new DummyJobList(); + $this->job = new TestTimedJob(); + $this->jobList->add($this->job); + } + + public function testShouldRunAfterInterval() { + $this->job->setLastRun(time() - 12); + try { + $this->job->execute($this->jobList); + $this->fail("job should have run"); + } catch (JobRun $e) { + } + } + + public function testShouldNotRunWithinInterval() { + $this->job->setLastRun(time() - 5); + try { + $this->job->execute($this->jobList); + } catch (JobRun $e) { + $this->fail("job should not have run"); + } + } + + public function testShouldNotTwice() { + $this->job->setLastRun(time() - 15); + try { + $this->job->execute($this->jobList); + $this->fail("job should have run the first time"); + } catch (JobRun $e) { + try { + $this->job->execute($this->jobList); + } catch (JobRun $e) { + $this->fail("job should not have run the second time"); + } + } + } +} From 3aecfda0c052c1b7f7a4e3459f339a27298e32a7 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 20 Apr 2013 23:51:58 +0200 Subject: [PATCH 03/15] Adjust backgroundjob test cases --- tests/lib/backgroundjob/dummyjoblist.php | 12 ++++++++---- tests/lib/backgroundjob/queuedjob.php | 6 +++--- tests/lib/backgroundjob/timedjob.php | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/lib/backgroundjob/dummyjoblist.php b/tests/lib/backgroundjob/dummyjoblist.php index c4ccfc22c9..833607c15c 100644 --- a/tests/lib/backgroundjob/dummyjoblist.php +++ b/tests/lib/backgroundjob/dummyjoblist.php @@ -26,17 +26,20 @@ class DummyJobList extends \OC\BackgroundJob\JobList { /** * @param \OC\BackgroundJob\Job|string $job + * @param mixed $argument */ - public function add($job) { - if (!$this->has($job)) { + public function add($job, $argument = null) { + $job->setArgument($argument); + if (!$this->has($job, null)) { $this->jobs[] = $job; } } /** * @param \OC\BackgroundJob\Job|string $job + * @param mixed $argument */ - public function remove($job) { + public function remove($job, $argument = null) { $index = array_search($job, $this->jobs); if ($index !== false) { unset($this->jobs[$index]); @@ -47,9 +50,10 @@ class DummyJobList extends \OC\BackgroundJob\JobList { * check if a job is in the list * * @param $job + * @param mixed $argument * @return bool */ - public function has($job) { + public function has($job, $argument) { return array_search($job, $this->jobs) !== false; } diff --git a/tests/lib/backgroundjob/queuedjob.php b/tests/lib/backgroundjob/queuedjob.php index 62d631a3ef..1d373473cd 100644 --- a/tests/lib/backgroundjob/queuedjob.php +++ b/tests/lib/backgroundjob/queuedjob.php @@ -9,7 +9,7 @@ namespace Test\BackgroundJob; class TestQueuedJob extends \OC\BackgroundJob\QueuedJob { - public function run() { + public function run($argument) { throw new JobRun(); //throw an exception so we can detect if this function is called } } @@ -32,11 +32,11 @@ class QueuedJob extends \PHPUnit_Framework_TestCase { public function testJobShouldBeRemoved() { try { - $this->assertTrue($this->jobList->has($this->job)); + $this->assertTrue($this->jobList->has($this->job, null)); $this->job->execute($this->jobList); $this->fail("job should have been run"); } catch (JobRun $e) { - $this->assertFalse($this->jobList->has($this->job)); + $this->assertFalse($this->jobList->has($this->job, null)); } } } diff --git a/tests/lib/backgroundjob/timedjob.php b/tests/lib/backgroundjob/timedjob.php index 4147b82cbb..0af933afef 100644 --- a/tests/lib/backgroundjob/timedjob.php +++ b/tests/lib/backgroundjob/timedjob.php @@ -13,7 +13,7 @@ class TestTimedJob extends \OC\BackgroundJob\TimedJob { $this->setInterval(10); } - public function run() { + public function run($argument) { throw new JobRun(); //throw an exception so we can detect if this function is called } } From db0ea9780b3056a9161205588e55c4808648ec0a Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 21 Apr 2013 00:04:58 +0200 Subject: [PATCH 04/15] Add database table for backgroundjob --- db_structure.xml | 30 +++++++++++++++--------------- lib/util.php | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/db_structure.xml b/db_structure.xml index dce90697b1..3372be9f69 100644 --- a/db_structure.xml +++ b/db_structure.xml @@ -843,7 +843,7 @@ - *dbprefix*queuedtasks + *dbprefix*jobs @@ -858,35 +858,35 @@ - app + class text true - 255 + 256 - klass + argument text true - 255 + 256 - method - text + last_run + timestamp - true - 255 + false - - parameters - text - true - 255 - + + job_class_index + + class + ascending + + diff --git a/lib/util.php b/lib/util.php index 38453c1ce9..b3fab5041f 100755 --- a/lib/util.php +++ b/lib/util.php @@ -75,7 +75,7 @@ class OC_Util { public static function getVersion() { // hint: We only can count up. Reset minor/patchlevel when // updating major/minor version number. - return array(5, 80, 02); + return array(5, 80, 03); } /** From 07f510692cfa6dfb8357b7eb66e897a03fd20e3f Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 21 Apr 2013 00:08:55 +0200 Subject: [PATCH 05/15] Ensure we don't throw an exception before we can upgrade to the new backgroundjob system --- lib/base.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/base.php b/lib/base.php index fda65a221c..631f934154 100644 --- a/lib/base.php +++ b/lib/base.php @@ -561,7 +561,11 @@ class OC { */ public static function registerCacheHooks() { // register cache cleanup jobs - \OCP\BackgroundJob::registerJob('OC_Cache_FileGlobalGC'); + try { //if this is executed before the upgrade to the new backgroundjob system is completed it will throw an exception + \OCP\BackgroundJob::registerJob('OC_Cache_FileGlobalGC'); + } catch (Exception $e) { + + } OC_Hook::connect('OC_User', 'post_login', 'OC_Cache_File', 'loginListener'); } From 40de36a8f3dfba5dc6297adbd6c92d8b64c57190 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 21 Apr 2013 00:58:15 +0200 Subject: [PATCH 06/15] Try to supress pre-upgrade backgroundjob error --- lib/base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/base.php b/lib/base.php index 631f934154..3568f48644 100644 --- a/lib/base.php +++ b/lib/base.php @@ -562,7 +562,7 @@ class OC { public static function registerCacheHooks() { // register cache cleanup jobs try { //if this is executed before the upgrade to the new backgroundjob system is completed it will throw an exception - \OCP\BackgroundJob::registerJob('OC_Cache_FileGlobalGC'); + @\OCP\BackgroundJob::registerJob('OC_Cache_FileGlobalGC'); } catch (Exception $e) { } From e63633b5f300a020872d11659874c39bda292a44 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 22 Apr 2013 20:23:23 +0200 Subject: [PATCH 07/15] Don't try to use backgroundjobs before the installtion is done --- lib/base.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/base.php b/lib/base.php index 3568f48644..9246bbb5cd 100644 --- a/lib/base.php +++ b/lib/base.php @@ -560,13 +560,15 @@ class OC { * register hooks for the cache */ public static function registerCacheHooks() { - // register cache cleanup jobs - try { //if this is executed before the upgrade to the new backgroundjob system is completed it will throw an exception - @\OCP\BackgroundJob::registerJob('OC_Cache_FileGlobalGC'); - } catch (Exception $e) { + if (OC_Config::getValue('installed', false)) { //don't try to do this before we are properly setup + // register cache cleanup jobs + try { //if this is executed before the upgrade to the new backgroundjob system is completed it will throw an exception + \OCP\BackgroundJob::registerJob('OC_Cache_FileGlobalGC'); + } catch (Exception $e) { + } + OC_Hook::connect('OC_User', 'post_login', 'OC_Cache_File', 'loginListener'); } - OC_Hook::connect('OC_User', 'post_login', 'OC_Cache_File', 'loginListener'); } /** From d209cac2a14dd7c7ad2ca9f7df048227c9e5cdc0 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Tue, 23 Apr 2013 23:46:56 +0200 Subject: [PATCH 08/15] lastRun is not a method --- lib/backgroundjob/job.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/backgroundjob/job.php b/lib/backgroundjob/job.php index ff647c7329..aab3b8e4a6 100644 --- a/lib/backgroundjob/job.php +++ b/lib/backgroundjob/job.php @@ -40,6 +40,6 @@ abstract class Job { } public function getLastRun() { - return $this->lastRun(); + return $this->lastRun; } } From 117412272e34f73b9e9f69e1261b0d353dc12393 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 24 Apr 2013 14:06:24 +0200 Subject: [PATCH 09/15] Update documentation for \OCP\BackgroundJob --- lib/public/backgroundjob.php | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/public/backgroundjob.php b/lib/public/backgroundjob.php index 785a9408aa..9a58bd3965 100644 --- a/lib/public/backgroundjob.php +++ b/lib/public/backgroundjob.php @@ -29,21 +29,16 @@ namespace OCP; /** - * This class provides functions to manage backgroundjobs in ownCloud + * This class provides functions to register backgroundjobs in ownCloud * - * There are two kind of background jobs in ownCloud: regular tasks and - * queued tasks. + * To create a new backgroundjob create a new class that inharits 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. * - * Regular tasks have to be registered in appinfo.php and - * will run on a regular base. Fetching news could be a task that should run - * frequently. - * - * Queued tasks have to be registered each time you want to execute them. - * An example of the queued task would be the creation of the thumbnail. As - * soon as the user uploads a picture the gallery app registers the queued - * task "create thumbnail" and saves the path in the parameter instead of doing - * the work right away. This makes the app more responsive. As soon as the task - * is done it will be deleted from the list. + * A regular Job will be executed every time cron.php is run, a QueuedJob will only run once and a TimedJob + * will only run at a specific interval which is to be specified in the constructor of the job by calling + * $this->setInterval($interval) with $interval in seconds. */ class BackgroundJob { /** @@ -77,5 +72,4 @@ class BackgroundJob { $jobList = new \OC\BackgroundJob\JobList(); $jobList->add($job, $argument); } - } From b31dc10c3c4b73b08c926751effbade601c7cab8 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 24 Apr 2013 14:38:41 +0200 Subject: [PATCH 10/15] Add support for the old public backgroundjob api --- lib/backgroundjob/job.php | 4 + lib/backgroundjob/joblist.php | 14 ++ lib/public/backgroundjob.php | 180 ++++++++++++++++++++--- tests/lib/backgroundjob/dummyjoblist.php | 13 ++ 4 files changed, 188 insertions(+), 23 deletions(-) diff --git a/lib/backgroundjob/job.php b/lib/backgroundjob/job.php index aab3b8e4a6..49fbffbd68 100644 --- a/lib/backgroundjob/job.php +++ b/lib/backgroundjob/job.php @@ -42,4 +42,8 @@ abstract class Job { public function getLastRun() { return $this->lastRun; } + + public function getArgument() { + return $this->argument; + } } diff --git a/lib/backgroundjob/joblist.php b/lib/backgroundjob/joblist.php index 7471f84153..cc803dd9b5 100644 --- a/lib/backgroundjob/joblist.php +++ b/lib/backgroundjob/joblist.php @@ -110,6 +110,20 @@ class JobList { } } + /** + * @param int $id + * @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()) { + return $this->buildJob($row); + } else { + return null; + } + } + /** * get the job object from a row in the db * diff --git a/lib/public/backgroundjob.php b/lib/public/backgroundjob.php index 9a58bd3965..aa6c59067e 100644 --- a/lib/public/backgroundjob.php +++ b/lib/public/backgroundjob.php @@ -1,24 +1,24 @@ . -* -*/ + * ownCloud + * + * @author Jakob Sack + * @copyright 2012 Jakob Sack owncloud@jakobsack.de + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see . + * + */ /** * Public interface of ownCloud forbackground jobs. @@ -28,6 +28,8 @@ // This means that they should be used by apps instead of the internal ownCloud classes namespace OCP; +use \OC\BackgroundJob\JobList; + /** * This class provides functions to register backgroundjobs in ownCloud * @@ -60,16 +62,148 @@ class BackgroundJob { * This method sets the execution type of the background jobs. Possible types * are "none", "ajax", "webcron", "cron" */ - public static function setExecutionType( $type ) { - return \OC_BackgroundJob::setExecutionType( $type ); + public static function setExecutionType($type) { + return \OC_BackgroundJob::setExecutionType($type); } /** * @param \OC\BackgroundJob\Job|string $job * @param mixed $argument */ - public static function registerJob($job, $argument = null){ - $jobList = new \OC\BackgroundJob\JobList(); + public static function registerJob($job, $argument = null) { + $jobList = new JobList(); $jobList->add($job, $argument); } + + /** + * @deprecated + * @brief creates a regular task + * @param string $klass class name + * @param string $method method name + * @return true + */ + public static function addRegularTask($klass, $method) { + self::registerJob('RegularLegacyJob', array($klass, $method)); + return true; + } + + /** + * @deprecated + * @brief gets all regular tasks + * @return associative array + * + * key is string "$klass-$method", value is array( $klass, $method ) + */ + static public function allRegularTasks() { + $jobList = new JobList(); + $allJobs = $jobList->getAll(); + $regularJobs = array(); + foreach ($allJobs as $job) { + if ($job instanceof RegularLegacyJob) { + $key = implode('-', $job->getArgument()); + $regularJobs[$key] = $job->getArgument(); + } + } + return $regularJobs; + } + + /** + * @deprecated + * @brief Gets one queued task + * @param int $id ID of the task + * @return associative array + */ + public static function findQueuedTask($id) { + $jobList = new JobList(); + return $jobList->getById($id); + } + + /** + * @deprecated + * @brief Gets all queued tasks + * @return array with associative arrays + */ + public static function allQueuedTasks() { + $jobList = new JobList(); + $allJobs = $jobList->getAll(); + $queuedJobs = array(); + foreach ($allJobs as $job) { + if ($job instanceof QueuedLegacyJob) { + $queuedJob = $job->getArgument(); + $queuedJob['id'] = $job->getId(); + $queuedJobs[] = $queuedJob; + } + } + return $queuedJobs; + } + + /** + * @deprecated + * @brief Gets all queued tasks of a specific app + * @param string $app app name + * @return array with associative arrays + */ + public static function queuedTaskWhereAppIs($app) { + $jobList = new JobList(); + $allJobs = $jobList->getAll(); + $queuedJobs = array(); + foreach ($allJobs as $job) { + if ($job instanceof QueuedLegacyJob) { + $queuedJob = $job->getArgument(); + $queuedJob['id'] = $job->getId(); + if ($queuedJob['app'] === $app) { + $queuedJobs[] = $queuedJob; + } + } + } + return $queuedJobs; + } + + /** + * @deprecated + * @brief queues a task + * @param string $app app name + * @param string $class class name + * @param string $method method name + * @param string $parameters all useful data as text + * @return int id of task + */ + public static function addQueuedTask($app, $class, $method, $parameters) { + self::registerJob('QueuedLegacyJob', array('app' => $app, 'klass' => $class, 'method' => $method, 'parameters' => $parameters)); + return true; + } + + /** + * @deprecated + * @brief deletes a queued task + * @param int $id id of task + * @return bool + * + * Deletes a report + */ + public static function deleteQueuedTask($id) { + $jobList = new JobList(); + $job = $jobList->getById($id); + if ($job) { + $jobList->remove($job); + } + } +} + +/** + * Wrappers to support old versions of the BackgroundJob api + */ +class RegularLegacyJob extends \OC\BackgroundJob\Job { + public function run($argument) { + call_user_func($argument); + } +} + +class QueuedLegacyJob extends \OC\BackgroundJob\QueuedJob { + public function run($argument) { + $class = $argument['klass']; + $method = $argument['method']; + $parameters = $argument['parameters']; + call_user_func(array($class, $method), $parameters); + } } diff --git a/tests/lib/backgroundjob/dummyjoblist.php b/tests/lib/backgroundjob/dummyjoblist.php index 833607c15c..d91d6b344b 100644 --- a/tests/lib/backgroundjob/dummyjoblist.php +++ b/tests/lib/backgroundjob/dummyjoblist.php @@ -98,6 +98,19 @@ class DummyJobList extends \OC\BackgroundJob\JobList { } } + /** + * @param int $id + * @return Job + */ + public function getById($id) { + foreach ($this->jobs as $job) { + if ($job->getId() === $id) { + return $job; + } + } + return null; + } + /** * get the id of the last ran job * From b7585050b565ac8f42c3afa625037adaf1a8d92c Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 3 May 2013 16:42:48 +0200 Subject: [PATCH 11/15] Move legacy backgroundjob classes to a place where they can be autoloaded --- lib/backgroundjob/legacy/queuedjob.php | 18 ++++++++++++++++++ lib/backgroundjob/legacy/regularjob.php | 15 +++++++++++++++ lib/public/backgroundjob.php | 24 +++--------------------- 3 files changed, 36 insertions(+), 21 deletions(-) create mode 100644 lib/backgroundjob/legacy/queuedjob.php create mode 100644 lib/backgroundjob/legacy/regularjob.php diff --git a/lib/backgroundjob/legacy/queuedjob.php b/lib/backgroundjob/legacy/queuedjob.php new file mode 100644 index 0000000000..2bc001103b --- /dev/null +++ b/lib/backgroundjob/legacy/queuedjob.php @@ -0,0 +1,18 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\BackgroundJob\Legacy; + +class QueuedJob extends \OC\BackgroundJob\QueuedJob { + public function run($argument) { + $class = $argument['klass']; + $method = $argument['method']; + $parameters = $argument['parameters']; + call_user_func(array($class, $method), $parameters); + } +} diff --git a/lib/backgroundjob/legacy/regularjob.php b/lib/backgroundjob/legacy/regularjob.php new file mode 100644 index 0000000000..d4cfa348ce --- /dev/null +++ b/lib/backgroundjob/legacy/regularjob.php @@ -0,0 +1,15 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\BackgroundJob\Legacy; + +class RegularJob extends \OC\BackgroundJob\Job { + public function run($argument) { + call_user_func($argument); + } +} diff --git a/lib/public/backgroundjob.php b/lib/public/backgroundjob.php index aa6c59067e..cc076a3a84 100644 --- a/lib/public/backgroundjob.php +++ b/lib/public/backgroundjob.php @@ -21,7 +21,7 @@ */ /** - * Public interface of ownCloud forbackground jobs. + * Public interface of ownCloud for background jobs. */ // use OCP namespace for all classes that are considered public. @@ -83,7 +83,7 @@ class BackgroundJob { * @return true */ public static function addRegularTask($klass, $method) { - self::registerJob('RegularLegacyJob', array($klass, $method)); + self::registerJob('OC\BackgroundJob\Legacy\RegularJob', array($klass, $method)); return true; } @@ -169,7 +169,7 @@ class BackgroundJob { * @return int id of task */ public static function addQueuedTask($app, $class, $method, $parameters) { - self::registerJob('QueuedLegacyJob', array('app' => $app, 'klass' => $class, 'method' => $method, 'parameters' => $parameters)); + self::registerJob('OC\BackgroundJob\Legacy\QueuedJob', array('app' => $app, 'klass' => $class, 'method' => $method, 'parameters' => $parameters)); return true; } @@ -189,21 +189,3 @@ class BackgroundJob { } } } - -/** - * Wrappers to support old versions of the BackgroundJob api - */ -class RegularLegacyJob extends \OC\BackgroundJob\Job { - public function run($argument) { - call_user_func($argument); - } -} - -class QueuedLegacyJob extends \OC\BackgroundJob\QueuedJob { - public function run($argument) { - $class = $argument['klass']; - $method = $argument['method']; - $parameters = $argument['parameters']; - call_user_func(array($class, $method), $parameters); - } -} From e1b5598f00b45484bf69e414c5a2cc19ebdc9ab0 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 2 Jun 2013 21:21:39 +0200 Subject: [PATCH 12/15] Files: use public api to register backgroundjob --- apps/files/appinfo/app.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/files/appinfo/app.php b/apps/files/appinfo/app.php index 05ab1722b3..99739cb4ce 100644 --- a/apps/files/appinfo/app.php +++ b/apps/files/appinfo/app.php @@ -20,4 +20,4 @@ OC_Search::registerProvider('OC_Search_Provider_File'); \OC_Hook::connect('OC_Filesystem', 'post_delete', '\OC\Files\Cache\Updater', 'deleteHook'); \OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Updater', 'renameHook'); -\OC_BackgroundJob_RegularTask::register('\OC\Files\Cache\BackgroundWatcher', 'checkNext'); +\OCP\BackgroundJob::addRegularTask('\OC\Files\Cache\BackgroundWatcher', 'checkNext'); From b5e817d638afcbdb86f6f8ffd6e92348c2454d3e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 2 Jun 2013 21:44:24 +0200 Subject: [PATCH 13/15] fix clearing mounts when filesystem isn't initialized yet --- lib/files/filesystem.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/files/filesystem.php b/lib/files/filesystem.php index 02cce001b4..7e0bcfd1a2 100644 --- a/lib/files/filesystem.php +++ b/lib/files/filesystem.php @@ -359,7 +359,9 @@ class Filesystem { * clear all mounts and storage backends */ public static function clearMounts() { - self::$mounts->clear(); + if (self::$mounts) { + self::$mounts->clear(); + } } /** From 38bd234686d206013d1f402c89e310503a65a5a8 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 2 Jun 2013 22:09:44 +0200 Subject: [PATCH 14/15] make sure the filesystem is setup before doing mount operations --- lib/files/filesystem.php | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/files/filesystem.php b/lib/files/filesystem.php index 7e0bcfd1a2..d3fddf8c42 100644 --- a/lib/files/filesystem.php +++ b/lib/files/filesystem.php @@ -152,6 +152,9 @@ class Filesystem { * @return string */ static public function getMountPoint($path) { + if (!self::$mounts) { + \OC_Util::setupFS(); + } $mount = self::$mounts->find($path); if ($mount) { return $mount->getMountPoint(); @@ -167,6 +170,9 @@ class Filesystem { * @return string[] */ static public function getMountPoints($path) { + if (!self::$mounts) { + \OC_Util::setupFS(); + } $result = array(); $mounts = self::$mounts->findIn($path); foreach ($mounts as $mount) { @@ -182,6 +188,9 @@ class Filesystem { * @return \OC\Files\Storage\Storage */ public static function getStorage($mountPoint) { + if (!self::$mounts) { + \OC_Util::setupFS(); + } $mount = self::$mounts->find($mountPoint); return $mount->getStorage(); } @@ -191,6 +200,9 @@ class Filesystem { * @return Mount\Mount[] */ public static function getMountByStorageId($id) { + if (!self::$mounts) { + \OC_Util::setupFS(); + } return self::$mounts->findByStorageId($id); } @@ -199,6 +211,9 @@ class Filesystem { * @return Mount\Mount[] */ public static function getMountByNumericId($id) { + if (!self::$mounts) { + \OC_Util::setupFS(); + } return self::$mounts->findByNumericId($id); } @@ -209,6 +224,9 @@ class Filesystem { * @return array consisting of the storage and the internal path */ static public function resolvePath($path) { + if (!self::$mounts) { + \OC_Util::setupFS(); + } $mount = self::$mounts->find($path); if ($mount) { return array($mount->getStorage(), $mount->getInternalPath($path)); @@ -223,7 +241,7 @@ class Filesystem { } self::$defaultInstance = new View($root); - if(!self::$mounts) { + if (!self::$mounts) { self::$mounts = new Mount\Manager(); } @@ -235,8 +253,8 @@ class Filesystem { return true; } - static public function initMounts(){ - if(!self::$mounts) { + static public function initMounts() { + if (!self::$mounts) { self::$mounts = new Mount\Manager(); } } @@ -372,6 +390,9 @@ class Filesystem { * @param string $mountpoint */ static public function mount($class, $arguments, $mountpoint) { + if (!self::$mounts) { + \OC_Util::setupFS(); + } $mount = new Mount\Mount($class, $mountpoint, $arguments); self::$mounts->addMount($mount); } From 24ab525669e177e37266d5b8a052e4e25990ebf0 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 2 Jun 2013 22:49:54 +0200 Subject: [PATCH 15/15] Use int for last_run --- db_structure.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db_structure.xml b/db_structure.xml index feb7c0d122..933b09988f 100644 --- a/db_structure.xml +++ b/db_structure.xml @@ -891,7 +891,7 @@ last_run - timestamp + integer false