cron.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Artem Sidorenko <artem@posteo.de>
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  8. * @author Damjan Georgievski <gdamjan@gmail.com>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Jakob Sack <mail@jakobsack.de>
  11. * @author Joas Schilling <coding@schilljs.com>
  12. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  13. * @author Ko- <k.stoffelen@cs.ru.nl>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Oliver Kohl D.Sc. <oliver@kohl.bz>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Steffen Lindner <mail@steffen-lindner.de>
  19. * @author Thomas Müller <thomas.mueller@tmit.eu>
  20. * @author Vincent Petry <pvince81@owncloud.com>
  21. *
  22. * @license AGPL-3.0
  23. *
  24. * This code is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Affero General Public License, version 3,
  26. * as published by the Free Software Foundation.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU Affero General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU Affero General Public License, version 3,
  34. * along with this program. If not, see <http://www.gnu.org/licenses/>
  35. *
  36. */
  37. require_once __DIR__ . '/lib/versioncheck.php';
  38. try {
  39. require_once __DIR__ . '/lib/base.php';
  40. if (\OCP\Util::needUpgrade()) {
  41. \OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']);
  42. exit;
  43. }
  44. if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
  45. \OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']);
  46. exit;
  47. }
  48. // load all apps to get all api routes properly setup
  49. OC_App::loadApps();
  50. \OC::$server->getSession()->close();
  51. // initialize a dummy memory session
  52. $session = new \OC\Session\Memory('');
  53. $cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
  54. $session = $cryptoWrapper->wrapSession($session);
  55. \OC::$server->setSession($session);
  56. $logger = \OC::$server->getLogger();
  57. $config = \OC::$server->getConfig();
  58. // Don't do anything if Nextcloud has not been installed
  59. if (!$config->getSystemValue('installed', false)) {
  60. exit(0);
  61. }
  62. \OC::$server->getTempManager()->cleanOld();
  63. // Exit if background jobs are disabled!
  64. $appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax');
  65. if ($appMode === 'none') {
  66. if (OC::$CLI) {
  67. echo 'Background Jobs are disabled!' . PHP_EOL;
  68. } else {
  69. OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!')));
  70. }
  71. exit(1);
  72. }
  73. if (OC::$CLI) {
  74. // set to run indefinitely if needed
  75. if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
  76. @set_time_limit(0);
  77. }
  78. // the cron job must be executed with the right user
  79. if (!function_exists('posix_getuid')) {
  80. echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL;
  81. exit(1);
  82. }
  83. $user = posix_getpwuid(posix_getuid());
  84. $configUser = posix_getpwuid(fileowner(OC::$configDir . 'config.php'));
  85. if ($user['name'] !== $configUser['name']) {
  86. echo "Console has to be executed with the user that owns the file config/config.php" . PHP_EOL;
  87. echo "Current user: " . $user['name'] . PHP_EOL;
  88. echo "Owner of config.php: " . $configUser['name'] . PHP_EOL;
  89. exit(1);
  90. }
  91. // We call Nextcloud from the CLI (aka cron)
  92. if ($appMode !== 'cron') {
  93. $config->setAppValue('core', 'backgroundjobs_mode', 'cron');
  94. }
  95. // Work
  96. $jobList = \OC::$server->getJobList();
  97. // We only ask for jobs for 14 minutes, because after 5 minutes the next
  98. // system cron task should spawn and we want to have at most three
  99. // cron jobs running in parallel.
  100. $endTime = time() + 14 * 60;
  101. $executedJobs = [];
  102. while ($job = $jobList->getNext()) {
  103. if (isset($executedJobs[$job->getId()])) {
  104. $jobList->unlockJob($job);
  105. break;
  106. }
  107. $job->execute($jobList, $logger);
  108. // clean up after unclean jobs
  109. \OC_Util::tearDownFS();
  110. $jobList->setLastJob($job);
  111. $executedJobs[$job->getId()] = true;
  112. unset($job);
  113. if (time() > $endTime) {
  114. break;
  115. }
  116. }
  117. } else {
  118. // We call cron.php from some website
  119. if ($appMode === 'cron') {
  120. // Cron is cron :-P
  121. OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
  122. } else {
  123. // Work and success :-)
  124. $jobList = \OC::$server->getJobList();
  125. $job = $jobList->getNext();
  126. if ($job != null) {
  127. $job->execute($jobList, $logger);
  128. $jobList->setLastJob($job);
  129. }
  130. OC_JSON::success();
  131. }
  132. }
  133. // Log the successful cron execution
  134. $config->setAppValue('core', 'lastcron', time());
  135. exit();
  136. } catch (Exception $ex) {
  137. \OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
  138. } catch (Error $ex) {
  139. \OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
  140. }