backgroundjob.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 inharits 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. * @brief get the execution type of background jobs
  44. * @return string
  45. *
  46. * This method returns the type how background jobs are executed. If the user
  47. * did not select something, the type is ajax.
  48. */
  49. public static function getExecutionType() {
  50. return \OC_BackgroundJob::getExecutionType();
  51. }
  52. /**
  53. * @brief sets the background jobs execution type
  54. * @param string $type execution type
  55. * @return boolean
  56. *
  57. * This method sets the execution type of the background jobs. Possible types
  58. * are "none", "ajax", "webcron", "cron"
  59. */
  60. public static function setExecutionType($type) {
  61. return \OC_BackgroundJob::setExecutionType($type);
  62. }
  63. /**
  64. * @param \OC\BackgroundJob\Job|string $job
  65. * @param mixed $argument
  66. */
  67. public static function registerJob($job, $argument = null) {
  68. $jobList = new JobList();
  69. $jobList->add($job, $argument);
  70. }
  71. /**
  72. * @deprecated
  73. * @brief creates a regular task
  74. * @param string $klass class name
  75. * @param string $method method name
  76. * @return true
  77. */
  78. public static function addRegularTask($klass, $method) {
  79. self::registerJob('OC\BackgroundJob\Legacy\RegularJob', array($klass, $method));
  80. return true;
  81. }
  82. /**
  83. * @deprecated
  84. * @brief gets all regular tasks
  85. * @return associative array
  86. *
  87. * key is string "$klass-$method", value is array( $klass, $method )
  88. */
  89. static public function allRegularTasks() {
  90. $jobList = new JobList();
  91. $allJobs = $jobList->getAll();
  92. $regularJobs = array();
  93. foreach ($allJobs as $job) {
  94. if ($job instanceof RegularLegacyJob) {
  95. $key = implode('-', $job->getArgument());
  96. $regularJobs[$key] = $job->getArgument();
  97. }
  98. }
  99. return $regularJobs;
  100. }
  101. /**
  102. * @deprecated
  103. * @brief Gets one queued task
  104. * @param int $id ID of the task
  105. * @return associative array
  106. */
  107. public static function findQueuedTask($id) {
  108. $jobList = new JobList();
  109. return $jobList->getById($id);
  110. }
  111. /**
  112. * @deprecated
  113. * @brief Gets all queued tasks
  114. * @return array with associative arrays
  115. */
  116. public static function allQueuedTasks() {
  117. $jobList = new JobList();
  118. $allJobs = $jobList->getAll();
  119. $queuedJobs = array();
  120. foreach ($allJobs as $job) {
  121. if ($job instanceof QueuedLegacyJob) {
  122. $queuedJob = $job->getArgument();
  123. $queuedJob['id'] = $job->getId();
  124. $queuedJobs[] = $queuedJob;
  125. }
  126. }
  127. return $queuedJobs;
  128. }
  129. /**
  130. * @deprecated
  131. * @brief Gets all queued tasks of a specific app
  132. * @param string $app app name
  133. * @return array with associative arrays
  134. */
  135. public static function queuedTaskWhereAppIs($app) {
  136. $jobList = new JobList();
  137. $allJobs = $jobList->getAll();
  138. $queuedJobs = array();
  139. foreach ($allJobs as $job) {
  140. if ($job instanceof QueuedLegacyJob) {
  141. $queuedJob = $job->getArgument();
  142. $queuedJob['id'] = $job->getId();
  143. if ($queuedJob['app'] === $app) {
  144. $queuedJobs[] = $queuedJob;
  145. }
  146. }
  147. }
  148. return $queuedJobs;
  149. }
  150. /**
  151. * @deprecated
  152. * @brief queues a task
  153. * @param string $app app name
  154. * @param string $class class name
  155. * @param string $method method name
  156. * @param string $parameters all useful data as text
  157. * @return int id of task
  158. */
  159. public static function addQueuedTask($app, $class, $method, $parameters) {
  160. self::registerJob('OC\BackgroundJob\Legacy\QueuedJob', array('app' => $app, 'klass' => $class, 'method' => $method, 'parameters' => $parameters));
  161. return true;
  162. }
  163. /**
  164. * @deprecated
  165. * @brief deletes a queued task
  166. * @param int $id id of task
  167. * @return bool
  168. *
  169. * Deletes a report
  170. */
  171. public static function deleteQueuedTask($id) {
  172. $jobList = new JobList();
  173. $job = $jobList->getById($id);
  174. if ($job) {
  175. $jobList->remove($job);
  176. }
  177. }
  178. }