Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cron.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // Show warning if a PHP version below 5.6.0 is used
  33. if (version_compare(PHP_VERSION, '5.6.0') === -1) {
  34. echo 'This version of Nextcloud requires at least PHP 5.6.0<br/>';
  35. echo 'You are currently running ' . PHP_VERSION . '. Please update your PHP version.';
  36. return;
  37. }
  38. try {
  39. require_once __DIR__ . '/lib/base.php';
  40. if (\OCP\Util::needUpgrade()) {
  41. \OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG);
  42. exit;
  43. }
  44. if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
  45. \OCP\Util::writeLog('cron', 'We are in maintenance mode, skipping cron', \OCP\Util::DEBUG);
  46. exit;
  47. }
  48. if (\OC::$server->getSystemConfig()->getValue('singleuser', false)) {
  49. \OCP\Util::writeLog('cron', 'We are in admin only mode, skipping cron', \OCP\Util::DEBUG);
  50. exit;
  51. }
  52. // load all apps to get all api routes properly setup
  53. OC_App::loadApps();
  54. \OC::$server->getSession()->close();
  55. // initialize a dummy memory session
  56. $session = new \OC\Session\Memory('');
  57. $cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
  58. $session = $cryptoWrapper->wrapSession($session);
  59. \OC::$server->setSession($session);
  60. $logger = \OC::$server->getLogger();
  61. $config = \OC::$server->getConfig();
  62. // Don't do anything if ownCloud has not been installed
  63. if (!$config->getSystemValue('installed', false)) {
  64. exit(0);
  65. }
  66. \OC::$server->getTempManager()->cleanOld();
  67. // Exit if background jobs are disabled!
  68. $appMode = \OCP\BackgroundJob::getExecutionType();
  69. if ($appMode == 'none') {
  70. if (OC::$CLI) {
  71. echo 'Background Jobs are disabled!' . PHP_EOL;
  72. } else {
  73. OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!')));
  74. }
  75. exit(1);
  76. }
  77. if (OC::$CLI) {
  78. // set to run indefinitely if needed
  79. set_time_limit(0);
  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 http://php.net/manual/en/book.posix.php" . PHP_EOL;
  83. exit(0);
  84. }
  85. $user = posix_getpwuid(posix_getuid());
  86. $configUser = posix_getpwuid(fileowner(OC::$configDir . 'config.php'));
  87. if ($user['name'] !== $configUser['name']) {
  88. echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL;
  89. echo "Current user: " . $user['name'] . PHP_EOL;
  90. echo "Web server user: " . $configUser['name'] . PHP_EOL;
  91. exit(0);
  92. }
  93. // We call ownCloud from the CLI (aka cron)
  94. if ($appMode != 'cron') {
  95. \OCP\BackgroundJob::setExecutionType('cron');
  96. }
  97. // Work
  98. $jobList = \OC::$server->getJobList();
  99. // We only ask for jobs for 14 minutes, because after 15 minutes the next
  100. // system cron task should spawn.
  101. $endTime = time() + 14 * 60;
  102. $executedJobs = [];
  103. while ($job = $jobList->getNext()) {
  104. if (isset($executedJobs[$job->getId()])) {
  105. $jobList->unlockJob($job);
  106. break;
  107. }
  108. $logger->debug('Run ' . get_class($job) . ' job with ID ' . $job->getId(), ['app' => 'cron']);
  109. $job->execute($jobList, $logger);
  110. // clean up after unclean jobs
  111. \OC_Util::tearDownFS();
  112. $logger->debug('Finished ' . get_class($job) . ' job with ID ' . $job->getId(), ['app' => 'cron']);
  113. $jobList->setLastJob($job);
  114. $executedJobs[$job->getId()] = true;
  115. unset($job);
  116. if (time() > $endTime) {
  117. break;
  118. }
  119. }
  120. } else {
  121. // We call cron.php from some website
  122. if ($appMode == 'cron') {
  123. // Cron is cron :-P
  124. OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
  125. } else {
  126. // Work and success :-)
  127. $jobList = \OC::$server->getJobList();
  128. $job = $jobList->getNext();
  129. if ($job != null) {
  130. $job->execute($jobList, $logger);
  131. $jobList->setLastJob($job);
  132. }
  133. OC_JSON::success();
  134. }
  135. }
  136. // Log the successful cron execution
  137. if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
  138. \OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
  139. }
  140. exit();
  141. } catch (Exception $ex) {
  142. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  143. } catch (Error $ex) {
  144. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  145. }