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.

260 lines
8.0 KiB

13 years ago
12 years ago
13 years ago
12 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. require_once __DIR__ . '/lib/versioncheck.php';
  9. use OCP\App\IAppManager;
  10. use OCP\BackgroundJob\IJobList;
  11. use OCP\IAppConfig;
  12. use OCP\IConfig;
  13. use OCP\ISession;
  14. use OCP\ITempManager;
  15. use OCP\Server;
  16. use OCP\Util;
  17. use Psr\Log\LoggerInterface;
  18. try {
  19. require_once __DIR__ . '/lib/base.php';
  20. if (isset($argv[1]) && ($argv[1] === '-h' || $argv[1] === '--help')) {
  21. echo 'Description:
  22. Run the background job routine
  23. Usage:
  24. php -f cron.php -- [-h] [--verbose] [<job-classes>...]
  25. Arguments:
  26. job-classes Optional job class list to only run those jobs
  27. Providing a class will ignore the time-sensitivity restriction
  28. Options:
  29. -h, --help Display this help message
  30. -v, --verbose Output more information' . PHP_EOL;
  31. exit(0);
  32. }
  33. if (Util::needUpgrade()) {
  34. Server::get(LoggerInterface::class)->debug('Update required, skipping cron', ['app' => 'cron']);
  35. exit;
  36. }
  37. $config = Server::get(IConfig::class);
  38. if ($config->getSystemValueBool('maintenance', false)) {
  39. Server::get(LoggerInterface::class)->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']);
  40. exit;
  41. }
  42. // Don't do anything if Nextcloud has not been installed
  43. if (!$config->getSystemValueBool('installed', false)) {
  44. exit(0);
  45. }
  46. // load all apps to get all api routes properly setup
  47. Server::get(IAppManager::class)->loadApps();
  48. Server::get(ISession::class)->close();
  49. $verbose = isset($argv[1]) && ($argv[1] === '-v' || $argv[1] === '--verbose');
  50. // initialize a dummy memory session
  51. $session = new \OC\Session\Memory();
  52. $cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
  53. $session = $cryptoWrapper->wrapSession($session);
  54. \OC::$server->setSession($session);
  55. $logger = Server::get(LoggerInterface::class);
  56. $appConfig = Server::get(IAppConfig::class);
  57. $tempManager = Server::get(ITempManager::class);
  58. $tempManager->cleanOld();
  59. // Exit if background jobs are disabled!
  60. $appMode = $appConfig->getValueString('core', 'backgroundjobs_mode', 'ajax');
  61. if ($appMode === 'none') {
  62. if (OC::$CLI) {
  63. echo 'Background Jobs are disabled!' . PHP_EOL;
  64. } else {
  65. OC_JSON::error(['data' => ['message' => 'Background jobs disabled!']]);
  66. }
  67. exit(1);
  68. }
  69. if (OC::$CLI) {
  70. // set to run indefinitely if needed
  71. if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
  72. @set_time_limit(0);
  73. }
  74. // the cron job must be executed with the right user
  75. if (!function_exists('posix_getuid')) {
  76. echo 'The posix extensions are required - see https://www.php.net/manual/en/book.posix.php' . PHP_EOL;
  77. exit(1);
  78. }
  79. $user = posix_getuid();
  80. $configUser = fileowner(OC::$configDir . 'config.php');
  81. if ($user !== $configUser) {
  82. echo 'Console has to be executed with the user that owns the file config/config.php' . PHP_EOL;
  83. echo 'Current user id: ' . $user . PHP_EOL;
  84. echo 'Owner id of config.php: ' . $configUser . PHP_EOL;
  85. exit(1);
  86. }
  87. // We call Nextcloud from the CLI (aka cron)
  88. if ($appMode !== 'cron') {
  89. $appConfig->setValueString('core', 'backgroundjobs_mode', 'cron');
  90. }
  91. // a specific job class list can optionally be given as argument
  92. $jobClasses = array_slice($argv, $verbose ? 2 : 1);
  93. $jobClasses = empty($jobClasses) ? null : $jobClasses;
  94. // Low-load hours
  95. $onlyTimeSensitive = false;
  96. $startHour = $config->getSystemValueInt('maintenance_window_start', 100);
  97. if ($jobClasses === null && $startHour <= 23) {
  98. $date = new \DateTime('now', new \DateTimeZone('UTC'));
  99. $currentHour = (int)$date->format('G');
  100. $endHour = $startHour + 4;
  101. if ($startHour <= 20) {
  102. // Start time: 01:00
  103. // End time: 05:00
  104. // Only run sensitive tasks when it's before the start or after the end
  105. $onlyTimeSensitive = $currentHour < $startHour || $currentHour > $endHour;
  106. } else {
  107. // Start time: 23:00
  108. // End time: 03:00
  109. $endHour -= 24; // Correct the end time from 27:00 to 03:00
  110. // Only run sensitive tasks when it's after the end and before the start
  111. $onlyTimeSensitive = $currentHour > $endHour && $currentHour < $startHour;
  112. }
  113. }
  114. // Work
  115. $jobList = Server::get(IJobList::class);
  116. // We only ask for jobs for 14 minutes, because after 5 minutes the next
  117. // system cron task should spawn and we want to have at most three
  118. // cron jobs running in parallel.
  119. $endTime = time() + 14 * 60;
  120. $executedJobs = [];
  121. while ($job = $jobList->getNext($onlyTimeSensitive, $jobClasses)) {
  122. if (isset($executedJobs[$job->getId()])) {
  123. $jobList->unlockJob($job);
  124. break;
  125. }
  126. $jobDetails = get_class($job) . ' (id: ' . $job->getId() . ', arguments: ' . json_encode($job->getArgument()) . ')';
  127. $logger->debug('CLI cron call has selected job ' . $jobDetails, ['app' => 'cron']);
  128. $timeBefore = time();
  129. $memoryBefore = memory_get_usage();
  130. $memoryPeakBefore = memory_get_peak_usage();
  131. if ($verbose) {
  132. echo 'Starting job ' . $jobDetails . PHP_EOL;
  133. }
  134. /** @psalm-suppress DeprecatedMethod Calling execute until it is removed, then will switch to start */
  135. $job->execute($jobList);
  136. $timeAfter = time();
  137. $memoryAfter = memory_get_usage();
  138. $memoryPeakAfter = memory_get_peak_usage();
  139. $cronInterval = 5 * 60;
  140. $timeSpent = $timeAfter - $timeBefore;
  141. if ($timeSpent > $cronInterval) {
  142. $logLevel = match (true) {
  143. $timeSpent > $cronInterval * 128 => \OCP\ILogger::FATAL,
  144. $timeSpent > $cronInterval * 64 => \OCP\ILogger::ERROR,
  145. $timeSpent > $cronInterval * 16 => \OCP\ILogger::WARN,
  146. $timeSpent > $cronInterval * 8 => \OCP\ILogger::INFO,
  147. default => \OCP\ILogger::DEBUG,
  148. };
  149. $logger->log(
  150. $logLevel,
  151. 'Background job ' . $jobDetails . ' ran for ' . $timeSpent . ' seconds',
  152. ['app' => 'cron']
  153. );
  154. }
  155. if ($memoryAfter - $memoryBefore > 50_000_000) {
  156. $message = 'Used memory grew by more than 50 MB when executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryAfter) . ' (before: ' . Util::humanFileSize($memoryBefore) . ')';
  157. $logger->warning($message, ['app' => 'cron']);
  158. if ($verbose) {
  159. echo $message . PHP_EOL;
  160. }
  161. }
  162. if ($memoryPeakAfter > 300_000_000 && $memoryPeakBefore <= 300_000_000) {
  163. $message = 'Cron job used more than 300 MB of ram after executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryPeakAfter) . ' (before: ' . Util::humanFileSize($memoryPeakBefore) . ')';
  164. $logger->warning($message, ['app' => 'cron']);
  165. if ($verbose) {
  166. echo $message . PHP_EOL;
  167. }
  168. }
  169. // clean up after unclean jobs
  170. Server::get(\OC\Files\SetupManager::class)->tearDown();
  171. $tempManager->clean();
  172. if ($verbose) {
  173. echo 'Job ' . $jobDetails . ' done in ' . ($timeAfter - $timeBefore) . ' seconds' . PHP_EOL;
  174. }
  175. $jobList->setLastJob($job);
  176. $executedJobs[$job->getId()] = true;
  177. unset($job);
  178. if ($timeAfter > $endTime) {
  179. break;
  180. }
  181. }
  182. } else {
  183. // We call cron.php from some website
  184. if ($appMode === 'cron') {
  185. // Cron is cron :-P
  186. OC_JSON::error(['data' => ['message' => 'Backgroundjobs are using system cron!']]);
  187. } else {
  188. // Work and success :-)
  189. $jobList = Server::get(IJobList::class);
  190. $job = $jobList->getNext();
  191. if ($job != null) {
  192. $logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']);
  193. /** @psalm-suppress DeprecatedMethod Calling execute until it is removed, then will switch to start */
  194. $job->execute($jobList);
  195. $jobList->setLastJob($job);
  196. }
  197. OC_JSON::success();
  198. }
  199. }
  200. // Log the successful cron execution
  201. $appConfig->setValueInt('core', 'lastcron', time());
  202. exit();
  203. } catch (Exception $ex) {
  204. Server::get(LoggerInterface::class)->error(
  205. $ex->getMessage(),
  206. ['app' => 'cron', 'exception' => $ex]
  207. );
  208. echo $ex . PHP_EOL;
  209. exit(1);
  210. } catch (Error $ex) {
  211. Server::get(LoggerInterface::class)->error(
  212. $ex->getMessage(),
  213. ['app' => 'cron', 'exception' => $ex]
  214. );
  215. echo $ex . PHP_EOL;
  216. exit(1);
  217. }