cron.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Unfortunately we need this class for shutdown function
  23. class TemporaryCronClass {
  24. public static $sent = false;
  25. public static $lockfile = "";
  26. public static $keeplock = false;
  27. }
  28. // We use this function to handle (unexpected) shutdowns
  29. function handleUnexpectedShutdown() {
  30. // Delete lockfile
  31. if (!TemporaryCronClass::$keeplock && file_exists(TemporaryCronClass::$lockfile)) {
  32. unlink(TemporaryCronClass::$lockfile);
  33. }
  34. // Say goodbye if the app did not shutdown properly
  35. if (!TemporaryCronClass::$sent) {
  36. if (OC::$CLI) {
  37. echo 'Unexpected error!' . PHP_EOL;
  38. } else {
  39. OC_JSON::error(array('data' => array('message' => 'Unexpected error!')));
  40. }
  41. }
  42. }
  43. try {
  44. require_once 'lib/base.php';
  45. if (\OCP\Util::needUpgrade()) {
  46. \OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG);
  47. exit();
  48. }
  49. // load all apps to get all api routes properly setup
  50. OC_App::loadApps();
  51. \OC::$server->getSession()->close();
  52. // initialize a dummy memory session
  53. \OC::$server->setSession(new \OC\Session\Memory(''));
  54. $logger = \OC_Log::$object;
  55. // Don't do anything if ownCloud has not been installed
  56. if (!OC_Config::getValue('installed', false)) {
  57. exit(0);
  58. }
  59. // Handle unexpected errors
  60. register_shutdown_function('handleUnexpectedShutdown');
  61. // Delete temp folder
  62. OC_Helper::cleanTmpNoClean();
  63. // Exit if background jobs are disabled!
  64. $appmode = OC_BackgroundJob::getExecutionType();
  65. if ($appmode == 'none') {
  66. TemporaryCronClass::$sent = true;
  67. if (OC::$CLI) {
  68. echo 'Background Jobs are disabled!' . PHP_EOL;
  69. } else {
  70. OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!')));
  71. }
  72. exit(1);
  73. }
  74. if (OC::$CLI) {
  75. // Create lock file first
  76. TemporaryCronClass::$lockfile = OC_Config::getValue("datadirectory", OC::$SERVERROOT . '/data') . '/cron.lock';
  77. // We call ownCloud from the CLI (aka cron)
  78. if ($appmode != 'cron') {
  79. // Use cron in future!
  80. OC_BackgroundJob::setExecutionType('cron');
  81. }
  82. // check if backgroundjobs is still running
  83. if (file_exists(TemporaryCronClass::$lockfile)) {
  84. TemporaryCronClass::$keeplock = true;
  85. TemporaryCronClass::$sent = true;
  86. echo "Another instance of cron.php is still running!" . PHP_EOL;
  87. exit(1);
  88. }
  89. // Create a lock file
  90. touch(TemporaryCronClass::$lockfile);
  91. // Work
  92. $jobList = \OC::$server->getJobList();
  93. $jobs = $jobList->getAll();
  94. foreach ($jobs as $job) {
  95. $job->execute($jobList, $logger);
  96. }
  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. // done!
  114. TemporaryCronClass::$sent = true;
  115. // Log the successfull cron exec
  116. if (OC_Config::getValue('cron_log', true)) {
  117. OC_Appconfig::setValue('core', 'lastcron', time());
  118. }
  119. exit();
  120. } catch (Exception $ex) {
  121. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  122. }