You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.9 KiB

10 years ago
10 years ago
10 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
11 years ago
13 years ago
12 years ago
  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. * @author Stephen Michel <git@smichel.me>
  24. *
  25. * @license AGPL-3.0
  26. *
  27. * This code is free software: you can redistribute it and/or modify
  28. * it under the terms of the GNU Affero General Public License, version 3,
  29. * as published by the Free Software Foundation.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU Affero General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Affero General Public License, version 3,
  37. * along with this program. If not, see <http://www.gnu.org/licenses/>
  38. *
  39. */
  40. require_once __DIR__ . '/lib/versioncheck.php';
  41. try {
  42. require_once __DIR__ . '/lib/base.php';
  43. if (\OCP\Util::needUpgrade()) {
  44. \OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']);
  45. exit;
  46. }
  47. if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
  48. \OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']);
  49. exit;
  50. }
  51. // load all apps to get all api routes properly setup
  52. OC_App::loadApps();
  53. \OC::$server->getSession()->close();
  54. // initialize a dummy memory session
  55. $session = new \OC\Session\Memory('');
  56. $cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
  57. $session = $cryptoWrapper->wrapSession($session);
  58. \OC::$server->setSession($session);
  59. $logger = \OC::$server->getLogger();
  60. $config = \OC::$server->getConfig();
  61. // Don't do anything if Nextcloud has not been installed
  62. if (!$config->getSystemValue('installed', false)) {
  63. exit(0);
  64. }
  65. \OC::$server->getTempManager()->cleanOld();
  66. // Exit if background jobs are disabled!
  67. $appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax');
  68. if ($appMode === 'none') {
  69. if (OC::$CLI) {
  70. echo 'Background Jobs are disabled!' . PHP_EOL;
  71. } else {
  72. OC_JSON::error(['data' => ['message' => 'Background jobs disabled!']]);
  73. }
  74. exit(1);
  75. }
  76. if (OC::$CLI) {
  77. // set to run indefinitely if needed
  78. if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
  79. @set_time_limit(0);
  80. }
  81. // the cron job must be executed with the right user
  82. if (!function_exists('posix_getuid')) {
  83. echo "The posix extensions are required - see https://www.php.net/manual/en/book.posix.php" . PHP_EOL;
  84. exit(1);
  85. }
  86. $user = posix_getuid();
  87. $configUser = fileowner(OC::$configDir . 'config.php');
  88. if ($user !== $configUser) {
  89. echo "Console has to be executed with the user that owns the file config/config.php" . PHP_EOL;
  90. echo "Current user id: " . $user . PHP_EOL;
  91. echo "Owner id of config.php: " . $configUser . PHP_EOL;
  92. exit(1);
  93. }
  94. // We call Nextcloud from the CLI (aka cron)
  95. if ($appMode !== 'cron') {
  96. $config->setAppValue('core', 'backgroundjobs_mode', 'cron');
  97. }
  98. // Low-load hours
  99. $onlyTimeSensitive = false;
  100. $startHour = $config->getSystemValueInt('maintenance_window_start', 100);
  101. if ($startHour <= 23) {
  102. $date = new \DateTime('now', new \DateTimeZone('UTC'));
  103. $currentHour = (int) $date->format('G');
  104. $endHour = $startHour + 4;
  105. if ($startHour <= 20) {
  106. // Start time: 01:00
  107. // End time: 05:00
  108. // Only run sensitive tasks when it's before the start or after the end
  109. $onlyTimeSensitive = $currentHour < $startHour || $currentHour > $endHour;
  110. } else {
  111. // Start time: 23:00
  112. // End time: 03:00
  113. $endHour -= 24; // Correct the end time from 27:00 to 03:00
  114. // Only run sensitive tasks when it's after the end and before the start
  115. $onlyTimeSensitive = $currentHour > $endHour && $currentHour < $startHour;
  116. }
  117. }
  118. // Work
  119. $jobList = \OC::$server->getJobList();
  120. // We only ask for jobs for 14 minutes, because after 5 minutes the next
  121. // system cron task should spawn and we want to have at most three
  122. // cron jobs running in parallel.
  123. $endTime = time() + 14 * 60;
  124. $executedJobs = [];
  125. while ($job = $jobList->getNext($onlyTimeSensitive)) {
  126. if (isset($executedJobs[$job->getId()])) {
  127. $jobList->unlockJob($job);
  128. break;
  129. }
  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. $job->execute($jobList, $logger);
  151. $jobList->setLastJob($job);
  152. }
  153. OC_JSON::success();
  154. }
  155. }
  156. // Log the successful cron execution
  157. $config->setAppValue('core', 'lastcron', time());
  158. exit();
  159. } catch (Exception $ex) {
  160. \OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
  161. echo $ex . PHP_EOL;
  162. exit(1);
  163. } catch (Error $ex) {
  164. \OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
  165. echo $ex . PHP_EOL;
  166. exit(1);
  167. }