BackgroundJob.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. }