cron.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // load all apps to get all api routes properly setup
  41. OC_App::loadApps();
  42. \OC::$server->getSession()->close();
  43. // initialize a dummy memory session
  44. \OC::$server->setSession(new \OC\Session\Memory(''));
  45. $logger = \OC_Log::$object;
  46. // Don't do anything if ownCloud has not been installed
  47. if (!OC_Config::getValue('installed', false)) {
  48. exit(0);
  49. }
  50. \OC::$server->getTempManager()->cleanOld();
  51. // Exit if background jobs are disabled!
  52. $appMode = OC_BackgroundJob::getExecutionType();
  53. if ($appMode == 'none') {
  54. if (OC::$CLI) {
  55. echo 'Background Jobs are disabled!' . PHP_EOL;
  56. } else {
  57. OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!')));
  58. }
  59. exit(1);
  60. }
  61. if (OC::$CLI) {
  62. // set to run indefinitely if needed
  63. set_time_limit(0);
  64. // the cron job must be executed with the right user
  65. if (!OC_Util::runningOnWindows()) {
  66. if (!function_exists('posix_getuid')) {
  67. echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL;
  68. exit(0);
  69. }
  70. $user = posix_getpwuid(posix_getuid());
  71. $configUser = posix_getpwuid(fileowner(OC::$SERVERROOT . '/config/config.php'));
  72. if ($user['name'] !== $configUser['name']) {
  73. echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL;
  74. echo "Current user: " . $user['name'] . PHP_EOL;
  75. echo "Web server user: " . $configUser['name'] . PHP_EOL;
  76. exit(0);
  77. }
  78. }
  79. $config = OC::$server->getConfig();
  80. $instanceId = $config->getSystemValue('instanceid');
  81. $lockFileName = 'owncloud-server-' . $instanceId . '-cron.lock';
  82. $lockDirectory = $config->getSystemValue('cron.lockfile.location', sys_get_temp_dir());
  83. $lockDirectory = rtrim($lockDirectory, '\\/');
  84. $lockFile = $lockDirectory . '/' . $lockFileName;
  85. if (!file_exists($lockFile)) {
  86. touch($lockFile);
  87. }
  88. // We call ownCloud from the CLI (aka cron)
  89. if ($appMode != 'cron') {
  90. OC_BackgroundJob::setExecutionType('cron');
  91. }
  92. // open the file and try to lock if. If it is not locked, the background
  93. // job can be executed, otherwise another instance is already running
  94. $fp = fopen($lockFile, 'w');
  95. $isLocked = flock($fp, LOCK_EX|LOCK_NB, $wouldBlock);
  96. // check if backgroundjobs is still running. The wouldBlock check is
  97. // needed on systems with advisory locking, see
  98. // http://php.net/manual/en/function.flock.php#45464
  99. if (!$isLocked || $wouldBlock) {
  100. echo "Another instance of cron.php is still running!" . PHP_EOL;
  101. exit(1);
  102. }
  103. // Work
  104. $jobList = \OC::$server->getJobList();
  105. $jobs = $jobList->getAll();
  106. foreach ($jobs as $job) {
  107. $job->execute($jobList, $logger);
  108. }
  109. // unlock the file
  110. flock($fp, LOCK_UN);
  111. fclose($fp);
  112. } else {
  113. // We call cron.php from some website
  114. if ($appMode == 'cron') {
  115. // Cron is cron :-P
  116. OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
  117. } else {
  118. // Work and success :-)
  119. $jobList = \OC::$server->getJobList();
  120. $job = $jobList->getNext();
  121. if ($job != null) {
  122. $job->execute($jobList, $logger);
  123. $jobList->setLastJob($job);
  124. }
  125. OC_JSON::success();
  126. }
  127. }
  128. // Log the successful cron execution
  129. if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
  130. \OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
  131. }
  132. exit();
  133. } catch (Exception $ex) {
  134. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  135. }