1 | <?php |
||
2 | /** |
||
3 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
||
4 | * |
||
5 | * @author Artem Sidorenko <[email protected]> |
||
6 | * @author Christopher Schäpers <[email protected]> |
||
7 | * @author Christoph Wurst <[email protected]> |
||
8 | * @author Damjan Georgievski <[email protected]> |
||
9 | * @author Daniel Kesselberg <[email protected]> |
||
10 | * @author Jakob Sack <[email protected]> |
||
11 | * @author Joas Schilling <[email protected]> |
||
12 | * @author Jörn Friedrich Dreyer <[email protected]> |
||
13 | * @author Ko- <[email protected]> |
||
14 | * @author Michael Kuhn <[email protected]> |
||
15 | * @author Morris Jobke <[email protected]> |
||
16 | * @author Oliver Kohl D.Sc. <[email protected]> |
||
17 | * @author Robin Appelman <[email protected]> |
||
18 | * @author Roeland Jago Douma <[email protected]> |
||
19 | * @author Steffen Lindner <[email protected]> |
||
20 | * @author Thomas Müller <[email protected]> |
||
21 | * @author Vincent Petry <[email protected]> |
||
22 | * |
||
23 | * @license AGPL-3.0 |
||
24 | * |
||
25 | * This code is free software: you can redistribute it and/or modify |
||
26 | * it under the terms of the GNU Affero General Public License, version 3, |
||
27 | * as published by the Free Software Foundation. |
||
28 | * |
||
29 | * This program is distributed in the hope that it will be useful, |
||
30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
32 | * GNU Affero General Public License for more details. |
||
33 | * |
||
34 | * You should have received a copy of the GNU Affero General Public License, version 3, |
||
35 | * along with this program. If not, see <http://www.gnu.org/licenses/> |
||
36 | * |
||
37 | */ |
||
38 | |||
39 | require_once __DIR__ . '/lib/versioncheck.php'; |
||
40 | |||
41 | try { |
||
42 | require_once __DIR__ . '/lib/base.php'; |
||
43 | |||
44 | if (\OCP\Util::needUpgrade()) { |
||
45 | \OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']); |
||
46 | exit; |
||
47 | } |
||
48 | if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
||
49 | \OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); |
||
50 | exit; |
||
51 | } |
||
52 | |||
53 | // load all apps to get all api routes properly setup |
||
54 | OC_App::loadApps(); |
||
55 | |||
56 | \OC::$server->getSession()->close(); |
||
57 | |||
58 | // initialize a dummy memory session |
||
59 | $session = new \OC\Session\Memory(''); |
||
60 | $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); |
||
61 | $session = $cryptoWrapper->wrapSession($session); |
||
62 | \OC::$server->setSession($session); |
||
63 | |||
64 | $logger = \OC::$server->getLogger(); |
||
65 | $config = \OC::$server->getConfig(); |
||
66 | |||
67 | // Don't do anything if Nextcloud has not been installed |
||
68 | if (!$config->getSystemValue('installed', false)) { |
||
69 | exit(0); |
||
70 | } |
||
71 | |||
72 | \OC::$server->getTempManager()->cleanOld(); |
||
73 | |||
74 | // Exit if background jobs are disabled! |
||
75 | $appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax'); |
||
76 | if ($appMode === 'none') { |
||
77 | if (OC::$CLI) { |
||
78 | echo 'Background Jobs are disabled!' . PHP_EOL; |
||
79 | } else { |
||
80 | OC_JSON::error(['data' => ['message' => 'Background jobs disabled!']]); |
||
81 | } |
||
82 | exit(1); |
||
83 | } |
||
84 | |||
85 | if (OC::$CLI) { |
||
86 | // set to run indefinitely if needed |
||
87 | if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
||
88 | @set_time_limit(0); |
||
0 ignored issues
–
show
|
|||
89 | } |
||
90 | |||
91 | // the cron job must be executed with the right user |
||
92 | if (!function_exists('posix_getuid')) { |
||
93 | echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL; |
||
94 | exit(1); |
||
95 | } |
||
96 | |||
97 | $user = posix_getuid(); |
||
98 | $configUser = fileowner(OC::$configDir . 'config.php'); |
||
99 | if ($user !== $configUser) { |
||
100 | echo "Console has to be executed with the user that owns the file config/config.php" . PHP_EOL; |
||
101 | echo "Current user id: " . $user . PHP_EOL; |
||
102 | echo "Owner id of config.php: " . $configUser . PHP_EOL; |
||
103 | exit(1); |
||
104 | } |
||
105 | |||
106 | |||
107 | // We call Nextcloud from the CLI (aka cron) |
||
108 | if ($appMode !== 'cron') { |
||
109 | $config->setAppValue('core', 'backgroundjobs_mode', 'cron'); |
||
110 | } |
||
111 | |||
112 | // Work |
||
113 | $jobList = \OC::$server->getJobList(); |
||
114 | |||
115 | // We only ask for jobs for 14 minutes, because after 5 minutes the next |
||
116 | // system cron task should spawn and we want to have at most three |
||
117 | // cron jobs running in parallel. |
||
118 | $endTime = time() + 14 * 60; |
||
119 | |||
120 | $executedJobs = []; |
||
121 | while ($job = $jobList->getNext()) { |
||
122 | if (isset($executedJobs[$job->getId()])) { |
||
123 | $jobList->unlockJob($job); |
||
124 | break; |
||
125 | } |
||
126 | |||
127 | $job->execute($jobList, $logger); |
||
128 | // clean up after unclean jobs |
||
129 | \OC_Util::tearDownFS(); |
||
130 | |||
131 | $jobList->setLastJob($job); |
||
132 | $executedJobs[$job->getId()] = true; |
||
133 | unset($job); |
||
134 | |||
135 | if (time() > $endTime) { |
||
136 | break; |
||
137 | } |
||
138 | } |
||
139 | } else { |
||
140 | // We call cron.php from some website |
||
141 | if ($appMode === 'cron') { |
||
142 | // Cron is cron :-P |
||
143 | OC_JSON::error(['data' => ['message' => 'Backgroundjobs are using system cron!']]); |
||
144 | } else { |
||
145 | // Work and success :-) |
||
146 | $jobList = \OC::$server->getJobList(); |
||
147 | $job = $jobList->getNext(); |
||
148 | if ($job != null) { |
||
149 | $job->execute($jobList, $logger); |
||
150 | $jobList->setLastJob($job); |
||
151 | } |
||
152 | OC_JSON::success(); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | // Log the successful cron execution |
||
157 | $config->setAppValue('core', 'lastcron', time()); |
||
158 | exit(); |
||
159 | } catch (Exception $ex) { |
||
160 | \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); |
||
161 | } catch (Error $ex) { |
||
162 | \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); |
||
163 | } |
||
164 |
If you suppress an error, we recommend checking for the error condition explicitly: