123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- require_once __DIR__ . '/lib/versioncheck.php';
- try {
- require_once __DIR__ . '/lib/base.php';
- if (\OCP\Util::needUpgrade()) {
- \OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']);
- exit;
- }
- if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
- \OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']);
- exit;
- }
-
- OC_App::loadApps();
- \OC::$server->getSession()->close();
-
- $session = new \OC\Session\Memory('');
- $cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
- $session = $cryptoWrapper->wrapSession($session);
- \OC::$server->setSession($session);
- $logger = \OC::$server->getLogger();
- $config = \OC::$server->getConfig();
- $tempManager = \OC::$server->getTempManager();
-
- if (!$config->getSystemValue('installed', false)) {
- exit(0);
- }
- $tempManager->cleanOld();
-
- $appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax');
- if ($appMode === 'none') {
- if (OC::$CLI) {
- echo 'Background Jobs are disabled!' . PHP_EOL;
- } else {
- OC_JSON::error(['data' => ['message' => 'Background jobs disabled!']]);
- }
- exit(1);
- }
- if (OC::$CLI) {
-
- if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
- @set_time_limit(0);
- }
-
- if (!function_exists('posix_getuid')) {
- echo "The posix extensions are required - see https://www.php.net/manual/en/book.posix.php" . PHP_EOL;
- exit(1);
- }
- $user = posix_getuid();
- $configUser = fileowner(OC::$configDir . 'config.php');
- if ($user !== $configUser) {
- echo "Console has to be executed with the user that owns the file config/config.php" . PHP_EOL;
- echo "Current user id: " . $user . PHP_EOL;
- echo "Owner id of config.php: " . $configUser . PHP_EOL;
- exit(1);
- }
-
- if ($appMode !== 'cron') {
- $config->setAppValue('core', 'backgroundjobs_mode', 'cron');
- }
-
- $onlyTimeSensitive = false;
- $startHour = $config->getSystemValueInt('maintenance_window_start', 100);
- if ($startHour <= 23) {
- $date = new \DateTime('now', new \DateTimeZone('UTC'));
- $currentHour = (int) $date->format('G');
- $endHour = $startHour + 4;
- if ($startHour <= 20) {
-
-
-
- $onlyTimeSensitive = $currentHour < $startHour || $currentHour > $endHour;
- } else {
-
-
- $endHour -= 24;
-
- $onlyTimeSensitive = $currentHour > $endHour && $currentHour < $startHour;
- }
- }
-
- $jobList = \OC::$server->getJobList();
-
-
-
- $endTime = time() + 14 * 60;
- $executedJobs = [];
- while ($job = $jobList->getNext($onlyTimeSensitive)) {
- if (isset($executedJobs[$job->getId()])) {
- $jobList->unlockJob($job);
- break;
- }
- $jobDetails = get_class($job) . ' (id: ' . $job->getId() . ', arguments: ' . json_encode($job->getArgument()) . ')';
- $logger->debug('CLI cron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']);
- $timeBefore = time();
- $job->execute($jobList, $logger);
- $timeAfter = time();
- $cronInterval = 5 * 60;
- $timeSpent = $timeAfter - $timeBefore;
- if ($timeSpent > $cronInterval) {
- $logLevel = match (true) {
- $timeSpent > $cronInterval * 128 => \OCP\ILogger::FATAL,
- $timeSpent > $cronInterval * 64 => \OCP\ILogger::ERROR,
- $timeSpent > $cronInterval * 16 => \OCP\ILogger::WARN,
- $timeSpent > $cronInterval * 8 => \OCP\ILogger::INFO,
- default => \OCP\ILogger::DEBUG,
- };
- $logger->log(
- $logLevel,
- 'Background job ' . $jobDetails . ' ran for ' . $timeSpent . ' seconds',
- ['app' => 'cron']
- );
- }
-
- \OC_Util::tearDownFS();
- $tempManager->clean();
- $jobList->setLastJob($job);
- $executedJobs[$job->getId()] = true;
- unset($job);
- if ($timeAfter > $endTime) {
- break;
- }
- }
- } else {
-
- if ($appMode === 'cron') {
-
- OC_JSON::error(['data' => ['message' => 'Backgroundjobs are using system cron!']]);
- } else {
-
- $jobList = \OC::$server->getJobList();
- $job = $jobList->getNext();
- if ($job != null) {
- $logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']);
- $job->execute($jobList, $logger);
- $jobList->setLastJob($job);
- }
- OC_JSON::success();
- }
- }
-
- $config->setAppValue('core', 'lastcron', time());
- exit();
- } catch (Exception $ex) {
- \OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
- echo $ex . PHP_EOL;
- exit(1);
- } catch (Error $ex) {
- \OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
- echo $ex . PHP_EOL;
- exit(1);
- }
|