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.

cron.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Phil Davis <phil.davis@inf.org>
  11. * @author Robin Appelman <icewind@owncloud.com>
  12. * @author Steffen Lindner <mail@steffen-lindner.de>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. *
  16. * @copyright Copyright (c) 2016, ownCloud, Inc.
  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. try {
  33. require_once 'lib/base.php';
  34. if (\OCP\Util::needUpgrade()) {
  35. \OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG);
  36. exit;
  37. }
  38. if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
  39. \OCP\Util::writeLog('cron', 'We are in maintenance mode, skipping cron', \OCP\Util::DEBUG);
  40. exit;
  41. }
  42. if (\OC::$server->getSystemConfig()->getValue('singleuser', false)) {
  43. \OCP\Util::writeLog('cron', 'We are in admin only mode, skipping cron', \OCP\Util::DEBUG);
  44. exit;
  45. }
  46. // load all apps to get all api routes properly setup
  47. OC_App::loadApps();
  48. \OC::$server->getSession()->close();
  49. // initialize a dummy memory session
  50. $session = new \OC\Session\Memory('');
  51. $cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
  52. $session = $cryptoWrapper->wrapSession($session);
  53. \OC::$server->setSession($session);
  54. $logger = \OC::$server->getLogger();
  55. $config = \OC::$server->getConfig();
  56. // Don't do anything if ownCloud has not been installed
  57. if (!$config->getSystemValue('installed', false)) {
  58. exit(0);
  59. }
  60. \OC::$server->getTempManager()->cleanOld();
  61. // Exit if background jobs are disabled!
  62. $appMode = \OCP\BackgroundJob::getExecutionType();
  63. if ($appMode == 'none') {
  64. if (OC::$CLI) {
  65. echo 'Background Jobs are disabled!' . PHP_EOL;
  66. } else {
  67. OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!')));
  68. }
  69. exit(1);
  70. }
  71. if (OC::$CLI) {
  72. // set to run indefinitely if needed
  73. set_time_limit(0);
  74. // the cron job must be executed with the right user
  75. if (!OC_Util::runningOnWindows()) {
  76. if (!function_exists('posix_getuid')) {
  77. echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL;
  78. exit(0);
  79. }
  80. $user = posix_getpwuid(posix_getuid());
  81. $configUser = posix_getpwuid(fileowner(OC::$SERVERROOT . '/config/config.php'));
  82. if ($user['name'] !== $configUser['name']) {
  83. echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL;
  84. echo "Current user: " . $user['name'] . PHP_EOL;
  85. echo "Web server user: " . $configUser['name'] . PHP_EOL;
  86. exit(0);
  87. }
  88. }
  89. $instanceId = $config->getSystemValue('instanceid');
  90. $lockFileName = 'owncloud-server-' . $instanceId . '-cron.lock';
  91. $lockDirectory = $config->getSystemValue('cron.lockfile.location', sys_get_temp_dir());
  92. $lockDirectory = rtrim($lockDirectory, '\\/');
  93. $lockFile = $lockDirectory . '/' . $lockFileName;
  94. if (!file_exists($lockFile)) {
  95. touch($lockFile);
  96. }
  97. // We call ownCloud from the CLI (aka cron)
  98. if ($appMode != 'cron') {
  99. \OCP\BackgroundJob::setExecutionType('cron');
  100. }
  101. // open the file and try to lock it. If it is not locked, the background
  102. // job can be executed, otherwise another instance is already running
  103. $fp = fopen($lockFile, 'w');
  104. $isLocked = flock($fp, LOCK_EX|LOCK_NB, $wouldBlock);
  105. // check if backgroundjobs is still running. The wouldBlock check is
  106. // needed on systems with advisory locking, see
  107. // http://php.net/manual/en/function.flock.php#45464
  108. if (!$isLocked || $wouldBlock) {
  109. echo "Another instance of cron.php is still running!" . PHP_EOL;
  110. exit(1);
  111. }
  112. // Work
  113. $jobList = \OC::$server->getJobList();
  114. $executedJobs = [];
  115. while ($job = $jobList->getNext()) {
  116. if (isset($executedJobs[$job->getId()])) {
  117. break;
  118. }
  119. $logger->debug('Run job with ID ' . $job->getId(), ['app' => 'cron']);
  120. $job->execute($jobList, $logger);
  121. $logger->debug('Finished job with ID ' . $job->getId(), ['app' => 'cron']);
  122. $jobList->setLastJob($job);
  123. $executedJobs[$job->getId()] = true;
  124. unset($job);
  125. }
  126. // unlock the file
  127. flock($fp, LOCK_UN);
  128. fclose($fp);
  129. } else {
  130. // We call cron.php from some website
  131. if ($appMode == 'cron') {
  132. // Cron is cron :-P
  133. OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
  134. } else {
  135. // Work and success :-)
  136. $jobList = \OC::$server->getJobList();
  137. $job = $jobList->getNext();
  138. if ($job != null) {
  139. $job->execute($jobList, $logger);
  140. $jobList->setLastJob($job);
  141. }
  142. OC_JSON::success();
  143. }
  144. }
  145. // Log the successful cron execution
  146. if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
  147. \OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
  148. }
  149. exit();
  150. } catch (Exception $ex) {
  151. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  152. } catch (Error $ex) {
  153. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  154. }