@@ -38,124 +38,124 @@ |
||
| 38 | 38 | |
| 39 | 39 | try { |
| 40 | 40 | |
| 41 | - require_once __DIR__ . '/lib/base.php'; |
|
| 42 | - |
|
| 43 | - if (\OCP\Util::needUpgrade()) { |
|
| 44 | - \OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']); |
|
| 45 | - exit; |
|
| 46 | - } |
|
| 47 | - if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 48 | - \OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); |
|
| 49 | - exit; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - // load all apps to get all api routes properly setup |
|
| 53 | - OC_App::loadApps(); |
|
| 54 | - |
|
| 55 | - \OC::$server->getSession()->close(); |
|
| 56 | - |
|
| 57 | - // initialize a dummy memory session |
|
| 58 | - $session = new \OC\Session\Memory(''); |
|
| 59 | - $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); |
|
| 60 | - $session = $cryptoWrapper->wrapSession($session); |
|
| 61 | - \OC::$server->setSession($session); |
|
| 62 | - |
|
| 63 | - $logger = \OC::$server->getLogger(); |
|
| 64 | - $config = \OC::$server->getConfig(); |
|
| 65 | - |
|
| 66 | - // Don't do anything if Nextcloud has not been installed |
|
| 67 | - if (!$config->getSystemValue('installed', false)) { |
|
| 68 | - exit(0); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - \OC::$server->getTempManager()->cleanOld(); |
|
| 72 | - |
|
| 73 | - // Exit if background jobs are disabled! |
|
| 74 | - $appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax'); |
|
| 75 | - if ($appMode === 'none') { |
|
| 76 | - if (OC::$CLI) { |
|
| 77 | - echo 'Background Jobs are disabled!' . PHP_EOL; |
|
| 78 | - } else { |
|
| 79 | - OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!'))); |
|
| 80 | - } |
|
| 81 | - exit(1); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - if (OC::$CLI) { |
|
| 85 | - // set to run indefinitely if needed |
|
| 86 | - if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
|
| 87 | - @set_time_limit(0); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - // the cron job must be executed with the right user |
|
| 91 | - if (!function_exists('posix_getuid')) { |
|
| 92 | - echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL; |
|
| 93 | - exit(1); |
|
| 94 | - } |
|
| 95 | - $user = posix_getpwuid(posix_getuid()); |
|
| 96 | - $configUser = posix_getpwuid(fileowner(OC::$configDir . 'config.php')); |
|
| 97 | - if ($user['name'] !== $configUser['name']) { |
|
| 98 | - echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL; |
|
| 99 | - echo "Current user: " . $user['name'] . PHP_EOL; |
|
| 100 | - echo "Web server user: " . $configUser['name'] . PHP_EOL; |
|
| 101 | - exit(1); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - // We call Nextcloud from the CLI (aka cron) |
|
| 105 | - if ($appMode !== 'cron') { |
|
| 106 | - $config->setAppValue('core', 'backgroundjobs_mode', 'cron'); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // Work |
|
| 110 | - $jobList = \OC::$server->getJobList(); |
|
| 111 | - |
|
| 112 | - // We only ask for jobs for 14 minutes, because after 15 minutes the next |
|
| 113 | - // system cron task should spawn. |
|
| 114 | - $endTime = time() + 14 * 60; |
|
| 115 | - |
|
| 116 | - $executedJobs = []; |
|
| 117 | - while ($job = $jobList->getNext()) { |
|
| 118 | - if (isset($executedJobs[$job->getId()])) { |
|
| 119 | - $jobList->unlockJob($job); |
|
| 120 | - break; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $job->execute($jobList, $logger); |
|
| 124 | - // clean up after unclean jobs |
|
| 125 | - \OC_Util::tearDownFS(); |
|
| 126 | - |
|
| 127 | - $jobList->setLastJob($job); |
|
| 128 | - $executedJobs[$job->getId()] = true; |
|
| 129 | - unset($job); |
|
| 130 | - |
|
| 131 | - if (time() > $endTime) { |
|
| 132 | - break; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - } else { |
|
| 137 | - // We call cron.php from some website |
|
| 138 | - if ($appMode === 'cron') { |
|
| 139 | - // Cron is cron :-P |
|
| 140 | - OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!'))); |
|
| 141 | - } else { |
|
| 142 | - // Work and success :-) |
|
| 143 | - $jobList = \OC::$server->getJobList(); |
|
| 144 | - $job = $jobList->getNext(); |
|
| 145 | - if ($job != null) { |
|
| 146 | - $job->execute($jobList, $logger); |
|
| 147 | - $jobList->setLastJob($job); |
|
| 148 | - } |
|
| 149 | - OC_JSON::success(); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - // Log the successful cron execution |
|
| 154 | - $config->setAppValue('core', 'lastcron', time()); |
|
| 155 | - exit(); |
|
| 41 | + require_once __DIR__ . '/lib/base.php'; |
|
| 42 | + |
|
| 43 | + if (\OCP\Util::needUpgrade()) { |
|
| 44 | + \OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']); |
|
| 45 | + exit; |
|
| 46 | + } |
|
| 47 | + if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 48 | + \OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); |
|
| 49 | + exit; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + // load all apps to get all api routes properly setup |
|
| 53 | + OC_App::loadApps(); |
|
| 54 | + |
|
| 55 | + \OC::$server->getSession()->close(); |
|
| 56 | + |
|
| 57 | + // initialize a dummy memory session |
|
| 58 | + $session = new \OC\Session\Memory(''); |
|
| 59 | + $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); |
|
| 60 | + $session = $cryptoWrapper->wrapSession($session); |
|
| 61 | + \OC::$server->setSession($session); |
|
| 62 | + |
|
| 63 | + $logger = \OC::$server->getLogger(); |
|
| 64 | + $config = \OC::$server->getConfig(); |
|
| 65 | + |
|
| 66 | + // Don't do anything if Nextcloud has not been installed |
|
| 67 | + if (!$config->getSystemValue('installed', false)) { |
|
| 68 | + exit(0); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + \OC::$server->getTempManager()->cleanOld(); |
|
| 72 | + |
|
| 73 | + // Exit if background jobs are disabled! |
|
| 74 | + $appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax'); |
|
| 75 | + if ($appMode === 'none') { |
|
| 76 | + if (OC::$CLI) { |
|
| 77 | + echo 'Background Jobs are disabled!' . PHP_EOL; |
|
| 78 | + } else { |
|
| 79 | + OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!'))); |
|
| 80 | + } |
|
| 81 | + exit(1); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + if (OC::$CLI) { |
|
| 85 | + // set to run indefinitely if needed |
|
| 86 | + if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
|
| 87 | + @set_time_limit(0); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + // the cron job must be executed with the right user |
|
| 91 | + if (!function_exists('posix_getuid')) { |
|
| 92 | + echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL; |
|
| 93 | + exit(1); |
|
| 94 | + } |
|
| 95 | + $user = posix_getpwuid(posix_getuid()); |
|
| 96 | + $configUser = posix_getpwuid(fileowner(OC::$configDir . 'config.php')); |
|
| 97 | + if ($user['name'] !== $configUser['name']) { |
|
| 98 | + echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL; |
|
| 99 | + echo "Current user: " . $user['name'] . PHP_EOL; |
|
| 100 | + echo "Web server user: " . $configUser['name'] . PHP_EOL; |
|
| 101 | + exit(1); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + // We call Nextcloud from the CLI (aka cron) |
|
| 105 | + if ($appMode !== 'cron') { |
|
| 106 | + $config->setAppValue('core', 'backgroundjobs_mode', 'cron'); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // Work |
|
| 110 | + $jobList = \OC::$server->getJobList(); |
|
| 111 | + |
|
| 112 | + // We only ask for jobs for 14 minutes, because after 15 minutes the next |
|
| 113 | + // system cron task should spawn. |
|
| 114 | + $endTime = time() + 14 * 60; |
|
| 115 | + |
|
| 116 | + $executedJobs = []; |
|
| 117 | + while ($job = $jobList->getNext()) { |
|
| 118 | + if (isset($executedJobs[$job->getId()])) { |
|
| 119 | + $jobList->unlockJob($job); |
|
| 120 | + break; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $job->execute($jobList, $logger); |
|
| 124 | + // clean up after unclean jobs |
|
| 125 | + \OC_Util::tearDownFS(); |
|
| 126 | + |
|
| 127 | + $jobList->setLastJob($job); |
|
| 128 | + $executedJobs[$job->getId()] = true; |
|
| 129 | + unset($job); |
|
| 130 | + |
|
| 131 | + if (time() > $endTime) { |
|
| 132 | + break; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + } else { |
|
| 137 | + // We call cron.php from some website |
|
| 138 | + if ($appMode === 'cron') { |
|
| 139 | + // Cron is cron :-P |
|
| 140 | + OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!'))); |
|
| 141 | + } else { |
|
| 142 | + // Work and success :-) |
|
| 143 | + $jobList = \OC::$server->getJobList(); |
|
| 144 | + $job = $jobList->getNext(); |
|
| 145 | + if ($job != null) { |
|
| 146 | + $job->execute($jobList, $logger); |
|
| 147 | + $jobList->setLastJob($job); |
|
| 148 | + } |
|
| 149 | + OC_JSON::success(); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + // Log the successful cron execution |
|
| 154 | + $config->setAppValue('core', 'lastcron', time()); |
|
| 155 | + exit(); |
|
| 156 | 156 | |
| 157 | 157 | } catch (Exception $ex) { |
| 158 | - \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); |
|
| 158 | + \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); |
|
| 159 | 159 | } catch (Error $ex) { |
| 160 | - \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); |
|
| 160 | + \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); |
|
| 161 | 161 | } |