cron.php 5.0 KB

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