1
0

cron.php 4.9 KB

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