backgroundjob.php 5.4 KB

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