cron.php 4.9 KB

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