|
1 | 1 | <?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
2 | 5 | /** |
3 | 6 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
4 | 7 | * |
5 | 8 | * @author Artem Sidorenko <artem@posteo.de> |
6 | 9 | * @author Christopher Schäpers <kondou@ts.unde.re> |
7 | 10 | * @author Christoph Wurst <christoph@winzerhof-wurst.at> |
| 11 | + * @author Côme Chilliet <come.chilliet@nextcloud.com> |
8 | 12 | * @author Daniel Kesselberg <mail@danielkesselberg.de> |
9 | 13 | * @author hoellen <dev@hoellen.eu> |
10 | 14 | * @author J0WI <J0WI@users.noreply.github.com> |
|
37 | 41 | * along with this program. If not, see <http://www.gnu.org/licenses/> |
38 | 42 | * |
39 | 43 | */ |
| 44 | + |
40 | 45 | require_once __DIR__ . '/lib/versioncheck.php'; |
41 | 46 |
|
| 47 | +use OCP\App\IAppManager; |
| 48 | +use OCP\BackgroundJob\IJobList; |
| 49 | +use OCP\IAppConfig; |
| 50 | +use OCP\IConfig; |
| 51 | +use OCP\ISession; |
| 52 | +use OCP\ITempManager; |
| 53 | +use OCP\Server; |
| 54 | +use OCP\Util; |
| 55 | +use Psr\Log\LoggerInterface; |
| 56 | + |
42 | 57 | try { |
43 | 58 | require_once __DIR__ . '/lib/base.php'; |
44 | 59 |
|
45 | | - if (\OCP\Util::needUpgrade()) { |
46 | | - \OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']); |
| 60 | + if (Util::needUpgrade()) { |
| 61 | + Server::get(LoggerInterface::class)->debug('Update required, skipping cron', ['app' => 'cron']); |
47 | 62 | exit; |
48 | 63 | } |
49 | | - if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
50 | | - \OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); |
| 64 | + |
| 65 | + $config = Server::get(IConfig::class); |
| 66 | + |
| 67 | + if ($config->getSystemValueBool('maintenance', false)) { |
| 68 | + Server::get(LoggerInterface::class)->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); |
51 | 69 | exit; |
52 | 70 | } |
53 | 71 |
|
| 72 | + // Don't do anything if Nextcloud has not been installed |
| 73 | + if (!$config->getSystemValueBool('installed', false)) { |
| 74 | + exit(0); |
| 75 | + } |
| 76 | + |
54 | 77 | // load all apps to get all api routes properly setup |
55 | | - OC_App::loadApps(); |
| 78 | + Server::get(IAppManager::class)->loadApps(); |
56 | 79 |
|
57 | | - \OC::$server->getSession()->close(); |
| 80 | + Server::get(ISession::class)->close(); |
58 | 81 |
|
59 | 82 | // initialize a dummy memory session |
60 | 83 | $session = new \OC\Session\Memory(''); |
61 | 84 | $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); |
62 | 85 | $session = $cryptoWrapper->wrapSession($session); |
63 | 86 | \OC::$server->setSession($session); |
64 | 87 |
|
65 | | - $logger = \OC::$server->getLogger(); |
66 | | - $config = \OC::$server->getConfig(); |
67 | | - $tempManager = \OC::$server->getTempManager(); |
68 | | - |
69 | | - // Don't do anything if Nextcloud has not been installed |
70 | | - if (!$config->getSystemValue('installed', false)) { |
71 | | - exit(0); |
72 | | - } |
| 88 | + $logger = Server::get(LoggerInterface::class); |
| 89 | + $appConfig = Server::get(IAppConfig::class); |
| 90 | + $tempManager = Server::get(ITempManager::class); |
73 | 91 |
|
74 | 92 | $tempManager->cleanOld(); |
75 | 93 |
|
76 | 94 | // Exit if background jobs are disabled! |
77 | | - $appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax'); |
| 95 | + $appMode = $appConfig->getValueString('core', 'backgroundjobs_mode', 'ajax'); |
78 | 96 | if ($appMode === 'none') { |
79 | 97 | if (OC::$CLI) { |
80 | 98 | echo 'Background Jobs are disabled!' . PHP_EOL; |
|
108 | 126 |
|
109 | 127 | // We call Nextcloud from the CLI (aka cron) |
110 | 128 | if ($appMode !== 'cron') { |
111 | | - $config->setAppValue('core', 'backgroundjobs_mode', 'cron'); |
| 129 | + $appConfig->setValueString('core', 'backgroundjobs_mode', 'cron'); |
112 | 130 | } |
113 | 131 |
|
114 | 132 | // Low-load hours |
|
134 | 152 | } |
135 | 153 |
|
136 | 154 | // Work |
137 | | - $jobList = \OC::$server->getJobList(); |
| 155 | + $jobList = Server::get(IJobList::class); |
138 | 156 |
|
139 | 157 | // We only ask for jobs for 14 minutes, because after 5 minutes the next |
140 | 158 | // system cron task should spawn and we want to have at most three |
|
160 | 178 | $memoryPeakAfter = memory_get_peak_usage(); |
161 | 179 |
|
162 | 180 | if ($memoryAfter - $memoryBefore > 10_000_000) { |
163 | | - $logger->warning('Used memory grew by more than 10 MB when executing job ' . $jobDetails . ': ' . \OCP\Util::humanFileSize($memoryAfter). ' (before: ' . \OCP\Util::humanFileSize($memoryBefore) . ')', ['app' => 'cron']); |
| 181 | + $logger->warning('Used memory grew by more than 10 MB when executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryAfter). ' (before: ' . Util::humanFileSize($memoryBefore) . ')', ['app' => 'cron']); |
164 | 182 | } |
165 | 183 | if ($memoryPeakAfter > 300_000_000) { |
166 | | - $logger->warning('Cron job used more than 300 MB of ram after executing job ' . $jobDetails . ': ' . \OCP\Util::humanFileSize($memoryPeakAfter) . ' (before: ' . \OCP\Util::humanFileSize($memoryPeakBefore) . ')', ['app' => 'cron']); |
| 184 | + $logger->warning('Cron job used more than 300 MB of ram after executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryPeakAfter) . ' (before: ' . Util::humanFileSize($memoryPeakBefore) . ')', ['app' => 'cron']); |
167 | 185 | } |
168 | 186 |
|
169 | 187 | // clean up after unclean jobs |
170 | | - \OC_Util::tearDownFS(); |
| 188 | + Server::get(\OC\Files\SetupManager::class)->tearDown(); |
171 | 189 | $tempManager->clean(); |
172 | 190 |
|
173 | 191 | $jobList->setLastJob($job); |
|
185 | 203 | OC_JSON::error(['data' => ['message' => 'Backgroundjobs are using system cron!']]); |
186 | 204 | } else { |
187 | 205 | // Work and success :-) |
188 | | - $jobList = \OC::$server->getJobList(); |
| 206 | + $jobList = Server::get(IJobList::class); |
189 | 207 | $job = $jobList->getNext(); |
190 | 208 | if ($job != null) { |
191 | 209 | $logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']); |
|
197 | 215 | } |
198 | 216 |
|
199 | 217 | // Log the successful cron execution |
200 | | - $config->setAppValue('core', 'lastcron', time()); |
| 218 | + $appConfig->setValueInt('core', 'lastcron', time()); |
201 | 219 | exit(); |
202 | 220 | } catch (Exception $ex) { |
203 | | - \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); |
| 221 | + Server::get(LoggerInterface::class)->error( |
| 222 | + $ex->getMessage(), |
| 223 | + ['app' => 'cron', 'exception' => $ex] |
| 224 | + ); |
204 | 225 | echo $ex . PHP_EOL; |
205 | 226 | exit(1); |
206 | 227 | } catch (Error $ex) { |
207 | | - \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); |
| 228 | + Server::get(LoggerInterface::class)->error( |
| 229 | + $ex->getMessage(), |
| 230 | + ['app' => 'cron', 'exception' => $ex] |
| 231 | + ); |
208 | 232 | echo $ex . PHP_EOL; |
209 | 233 | exit(1); |
210 | 234 | } |
0 commit comments