cron.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 Phil Davis <phil.davis@inf.org>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Steffen Lindner <mail@steffen-lindner.de>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Vincent Petry <pvince81@owncloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  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. if (\OC::$server->getSystemConfig()->getValue('singleuser', false)) {
  44. \OCP\Util::writeLog('cron', 'We are in admin only mode, skipping cron', \OCP\Util::DEBUG);
  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 ownCloud 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 = \OCP\BackgroundJob::getExecutionType();
  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. set_time_limit(0);
  75. // the cron job must be executed with the right user
  76. if (!OC_Util::runningOnWindows()) {
  77. if (!function_exists('posix_getuid')) {
  78. echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL;
  79. exit(0);
  80. }
  81. $user = posix_getpwuid(posix_getuid());
  82. $configUser = posix_getpwuid(fileowner(OC::$SERVERROOT . '/config/config.php'));
  83. if ($user['name'] !== $configUser['name']) {
  84. echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL;
  85. echo "Current user: " . $user['name'] . PHP_EOL;
  86. echo "Web server user: " . $configUser['name'] . PHP_EOL;
  87. exit(0);
  88. }
  89. }
  90. $instanceId = $config->getSystemValue('instanceid');
  91. $lockFileName = 'owncloud-server-' . $instanceId . '-cron.lock';
  92. $lockDirectory = $config->getSystemValue('cron.lockfile.location', sys_get_temp_dir());
  93. $lockDirectory = rtrim($lockDirectory, '\\/');
  94. $lockFile = $lockDirectory . '/' . $lockFileName;
  95. if (!file_exists($lockFile)) {
  96. touch($lockFile);
  97. }
  98. // We call ownCloud from the CLI (aka cron)
  99. if ($appMode != 'cron') {
  100. \OCP\BackgroundJob::setExecutionType('cron');
  101. }
  102. // open the file and try to lock it. If it is not locked, the background
  103. // job can be executed, otherwise another instance is already running
  104. $fp = fopen($lockFile, 'w');
  105. $isLocked = flock($fp, LOCK_EX|LOCK_NB, $wouldBlock);
  106. // check if backgroundjobs is still running. The wouldBlock check is
  107. // needed on systems with advisory locking, see
  108. // http://php.net/manual/en/function.flock.php#45464
  109. if (!$isLocked || $wouldBlock) {
  110. echo "Another instance of cron.php is still running!" . PHP_EOL;
  111. exit(1);
  112. }
  113. // Work
  114. $jobList = \OC::$server->getJobList();
  115. // We only ask for jobs for 14 minutes, because after 15 minutes the next
  116. // system cron task should spawn.
  117. $endTime = time() + 14 * 60;
  118. $executedJobs = [];
  119. while ($job = $jobList->getNext()) {
  120. if (isset($executedJobs[$job->getId()])) {
  121. break;
  122. }
  123. $logger->debug('Run ' . get_class($job) . ' job with ID ' . $job->getId(), ['app' => 'cron']);
  124. $job->execute($jobList, $logger);
  125. // clean up after unclean jobs
  126. \OC_Util::tearDownFS();
  127. $logger->debug('Finished ' . get_class($job) . ' job with ID ' . $job->getId(), ['app' => 'cron']);
  128. $jobList->setLastJob($job);
  129. $executedJobs[$job->getId()] = true;
  130. unset($job);
  131. if (time() > $endTime) {
  132. break;
  133. }
  134. }
  135. // unlock the file
  136. flock($fp, LOCK_UN);
  137. fclose($fp);
  138. } else {
  139. // We call cron.php from some website
  140. if ($appMode == 'cron') {
  141. // Cron is cron :-P
  142. OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
  143. } else {
  144. // Work and success :-)
  145. $jobList = \OC::$server->getJobList();
  146. $job = $jobList->getNext();
  147. if ($job != null) {
  148. $job->execute($jobList, $logger);
  149. $jobList->setLastJob($job);
  150. }
  151. OC_JSON::success();
  152. }
  153. }
  154. // Log the successful cron execution
  155. if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
  156. \OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
  157. }
  158. exit();
  159. } catch (Exception $ex) {
  160. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  161. }