cron.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Jakob Sack
  6. * @copyright 2012 Jakob Sack owncloud@jakobsack.de
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. try {
  23. require_once 'lib/base.php';
  24. if (\OCP\Util::needUpgrade()) {
  25. \OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG);
  26. exit;
  27. }
  28. if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
  29. \OCP\Util::writeLog('cron', 'We are in maintenance mode, skipping cron', \OCP\Util::DEBUG);
  30. exit;
  31. }
  32. // load all apps to get all api routes properly setup
  33. OC_App::loadApps();
  34. \OC::$server->getSession()->close();
  35. // initialize a dummy memory session
  36. \OC::$server->setSession(new \OC\Session\Memory(''));
  37. $logger = \OC_Log::$object;
  38. // Don't do anything if ownCloud has not been installed
  39. if (!OC_Config::getValue('installed', false)) {
  40. exit(0);
  41. }
  42. \OC::$server->getTempManager()->cleanOld();
  43. // Exit if background jobs are disabled!
  44. $appMode = OC_BackgroundJob::getExecutionType();
  45. if ($appMode == 'none') {
  46. if (OC::$CLI) {
  47. echo 'Background Jobs are disabled!' . PHP_EOL;
  48. } else {
  49. OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!')));
  50. }
  51. exit(1);
  52. }
  53. if (OC::$CLI) {
  54. // set to run indefinitely if needed
  55. set_time_limit(0);
  56. $config = OC::$server->getConfig();
  57. $instanceId = $config->getSystemValue('instanceid');
  58. $lockFileName = 'owncloud-server-' . $instanceId . '-cron.lock';
  59. $lockDirectory = $config->getSystemValue('cron.lockfile.location', sys_get_temp_dir());
  60. $lockDirectory = rtrim($lockDirectory, '\\/');
  61. $lockFile = $lockDirectory . '/' . $lockFileName;
  62. if (!file_exists($lockFile)) {
  63. touch($lockFile);
  64. }
  65. // We call ownCloud from the CLI (aka cron)
  66. if ($appMode != 'cron') {
  67. OC_BackgroundJob::setExecutionType('cron');
  68. }
  69. // open the file and try to lock if. If it is not locked, the background
  70. // job can be executed, otherwise another instance is already running
  71. $fp = fopen($lockFile, 'w');
  72. $isLocked = flock($fp, LOCK_EX|LOCK_NB, $wouldBlock);
  73. // check if backgroundjobs is still running. The wouldBlock check is
  74. // needed on systems with advisory locking, see
  75. // http://php.net/manual/en/function.flock.php#45464
  76. if (!$isLocked || $wouldBlock) {
  77. echo "Another instance of cron.php is still running!" . PHP_EOL;
  78. exit(1);
  79. }
  80. // Work
  81. $jobList = \OC::$server->getJobList();
  82. $jobs = $jobList->getAll();
  83. foreach ($jobs as $job) {
  84. $job->execute($jobList, $logger);
  85. }
  86. // unlock the file
  87. flock($fp, LOCK_UN);
  88. fclose($fp);
  89. } else {
  90. // We call cron.php from some website
  91. if ($appMode == 'cron') {
  92. // Cron is cron :-P
  93. OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
  94. } else {
  95. // Work and success :-)
  96. $jobList = \OC::$server->getJobList();
  97. $job = $jobList->getNext();
  98. if ($job != null) {
  99. $job->execute($jobList, $logger);
  100. $jobList->setLastJob($job);
  101. }
  102. OC_JSON::success();
  103. }
  104. }
  105. // Log the successful cron execution
  106. if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
  107. \OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
  108. }
  109. exit();
  110. } catch (Exception $ex) {
  111. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  112. }