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.2KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
9 years ago
11 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Oliver Kohl D.Sc. <oliver@kohl.bz>
  9. * @author Robin Appelman <icewind@owncloud.com>
  10. * @author Steffen Lindner <mail@steffen-lindner.de>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @copyright Copyright (c) 2015, ownCloud, Inc.
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. try {
  31. require_once 'lib/base.php';
  32. if (\OCP\Util::needUpgrade()) {
  33. \OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG);
  34. exit;
  35. }
  36. if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
  37. \OCP\Util::writeLog('cron', 'We are in maintenance mode, skipping cron', \OCP\Util::DEBUG);
  38. exit;
  39. }
  40. if (\OC::$server->getSystemConfig()->getValue('singleuser', false)) {
  41. \OCP\Util::writeLog('cron', 'We are in admin only mode, skipping cron', \OCP\Util::DEBUG);
  42. exit;
  43. }
  44. // load all apps to get all api routes properly setup
  45. OC_App::loadApps();
  46. \OC::$server->getSession()->close();
  47. // initialize a dummy memory session
  48. $session = new \OC\Session\Memory('');
  49. $cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
  50. $session = $cryptoWrapper->wrapSession($session);
  51. \OC::$server->setSession($session);
  52. $logger = \OC::$server->getLogger();
  53. // Don't do anything if ownCloud has not been installed
  54. if (!OC_Config::getValue('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. set_time_limit(0);
  71. // the cron job must be executed with the right user
  72. if (!OC_Util::runningOnWindows()) {
  73. if (!function_exists('posix_getuid')) {
  74. echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL;
  75. exit(0);
  76. }
  77. $user = posix_getpwuid(posix_getuid());
  78. $configUser = posix_getpwuid(fileowner(OC::$SERVERROOT . '/config/config.php'));
  79. if ($user['name'] !== $configUser['name']) {
  80. echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL;
  81. echo "Current user: " . $user['name'] . PHP_EOL;
  82. echo "Web server user: " . $configUser['name'] . PHP_EOL;
  83. exit(0);
  84. }
  85. }
  86. $config = OC::$server->getConfig();
  87. $instanceId = $config->getSystemValue('instanceid');
  88. $lockFileName = 'owncloud-server-' . $instanceId . '-cron.lock';
  89. $lockDirectory = $config->getSystemValue('cron.lockfile.location', sys_get_temp_dir());
  90. $lockDirectory = rtrim($lockDirectory, '\\/');
  91. $lockFile = $lockDirectory . '/' . $lockFileName;
  92. if (!file_exists($lockFile)) {
  93. touch($lockFile);
  94. }
  95. // We call ownCloud from the CLI (aka cron)
  96. if ($appMode != 'cron') {
  97. \OCP\BackgroundJob::setExecutionType('cron');
  98. }
  99. // open the file and try to lock if. If it is not locked, the background
  100. // job can be executed, otherwise another instance is already running
  101. $fp = fopen($lockFile, 'w');
  102. $isLocked = flock($fp, LOCK_EX|LOCK_NB, $wouldBlock);
  103. // check if backgroundjobs is still running. The wouldBlock check is
  104. // needed on systems with advisory locking, see
  105. // http://php.net/manual/en/function.flock.php#45464
  106. if (!$isLocked || $wouldBlock) {
  107. echo "Another instance of cron.php is still running!" . PHP_EOL;
  108. exit(1);
  109. }
  110. // Work
  111. $jobList = \OC::$server->getJobList();
  112. $jobs = $jobList->getAll();
  113. foreach ($jobs as $job) {
  114. $job->execute($jobList, $logger);
  115. }
  116. // unlock the file
  117. flock($fp, LOCK_UN);
  118. fclose($fp);
  119. } else {
  120. // We call cron.php from some website
  121. if ($appMode == 'cron') {
  122. // Cron is cron :-P
  123. OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
  124. } else {
  125. // Work and success :-)
  126. $jobList = \OC::$server->getJobList();
  127. $job = $jobList->getNext();
  128. if ($job != null) {
  129. $job->execute($jobList, $logger);
  130. $jobList->setLastJob($job);
  131. }
  132. OC_JSON::success();
  133. }
  134. }
  135. // Log the successful cron execution
  136. if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
  137. \OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
  138. }
  139. exit();
  140. } catch (Exception $ex) {
  141. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  142. }