1
0

cron.php 4.7 KB

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