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.

246 lines
7.7 KiB

  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-FileContributor: Carl Schwan
  7. * SPDX-License-Identifier: AGPL-3.0-only
  8. */
  9. namespace OC\Core\Service;
  10. use OC;
  11. use OC\Authentication\LoginCredentials\Store;
  12. use OC\Files\SetupManager;
  13. use OC\Security\CSRF\TokenStorage\SessionStorage;
  14. use OC\Session\CryptoWrapper;
  15. use OC\Session\Memory;
  16. use OC\User\Session;
  17. use OCP\App\IAppManager;
  18. use OCP\BackgroundJob\IJobList;
  19. use OCP\IAppConfig;
  20. use OCP\IConfig;
  21. use OCP\ILogger;
  22. use OCP\ISession;
  23. use OCP\ITempManager;
  24. use OCP\Util;
  25. use Psr\Log\LoggerInterface;
  26. class CronService {
  27. /** * @var ?callable $verboseCallback */
  28. private $verboseCallback = null;
  29. public function __construct(
  30. private readonly LoggerInterface $logger,
  31. private readonly IConfig $config,
  32. private readonly IAppManager $appManager,
  33. private readonly ISession $session,
  34. private readonly Session $userSession,
  35. private readonly CryptoWrapper $cryptoWrapper,
  36. private readonly Store $store,
  37. private readonly SessionStorage $sessionStorage,
  38. private readonly ITempManager $tempManager,
  39. private readonly IAppConfig $appConfig,
  40. private readonly IJobList $jobList,
  41. private readonly SetupManager $setupManager,
  42. private readonly bool $isCLI,
  43. ) {
  44. }
  45. /**
  46. * @param callable(string):void $callback
  47. */
  48. public function registerVerboseCallback(callable $callback): void {
  49. $this->verboseCallback = $callback;
  50. }
  51. /**
  52. * @throws \RuntimeException
  53. */
  54. public function run(?array $jobClasses): void {
  55. if (Util::needUpgrade()) {
  56. $this->logger->debug('Update required, skipping cron', ['app' => 'core']);
  57. return;
  58. }
  59. if ($this->config->getSystemValueBool('maintenance', false)) {
  60. $this->logger->debug('We are in maintenance mode, skipping cron', ['app' => 'core']);
  61. return;
  62. }
  63. // Don't do anything if Nextcloud has not been installed
  64. if (!$this->config->getSystemValueBool('installed', false)) {
  65. return;
  66. }
  67. // load all apps to get all api routes properly setup
  68. $this->appManager->loadApps();
  69. $this->session->close();
  70. // initialize a dummy memory session
  71. $session = new Memory();
  72. $session = $this->cryptoWrapper->wrapSession($session);
  73. $this->sessionStorage->setSession($session);
  74. $this->userSession->setSession($session);
  75. $this->store->setSession($session);
  76. $this->tempManager->cleanOld();
  77. // Exit if background jobs are disabled!
  78. $appMode = $this->appConfig->getValueString('core', 'backgroundjobs_mode', 'ajax');
  79. if ($appMode === 'none') {
  80. throw new \RuntimeException('Background Jobs are disabled!');
  81. }
  82. if ($this->isCLI) {
  83. $this->runCli($appMode, $jobClasses);
  84. } else {
  85. $this->runWeb($appMode);
  86. }
  87. // Log the successful cron execution
  88. $this->appConfig->setValueInt('core', 'lastcron', time());
  89. }
  90. /**
  91. * @throws \RuntimeException
  92. */
  93. private function runCli(string $appMode, ?array $jobClasses): void {
  94. // set to run indefinitely if needed
  95. if (!str_contains(@ini_get('disable_functions'), 'set_time_limit')) {
  96. @set_time_limit(0);
  97. }
  98. // the cron job must be executed with the right user
  99. if (!function_exists('posix_getuid')) {
  100. throw new \RuntimeException('The posix extensions are required - see https://www.php.net/manual/en/book.posix.php');
  101. }
  102. $user = posix_getuid();
  103. $configUser = fileowner(OC::$configDir . 'config.php');
  104. if ($user !== $configUser) {
  105. throw new \RuntimeException('Console has to be executed with the user that owns the file config/config.php.' . PHP_EOL . 'Current user id: ' . $user . PHP_EOL . 'Owner id of config.php: ' . $configUser . PHP_EOL);
  106. }
  107. // We call Nextcloud from the CLI (aka cron)
  108. if ($appMode !== 'cron') {
  109. $this->appConfig->setValueString('core', 'backgroundjobs_mode', 'cron');
  110. }
  111. // Low-load hours
  112. $onlyTimeSensitive = false;
  113. $startHour = $this->config->getSystemValueInt('maintenance_window_start', 100);
  114. if ($jobClasses === null && $startHour <= 23) {
  115. $date = new \DateTime('now', new \DateTimeZone('UTC'));
  116. $currentHour = (int)$date->format('G');
  117. $endHour = $startHour + 4;
  118. if ($startHour <= 20) {
  119. // Start time: 01:00
  120. // End time: 05:00
  121. // Only run sensitive tasks when it's before the start or after the end
  122. $onlyTimeSensitive = $currentHour < $startHour || $currentHour > $endHour;
  123. } else {
  124. // Start time: 23:00
  125. // End time: 03:00
  126. $endHour -= 24; // Correct the end time from 27:00 to 03:00
  127. // Only run sensitive tasks when it's after the end and before the start
  128. $onlyTimeSensitive = $currentHour > $endHour && $currentHour < $startHour;
  129. }
  130. }
  131. // We only ask for jobs for 14 minutes, because after 5 minutes the next
  132. // system cron task should spawn and we want to have at most three
  133. // cron jobs running in parallel.
  134. $endTime = time() + 14 * 60;
  135. $executedJobs = [];
  136. while ($job = $this->jobList->getNext($onlyTimeSensitive, $jobClasses)) {
  137. if (isset($executedJobs[$job->getId()])) {
  138. $this->jobList->unlockJob($job);
  139. break;
  140. }
  141. $jobDetails = get_class($job) . ' (id: ' . $job->getId() . ', arguments: ' . json_encode($job->getArgument()) . ')';
  142. $this->logger->debug('CLI cron call has selected job ' . $jobDetails, ['app' => 'cron']);
  143. $timeBefore = time();
  144. $memoryBefore = memory_get_usage();
  145. $memoryPeakBefore = memory_get_peak_usage();
  146. $this->verboseOutput('Starting job ' . $jobDetails);
  147. $job->start($this->jobList);
  148. $timeAfter = time();
  149. $memoryAfter = memory_get_usage();
  150. $memoryPeakAfter = memory_get_peak_usage();
  151. $cronInterval = 5 * 60;
  152. $timeSpent = $timeAfter - $timeBefore;
  153. if ($timeSpent > $cronInterval) {
  154. $logLevel = match (true) {
  155. $timeSpent > $cronInterval * 128 => ILogger::FATAL,
  156. $timeSpent > $cronInterval * 64 => ILogger::ERROR,
  157. $timeSpent > $cronInterval * 16 => ILogger::WARN,
  158. $timeSpent > $cronInterval * 8 => ILogger::INFO,
  159. default => ILogger::DEBUG,
  160. };
  161. $this->logger->log(
  162. $logLevel,
  163. 'Background job ' . $jobDetails . ' ran for ' . $timeSpent . ' seconds',
  164. ['app' => 'cron']
  165. );
  166. }
  167. if ($memoryAfter - $memoryBefore > 50_000_000) {
  168. $message = 'Used memory grew by more than 50 MB when executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryAfter) . ' (before: ' . Util::humanFileSize($memoryBefore) . ')';
  169. $this->logger->warning($message, ['app' => 'cron']);
  170. $this->verboseOutput($message);
  171. }
  172. if ($memoryPeakAfter > 300_000_000 && $memoryPeakBefore <= 300_000_000) {
  173. $message = 'Cron job used more than 300 MB of ram after executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryPeakAfter) . ' (before: ' . Util::humanFileSize($memoryPeakBefore) . ')';
  174. $this->logger->warning($message, ['app' => 'cron']);
  175. $this->verboseOutput($message);
  176. }
  177. // clean up after unclean jobs
  178. $this->setupManager->tearDown();
  179. $this->tempManager->clean();
  180. $this->verboseOutput('Job ' . $jobDetails . ' done in ' . ($timeAfter - $timeBefore) . ' seconds');
  181. $this->jobList->setLastJob($job);
  182. $executedJobs[$job->getId()] = true;
  183. unset($job);
  184. if ($timeAfter > $endTime) {
  185. break;
  186. }
  187. }
  188. }
  189. private function runWeb(string $appMode): void {
  190. if ($appMode === 'cron') {
  191. // Cron is cron :-P
  192. throw new \RuntimeException('Backgroundjobs are using system cron!');
  193. } else {
  194. // Work and success :-)
  195. $job = $this->jobList->getNext();
  196. if ($job != null) {
  197. $this->logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']);
  198. $job->start($this->jobList);
  199. $this->jobList->setLastJob($job);
  200. }
  201. }
  202. }
  203. private function verboseOutput(string $message): void {
  204. if ($this->verboseCallback !== null) {
  205. call_user_func($this->verboseCallback, $message);
  206. }
  207. }
  208. }