cron.php 6.1 KB

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