1
0

BackgroundJob.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Felix Moeller <mail@felixmoeller.de>
  7. * @author Jakob Sack <mail@jakobsack.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Robin McCorkell <robin@mccorkell.me.uk>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. /**
  30. * Public interface of ownCloud for background jobs.
  31. */
  32. // use OCP namespace for all classes that are considered public.
  33. // This means that they should be used by apps instead of the internal ownCloud classes
  34. namespace OCP;
  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. * @since 4.5.0
  47. */
  48. class BackgroundJob {
  49. /**
  50. * get the execution type of background jobs
  51. *
  52. * @return string
  53. *
  54. * This method returns the type how background jobs are executed. If the user
  55. * did not select something, the type is ajax.
  56. * @since 5.0.0
  57. */
  58. public static function getExecutionType() {
  59. return \OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax');
  60. }
  61. /**
  62. * sets the background jobs execution type
  63. *
  64. * @param string $type execution type
  65. * @return false|null
  66. *
  67. * This method sets the execution type of the background jobs. Possible types
  68. * are "none", "ajax", "webcron", "cron"
  69. * @since 5.0.0
  70. */
  71. public static function setExecutionType($type) {
  72. if( !in_array( $type, array('none', 'ajax', 'webcron', 'cron'))) {
  73. return false;
  74. }
  75. \OC::$server->getConfig()->setAppValue('core', 'backgroundjobs_mode', $type);
  76. }
  77. /**
  78. * @param string $job
  79. * @param mixed $argument
  80. * @deprecated 8.1.0 Use \OC::$server->getJobList()->add() instead
  81. * @since 6.0.0
  82. */
  83. public static function registerJob($job, $argument = null) {
  84. $jobList = \OC::$server->getJobList();
  85. $jobList->add($job, $argument);
  86. }
  87. /**
  88. * @deprecated 6.0.0
  89. * creates a regular task
  90. * @param string $klass class name
  91. * @param string $method method name
  92. * @return boolean|null
  93. * @since 4.5.0
  94. */
  95. public static function addRegularTask($klass, $method) {
  96. if (!\OCP\Util::needUpgrade()) {
  97. self::registerJob('OC\BackgroundJob\Legacy\RegularJob', array($klass, $method));
  98. return true;
  99. }
  100. }
  101. /**
  102. * @deprecated 6.0.0
  103. * gets all regular tasks
  104. * @return array
  105. *
  106. * key is string "$klass-$method", value is array( $klass, $method )
  107. * @since 4.5.0
  108. */
  109. static public function allRegularTasks() {
  110. return [];
  111. }
  112. /**
  113. * @deprecated 6.0.0
  114. * Gets one queued task
  115. * @param int $id ID of the task
  116. * @return BackgroundJob\IJob|null
  117. * @since 4.5.0
  118. */
  119. public static function findQueuedTask($id) {
  120. $jobList = \OC::$server->getJobList();
  121. return $jobList->getById($id);
  122. }
  123. /**
  124. * @deprecated 6.0.0
  125. * Gets all queued tasks
  126. * @return array an array of associative arrays
  127. * @since 4.5.0
  128. */
  129. public static function allQueuedTasks() {
  130. return [];
  131. }
  132. /**
  133. * @deprecated 6.0.0
  134. * Gets all queued tasks of a specific app
  135. * @param string $app app name
  136. * @return array an array of associative arrays
  137. * @since 4.5.0
  138. */
  139. public static function queuedTaskWhereAppIs($app) {
  140. return [];
  141. }
  142. /**
  143. * @deprecated 6.0.0
  144. * queues a task
  145. * @param string $app app name
  146. * @param string $class class name
  147. * @param string $method method name
  148. * @param string $parameters all useful data as text
  149. * @return boolean id of task
  150. * @since 4.5.0
  151. */
  152. public static function addQueuedTask($app, $class, $method, $parameters) {
  153. self::registerJob('OC\BackgroundJob\Legacy\QueuedJob', array('app' => $app, 'klass' => $class, 'method' => $method, 'parameters' => $parameters));
  154. return true;
  155. }
  156. /**
  157. * @deprecated 6.0.0
  158. * deletes a queued task
  159. * @param int $id id of task
  160. * @return boolean|null
  161. *
  162. * Deletes a report
  163. * @since 4.5.0
  164. */
  165. public static function deleteQueuedTask($id) {
  166. $jobList = \OC::$server->getJobList();
  167. $job = $jobList->getById($id);
  168. if ($job) {
  169. $jobList->remove($job);
  170. }
  171. }
  172. }