MDL-68714 clilib: Allow cli processes to set a process title suffix
[moodle.git] / lib / cronlib.php
CommitLineData
28bd3d9a 1<?php
28bd3d9a
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Cron functions.
19 *
20 * @package core
21 * @subpackage admin
22 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
50ed196e
PS
26/**
27 * Execute cron tasks
28 */
28bd3d9a
PS
29function cron_run() {
30 global $DB, $CFG, $OUTPUT;
31
9957af68
PS
32 if (CLI_MAINTENANCE) {
33 echo "CLI maintenance mode active, cron execution suspended.\n";
34 exit(1);
35 }
36
4c56c7bf
PS
37 if (moodle_needs_upgrading()) {
38 echo "Moodle upgrade pending, cron execution suspended.\n";
39 exit(1);
40 }
41
28bd3d9a 42 require_once($CFG->libdir.'/adminlib.php');
28bd3d9a
PS
43
44 if (!empty($CFG->showcronsql)) {
45 $DB->set_debug(true);
46 }
47 if (!empty($CFG->showcrondebugging)) {
96f81ea3 48 set_debugging(DEBUG_DEVELOPER, true);
28bd3d9a
PS
49 }
50
3ef7279f 51 core_php_time_limit::raise();
28bd3d9a
PS
52 $starttime = microtime();
53
50ed196e 54 // Increase memory limit
346c5887 55 raise_memory_limit(MEMORY_EXTRA);
28bd3d9a 56
50ed196e 57 // Emulate normal session - we use admin accoutn by default
28bd3d9a
PS
58 cron_setup_user();
59
50ed196e 60 // Start output log
28bd3d9a 61 $timenow = time();
658b9372 62 mtrace("Server Time: ".date('r', $timenow)."\n\n");
28bd3d9a 63
b18034ed 64 // Record start time and interval between the last cron runs.
65 $laststart = get_config('tool_task', 'lastcronstart');
66 set_config('lastcronstart', $timenow, 'tool_task');
67 if ($laststart) {
68 // Record the interval between last two runs (always store at least 1 second).
69 set_config('lastcroninterval', max(1, $timenow - $laststart), 'tool_task');
70 }
71
309ae892 72 // Run all scheduled tasks.
23d67562 73 cron_run_scheduled_tasks($timenow);
50ed196e 74
24bfd53c
AN
75 // Run adhoc tasks.
76 cron_run_adhoc_tasks($timenow);
50ed196e 77
28bd3d9a
PS
78 mtrace("Cron script completed correctly");
79
658b9372
TH
80 gc_collect_cycles();
81 mtrace('Cron completed at ' . date('H:i:s') . '. Memory used ' . display_size(memory_get_usage()) . '.');
28bd3d9a
PS
82 $difftime = microtime_diff($starttime, microtime());
83 mtrace("Execution took ".$difftime." seconds");
cbb9e9b8 84}
b5541735 85
23d67562
AN
86/**
87 * Execute all queued scheduled tasks, applying necessary concurrency limits and time limits.
88 *
89 * @param int $timenow The time this process started.
6b2e15f6 90 * @throws \moodle_exception
23d67562
AN
91 */
92function cron_run_scheduled_tasks(int $timenow) {
93 // Allow a restriction on the number of scheduled task runners at once.
94 $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
95 $maxruns = get_config('core', 'task_scheduled_concurrency_limit');
96 $maxruntime = get_config('core', 'task_scheduled_max_runtime');
97
98 $scheduledlock = null;
99 for ($run = 0; $run < $maxruns; $run++) {
286cc88c
BH
100 // If we can't get a lock instantly it means runner N is already running
101 // so fail as fast as possible and try N+1 so we don't limit the speed at
102 // which we bring new runners into the pool.
103 if ($scheduledlock = $cronlockfactory->get_lock("scheduled_task_runner_{$run}", 0)) {
23d67562
AN
104 break;
105 }
106 }
107
108 if (!$scheduledlock) {
109 mtrace("Skipping processing of scheduled tasks. Concurrency limit reached.");
110 return;
111 }
112
113 $starttime = time();
114
115 // Run all scheduled tasks.
6b2e15f6 116 try {
b15c53f4
BH
117 while (!\core\local\cli\shutdown::should_gracefully_exit() &&
118 !\core\task\manager::static_caches_cleared_since($timenow) &&
6b2e15f6
MG
119 $task = \core\task\manager::get_next_scheduled_task($timenow)) {
120 cron_run_inner_scheduled_task($task);
121 unset($task);
23d67562 122
6b2e15f6
MG
123 if ((time() - $starttime) > $maxruntime) {
124 mtrace("Stopping processing of scheduled tasks as time limit has been reached.");
125 break;
126 }
23d67562 127 }
6b2e15f6
MG
128 } finally {
129 // Release the scheduled task runner lock.
130 $scheduledlock->release();
23d67562 131 }
23d67562
AN
132}
133
24bfd53c
AN
134/**
135 * Execute all queued adhoc tasks, applying necessary concurrency limits and time limits.
136 *
137 * @param int $timenow The time this process started.
4234159e
MG
138 * @param int $keepalive Keep this function alive for N seconds and poll for new adhoc tasks.
139 * @param bool $checklimits Should we check limits?
6b2e15f6 140 * @throws \moodle_exception
24bfd53c 141 */
4234159e 142function cron_run_adhoc_tasks(int $timenow, $keepalive = 0, $checklimits = true) {
24bfd53c
AN
143 // Allow a restriction on the number of adhoc task runners at once.
144 $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
145 $maxruns = get_config('core', 'task_adhoc_concurrency_limit');
146 $maxruntime = get_config('core', 'task_adhoc_max_runtime');
147
4234159e
MG
148 if ($checklimits) {
149 $adhoclock = null;
150 for ($run = 0; $run < $maxruns; $run++) {
286cc88c
BH
151 // If we can't get a lock instantly it means runner N is already running
152 // so fail as fast as possible and try N+1 so we don't limit the speed at
153 // which we bring new runners into the pool.
154 if ($adhoclock = $cronlockfactory->get_lock("adhoc_task_runner_{$run}", 0)) {
4234159e
MG
155 break;
156 }
24bfd53c 157 }
24bfd53c 158
4234159e
MG
159 if (!$adhoclock) {
160 mtrace("Skipping processing of adhoc tasks. Concurrency limit reached.");
161 return;
162 }
24bfd53c
AN
163 }
164
4234159e
MG
165 $humantimenow = date('r', $timenow);
166 $finishtime = $timenow + $keepalive;
167 $waiting = false;
168 $taskcount = 0;
24bfd53c
AN
169
170 // Run all adhoc tasks.
b15c53f4
BH
171 while (!\core\local\cli\shutdown::should_gracefully_exit() &&
172 !\core\task\manager::static_caches_cleared_since($timenow)) {
24bfd53c 173
4234159e
MG
174 if ($checklimits && (time() - $timenow) >= $maxruntime) {
175 if ($waiting) {
176 $waiting = false;
177 mtrace('');
178 }
24bfd53c
AN
179 mtrace("Stopping processing of adhoc tasks as time limit has been reached.");
180 break;
181 }
4234159e 182
6b2e15f6 183 try {
217a9b1c 184 $task = \core\task\manager::get_next_adhoc_task(time(), $checklimits);
6b2e15f6
MG
185 } catch (Exception $e) {
186 if ($adhoclock) {
187 // Release the adhoc task runner lock.
188 $adhoclock->release();
189 }
190 throw $e;
191 }
4234159e
MG
192
193 if ($task) {
194 if ($waiting) {
195 mtrace('');
196 }
197 $waiting = false;
198 cron_run_inner_adhoc_task($task);
199 $taskcount++;
200 unset($task);
201 } else {
202 if (time() >= $finishtime) {
203 break;
204 }
205 if (!$waiting) {
206 mtrace('Waiting for more adhoc tasks to be queued ', '');
207 } else {
208 mtrace('.', '');
209 }
210 $waiting = true;
211 sleep(1);
212 }
24bfd53c
AN
213 }
214
4234159e
MG
215 if ($waiting) {
216 mtrace('');
217 }
218
219 mtrace("Ran {$taskcount} adhoc tasks found at {$humantimenow}");
220
221 if ($adhoclock) {
222 // Release the adhoc task runner lock.
223 $adhoclock->release();
224 }
24bfd53c
AN
225}
226
38fa1ca5 227/**
228 * Shared code that handles running of a single scheduled task within the cron.
229 *
230 * Not intended for calling directly outside of this library!
231 *
232 * @param \core\task\task_base $task
233 */
234function cron_run_inner_scheduled_task(\core\task\task_base $task) {
235 global $CFG, $DB;
236
4b71596f
AN
237 \core\task\logmanager::start_logging($task);
238
38fa1ca5 239 $fullname = $task->get_name() . ' (' . get_class($task) . ')';
240 mtrace('Execute scheduled task: ' . $fullname);
241 cron_trace_time_and_memory();
242 $predbqueries = null;
243 $predbqueries = $DB->perf_get_queries();
244 $pretime = microtime(1);
245 try {
246 get_mailer('buffer');
cd305dc2 247 cron_prepare_core_renderer();
38fa1ca5 248 $task->execute();
249 if ($DB->is_transaction_started()) {
250 throw new coding_exception("Task left transaction open");
251 }
252 if (isset($predbqueries)) {
253 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
254 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
255 }
256 mtrace('Scheduled task complete: ' . $fullname);
257 \core\task\manager::scheduled_task_complete($task);
258 } catch (Exception $e) {
259 if ($DB && $DB->is_transaction_started()) {
260 error_log('Database transaction aborted automatically in ' . get_class($task));
261 $DB->force_transaction_rollback();
262 }
263 if (isset($predbqueries)) {
264 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
265 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
266 }
267 mtrace('Scheduled task failed: ' . $fullname . ',' . $e->getMessage());
268 if ($CFG->debugdeveloper) {
269 if (!empty($e->debuginfo)) {
270 mtrace("Debug info:");
271 mtrace($e->debuginfo);
272 }
273 mtrace("Backtrace:");
274 mtrace(format_backtrace($e->getTrace(), true));
275 }
276 \core\task\manager::scheduled_task_failed($task);
cd305dc2 277 } finally {
4ac6c3d4
JF
278 // Reset back to the standard admin user.
279 cron_setup_user();
cd305dc2 280 cron_prepare_core_renderer(true);
38fa1ca5 281 }
282 get_mailer('close');
283}
284
7e4c4b6f
AN
285/**
286 * Shared code that handles running of a single adhoc task within the cron.
287 *
288 * @param \core\task\adhoc_task $task
289 */
290function cron_run_inner_adhoc_task(\core\task\adhoc_task $task) {
291 global $DB, $CFG;
4b71596f
AN
292
293 \core\task\logmanager::start_logging($task);
294
7e4c4b6f
AN
295 mtrace("Execute adhoc task: " . get_class($task));
296 cron_trace_time_and_memory();
297 $predbqueries = null;
298 $predbqueries = $DB->perf_get_queries();
299 $pretime = microtime(1);
300
301 if ($userid = $task->get_userid()) {
302 // This task has a userid specified.
303 if ($user = \core_user::get_user($userid)) {
304 // User found. Check that they are suitable.
305 try {
306 \core_user::require_active_user($user, true, true);
307 } catch (moodle_exception $e) {
308 mtrace("User {$userid} cannot be used to run an adhoc task: " . get_class($task) . ". Cancelling task.");
309 $user = null;
310 }
311 } else {
312 // Unable to find the user for this task.
313 // A user missing in the database will never reappear.
314 mtrace("User {$userid} could not be found for adhoc task: " . get_class($task) . ". Cancelling task.");
315 }
316
317 if (empty($user)) {
318 // A user missing in the database will never reappear so the task needs to be failed to ensure that locks are removed,
319 // and then removed to prevent future runs.
320 // A task running as a user should only be run as that user.
321 \core\task\manager::adhoc_task_failed($task);
322 $DB->delete_records('task_adhoc', ['id' => $task->get_id()]);
323
324 return;
325 }
326
327 cron_setup_user($user);
328 }
329
330 try {
331 get_mailer('buffer');
cd305dc2 332 cron_prepare_core_renderer();
7e4c4b6f
AN
333 $task->execute();
334 if ($DB->is_transaction_started()) {
335 throw new coding_exception("Task left transaction open");
336 }
337 if (isset($predbqueries)) {
338 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
339 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
340 }
341 mtrace("Adhoc task complete: " . get_class($task));
342 \core\task\manager::adhoc_task_complete($task);
343 } catch (Exception $e) {
344 if ($DB && $DB->is_transaction_started()) {
345 error_log('Database transaction aborted automatically in ' . get_class($task));
346 $DB->force_transaction_rollback();
347 }
348 if (isset($predbqueries)) {
349 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
350 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
351 }
352 mtrace("Adhoc task failed: " . get_class($task) . "," . $e->getMessage());
353 if ($CFG->debugdeveloper) {
354 if (!empty($e->debuginfo)) {
355 mtrace("Debug info:");
356 mtrace($e->debuginfo);
357 }
358 mtrace("Backtrace:");
359 mtrace(format_backtrace($e->getTrace(), true));
360 }
361 \core\task\manager::adhoc_task_failed($task);
362 } finally {
363 // Reset back to the standard admin user.
364 cron_setup_user();
cd305dc2 365 cron_prepare_core_renderer(true);
7e4c4b6f
AN
366 }
367 get_mailer('close');
368}
369
309ae892
DW
370/**
371 * Output some standard information during cron runs. Specifically current time
372 * and memory usage. This method also does gc_collect_cycles() (before displaying
373 * memory usage) to try to help PHP manage memory better.
374 */
375function cron_trace_time_and_memory() {
376 gc_collect_cycles();
377 mtrace('... started ' . date('H:i:s') . '. Current memory use ' . display_size(memory_get_usage()) . '.');
378}
379
cd305dc2
AN
380/**
381 * Prepare the output renderer for the cron run.
382 *
383 * This involves creating a new $PAGE, and $OUTPUT fresh for each task and prevents any one task from influencing
384 * any other.
385 *
386 * @param bool $restore Whether to restore the original PAGE and OUTPUT
387 */
388function cron_prepare_core_renderer($restore = false) {
389 global $OUTPUT, $PAGE;
390
391 // Store the original PAGE and OUTPUT values so that they can be reset at a later point to the original.
392 // This should not normally be required, but may be used in places such as the scheduled task tool's "Run now"
393 // functionality.
394 static $page = null;
395 static $output = null;
396
397 if (null === $page) {
398 $page = $PAGE;
399 }
400
401 if (null === $output) {
402 $output = $OUTPUT;
403 }
404
405 if (!empty($restore)) {
406 $PAGE = $page;
407 $page = null;
408
409 $OUTPUT = $output;
410 $output = null;
411 } else {
412 // Setup a new General renderer.
413 // Cron tasks may produce output to be used in web, so we must use the appropriate renderer target.
414 // This allows correct use of templates, etc.
415 $PAGE = new \moodle_page();
416 $OUTPUT = new \core_renderer($PAGE, RENDERER_TARGET_GENERAL);
417 }
418}