summaryrefslogtreecommitdiffstats
path: root/lib/public/backgroundjob.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/public/backgroundjob.php')
-rw-r--r--lib/public/backgroundjob.php169
1 files changed, 110 insertions, 59 deletions
diff --git a/lib/public/backgroundjob.php b/lib/public/backgroundjob.php
index 601046fe691..cc076a3a845 100644
--- a/lib/public/backgroundjob.php
+++ b/lib/public/backgroundjob.php
@@ -1,49 +1,46 @@
<?php
/**
-* 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 <http://www.gnu.org/licenses/>.
-*
-*/
+ * 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 <http://www.gnu.org/licenses/>.
+ *
+ */
/**
- * Public interface of ownCloud forbackground jobs.
+ * Public interface of ownCloud for background jobs.
*/
// use OCP namespace for all classes that are considered public.
// 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 manage backgroundjobs in ownCloud
- *
- * There are two kind of background jobs in ownCloud: regular tasks and
- * queued tasks.
+ * This class provides functions to register backgroundjobs in ownCloud
*
- * 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.
+ * 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.
*
- * 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 {
/**
@@ -59,82 +56,136 @@ class BackgroundJob {
/**
* @brief sets the background jobs execution type
- * @param $type execution type
+ * @param string $type execution type
* @return boolean
*
* 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 JobList();
+ $jobList->add($job, $argument);
}
/**
+ * @deprecated
* @brief creates a regular task
- * @param $klass class name
- * @param $method method name
+ * @param string $klass class name
+ * @param string $method method name
* @return true
*/
- public static function addRegularTask( $klass, $method ) {
- return \OC_BackgroundJob_RegularTask::register( $klass, $method );
+ public static function addRegularTask($klass, $method) {
+ self::registerJob('OC\BackgroundJob\Legacy\RegularJob', 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() {
- return \OC_BackgroundJob_RegularTask::all();
+ $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 $id ID of the task
+ * @param int $id ID of the task
* @return associative array
*/
- public static function findQueuedTask( $id ) {
- return \OC_BackgroundJob_QueuedTask::find( $id );
+ 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() {
- return \OC_BackgroundJob_QueuedTask::all();
+ $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 $app app name
+ * @param string $app app name
* @return array with associative arrays
*/
- public static function queuedTaskWhereAppIs( $app ) {
- return \OC_BackgroundJob_QueuedTask::whereAppIs( $app );
+ 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 $app app name
- * @param $klass class name
- * @param $method method name
- * @param $parameters all useful data as text
- * @return id of 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, $klass, $method, $parameters ) {
- return \OC_BackgroundJob_QueuedTask::add( $app, $klass, $method, $parameters );
+ public static function addQueuedTask($app, $class, $method, $parameters) {
+ self::registerJob('OC\BackgroundJob\Legacy\QueuedJob', array('app' => $app, 'klass' => $class, 'method' => $method, 'parameters' => $parameters));
+ return true;
}
/**
+ * @deprecated
* @brief deletes a queued task
- * @param $id id of task
- * @return true/false
+ * @param int $id id of task
+ * @return bool
*
* Deletes a report
*/
- public static function deleteQueuedTask( $id ) {
- return \OC_BackgroundJob_QueuedTask::delete( $id );
+ public static function deleteQueuedTask($id) {
+ $jobList = new JobList();
+ $job = $jobList->getById($id);
+ if ($job) {
+ $jobList->remove($job);
+ }
}
}