BackgroundJob.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. /**
  28. * Public interface of ownCloud for background jobs.
  29. */
  30. // use OCP namespace for all classes that are considered public.
  31. // This means that they should be used by apps instead of the internal ownCloud classes
  32. namespace OCP;
  33. /**
  34. * This class provides functions to register backgroundjobs in ownCloud
  35. *
  36. * To create a new backgroundjob create a new class that inherits from either \OC\BackgroundJob\Job,
  37. * \OC\BackgroundJob\QueuedJob or \OC\BackgroundJob\TimedJob and register it using
  38. * \OCP\BackgroundJob->registerJob($job, $argument), $argument will be passed to the run() function
  39. * of the job when the job is executed.
  40. *
  41. * A regular Job will be executed every time cron.php is run, a QueuedJob will only run once and a TimedJob
  42. * will only run at a specific interval which is to be specified in the constructor of the job by calling
  43. * $this->setInterval($interval) with $interval in seconds.
  44. * @since 4.5.0
  45. */
  46. class BackgroundJob {
  47. /**
  48. * get the execution type of background jobs
  49. *
  50. * @return string
  51. *
  52. * This method returns the type how background jobs are executed. If the user
  53. * did not select something, the type is ajax.
  54. * @since 5.0.0
  55. */
  56. public static function getExecutionType() {
  57. return \OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax');
  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. * @since 5.0.0
  68. */
  69. public static function setExecutionType($type) {
  70. if( !in_array( $type, array('none', 'ajax', 'webcron', 'cron'))) {
  71. return false;
  72. }
  73. \OC::$server->getConfig()->setAppValue('core', 'backgroundjobs_mode', $type);
  74. }
  75. /**
  76. * @param string $job
  77. * @param mixed $argument
  78. * @deprecated 8.1.0 Use \OC::$server->getJobList()->add() instead
  79. * @since 6.0.0
  80. */
  81. public static function registerJob($job, $argument = null) {
  82. $jobList = \OC::$server->getJobList();
  83. $jobList->add($job, $argument);
  84. }
  85. }