cron.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. $config = OC::$server->getConfig();
  65. $instanceId = $config->getSystemValue('instanceid');
  66. $lockFileName = 'owncloud-server-' . $instanceId . '-cron.lock';
  67. $lockDirectory = $config->getSystemValue('cron.lockfile.location', sys_get_temp_dir());
  68. $lockDirectory = rtrim($lockDirectory, '\\/');
  69. $lockFile = $lockDirectory . '/' . $lockFileName;
  70. if (!file_exists($lockFile)) {
  71. touch($lockFile);
  72. }
  73. // We call ownCloud from the CLI (aka cron)
  74. if ($appMode != 'cron') {
  75. OC_BackgroundJob::setExecutionType('cron');
  76. }
  77. // open the file and try to lock if. If it is not locked, the background
  78. // job can be executed, otherwise another instance is already running
  79. $fp = fopen($lockFile, 'w');
  80. $isLocked = flock($fp, LOCK_EX|LOCK_NB, $wouldBlock);
  81. // check if backgroundjobs is still running. The wouldBlock check is
  82. // needed on systems with advisory locking, see
  83. // http://php.net/manual/en/function.flock.php#45464
  84. if (!$isLocked || $wouldBlock) {
  85. echo "Another instance of cron.php is still running!" . PHP_EOL;
  86. exit(1);
  87. }
  88. // Work
  89. $jobList = \OC::$server->getJobList();
  90. $jobs = $jobList->getAll();
  91. foreach ($jobs as $job) {
  92. $job->execute($jobList, $logger);
  93. }
  94. // unlock the file
  95. flock($fp, LOCK_UN);
  96. fclose($fp);
  97. } else {
  98. // We call cron.php from some website
  99. if ($appMode == 'cron') {
  100. // Cron is cron :-P
  101. OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
  102. } else {
  103. // Work and success :-)
  104. $jobList = \OC::$server->getJobList();
  105. $job = $jobList->getNext();
  106. if ($job != null) {
  107. $job->execute($jobList, $logger);
  108. $jobList->setLastJob($job);
  109. }
  110. OC_JSON::success();
  111. }
  112. }
  113. // Log the successful cron execution
  114. if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
  115. \OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
  116. }
  117. exit();
  118. } catch (Exception $ex) {
  119. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  120. }