backgroundjob.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Felix Moeller <mail@felixmoeller.de>
  5. * @author Jakob Sack <mail@jakobsack.de>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <icewind@owncloud.com>
  9. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  10. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  11. *
  12. * @copyright Copyright (c) 2015, ownCloud, Inc.
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. /**
  29. * Public interface of ownCloud for background jobs.
  30. */
  31. // use OCP namespace for all classes that are considered public.
  32. // This means that they should be used by apps instead of the internal ownCloud classes
  33. namespace OCP;
  34. use \OC\BackgroundJob\JobList;
  35. /**
  36. * This class provides functions to register backgroundjobs in ownCloud
  37. *
  38. * To create a new backgroundjob create a new class that inherits from either \OC\BackgroundJob\Job,
  39. * \OC\BackgroundJob\QueuedJob or \OC\BackgroundJob\TimedJob and register it using
  40. * \OCP\BackgroundJob->registerJob($job, $argument), $argument will be passed to the run() function
  41. * of the job when the job is executed.
  42. *
  43. * A regular Job will be executed every time cron.php is run, a QueuedJob will only run once and a TimedJob
  44. * will only run at a specific interval which is to be specified in the constructor of the job by calling
  45. * $this->setInterval($interval) with $interval in seconds.
  46. */
  47. class BackgroundJob {
  48. /**
  49. * get the execution type of background jobs
  50. *
  51. * @return string
  52. *
  53. * This method returns the type how background jobs are executed. If the user
  54. * did not select something, the type is ajax.
  55. */
  56. public static function getExecutionType() {
  57. return \OC_BackgroundJob::getExecutionType();
  58. }
  59. /**
  60. * sets the background jobs execution type
  61. *
  62. * @param string $type execution type
  63. * @return false|null
  64. *
  65. * This method sets the execution type of the background jobs. Possible types
  66. * are "none", "ajax", "webcron", "cron"
  67. */
  68. public static function setExecutionType($type) {
  69. return \OC_BackgroundJob::setExecutionType($type);
  70. }
  71. /**
  72. * @param string $job
  73. * @param mixed $argument
  74. */
  75. public static function registerJob($job, $argument = null) {
  76. $jobList = \OC::$server->getJobList();
  77. $jobList->add($job, $argument);
  78. }
  79. /**
  80. * @deprecated
  81. * creates a regular task
  82. * @param string $klass class name
  83. * @param string $method method name
  84. * @return boolean|null
  85. */
  86. public static function addRegularTask($klass, $method) {
  87. if (!\OC::needUpgrade()) {
  88. self::registerJob('OC\BackgroundJob\Legacy\RegularJob', array($klass, $method));
  89. return true;
  90. }
  91. }
  92. /**
  93. * @deprecated
  94. * gets all regular tasks
  95. * @return array
  96. *
  97. * key is string "$klass-$method", value is array( $klass, $method )
  98. */
  99. static public function allRegularTasks() {
  100. $jobList = \OC::$server->getJobList();
  101. $allJobs = $jobList->getAll();
  102. $regularJobs = array();
  103. foreach ($allJobs as $job) {
  104. if ($job instanceof RegularLegacyJob) {
  105. $key = implode('-', $job->getArgument());
  106. $regularJobs[$key] = $job->getArgument();
  107. }
  108. }
  109. return $regularJobs;
  110. }
  111. /**
  112. * @deprecated
  113. * Gets one queued task
  114. * @param int $id ID of the task
  115. * @return BackgroundJob\IJob|null
  116. */
  117. public static function findQueuedTask($id) {
  118. $jobList = \OC::$server->getJobList();
  119. return $jobList->getById($id);
  120. }
  121. /**
  122. * @deprecated
  123. * Gets all queued tasks
  124. * @return array an array of associative arrays
  125. */
  126. public static function allQueuedTasks() {
  127. $jobList = \OC::$server->getJobList();
  128. $allJobs = $jobList->getAll();
  129. $queuedJobs = array();
  130. foreach ($allJobs as $job) {
  131. if ($job instanceof QueuedLegacyJob) {
  132. $queuedJob = $job->getArgument();
  133. $queuedJob['id'] = $job->getId();
  134. $queuedJobs[] = $queuedJob;
  135. }
  136. }
  137. return $queuedJobs;
  138. }
  139. /**
  140. * @deprecated
  141. * Gets all queued tasks of a specific app
  142. * @param string $app app name
  143. * @return array an array of associative arrays
  144. */
  145. public static function queuedTaskWhereAppIs($app) {
  146. $jobList = \OC::$server->getJobList();
  147. $allJobs = $jobList->getAll();
  148. $queuedJobs = array();
  149. foreach ($allJobs as $job) {
  150. if ($job instanceof QueuedLegacyJob) {
  151. $queuedJob = $job->getArgument();
  152. $queuedJob['id'] = $job->getId();
  153. if ($queuedJob['app'] === $app) {
  154. $queuedJobs[] = $queuedJob;
  155. }
  156. }
  157. }
  158. return $queuedJobs;
  159. }
  160. /**
  161. * @deprecated
  162. * queues a task
  163. * @param string $app app name
  164. * @param string $class class name
  165. * @param string $method method name
  166. * @param string $parameters all useful data as text
  167. * @return boolean id of task
  168. */
  169. public static function addQueuedTask($app, $class, $method, $parameters) {
  170. self::registerJob('OC\BackgroundJob\Legacy\QueuedJob', array('app' => $app, 'klass' => $class, 'method' => $method, 'parameters' => $parameters));
  171. return true;
  172. }
  173. /**
  174. * @deprecated
  175. * deletes a queued task
  176. * @param int $id id of task
  177. * @return boolean|null
  178. *
  179. * Deletes a report
  180. */
  181. public static function deleteQueuedTask($id) {
  182. $jobList = \OC::$server->getJobList();
  183. $job = $jobList->getById($id);
  184. if ($job) {
  185. $jobList->remove($job);
  186. }
  187. }
  188. }