Merge branch 'MDL-75909-311' of https://github.com/andrewnicols/moodle into MOODLE_31...
[moodle.git] / admin / cli / adhoc_task.php
blobdd1cc14b478b4e67fe7870f8a6489c40282d6ca7
1 <?php
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.
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/>.
17 /**
18 * Task executor for adhoc tasks.
20 * @package core
21 * @subpackage cli
22 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('CLI_SCRIPT', true);
28 require(__DIR__ . '/../../config.php');
29 require_once("{$CFG->libdir}/clilib.php");
30 require_once("{$CFG->libdir}/cronlib.php");
32 list($options, $unrecognized) = cli_get_params(
34 'execute' => false,
35 'help' => false,
36 'keep-alive' => 0,
37 'showsql' => false,
38 'showdebugging' => false,
39 'ignorelimits' => false,
40 'force' => false,
41 ], [
42 'h' => 'help',
43 'e' => 'execute',
44 'k' => 'keep-alive',
45 'i' => 'ignorelimits',
46 'f' => 'force',
50 if ($unrecognized) {
51 $unrecognized = implode("\n ", $unrecognized);
52 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
55 if ($options['help'] or empty($options['execute'])) {
56 $help = <<<EOT
57 Ad hoc cron tasks.
59 Options:
60 -h, --help Print out this help
61 --showsql Show sql queries before they are executed
62 --showdebugging Show developer level debugging information
63 -e, --execute Run all queued adhoc tasks
64 -k, --keep-alive=N Keep this script alive for N seconds and poll for new adhoc tasks
65 -i --ignorelimits Ignore task_adhoc_concurrency_limit and task_adhoc_max_runtime limits
66 -f, --force Run even if cron is disabled
68 Example:
69 \$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --execute
71 EOT;
73 echo $help;
74 die;
77 if ($options['showdebugging']) {
78 set_debugging(DEBUG_DEVELOPER, true);
81 if ($options['showsql']) {
82 $DB->set_debug(true);
85 if (CLI_MAINTENANCE) {
86 echo "CLI maintenance mode active, cron execution suspended.\n";
87 exit(1);
90 if (moodle_needs_upgrading()) {
91 echo "Moodle upgrade pending, cron execution suspended.\n";
92 exit(1);
95 if (empty($options['execute'])) {
96 exit(0);
99 if (!get_config('core', 'cron_enabled') && !$options['force']) {
100 mtrace('Cron is disabled. Use --force to override.');
101 exit(1);
104 if (empty($options['keep-alive'])) {
105 $options['keep-alive'] = 0;
108 if (!empty($CFG->showcronsql)) {
109 $DB->set_debug(true);
111 if (!empty($CFG->showcrondebugging)) {
112 set_debugging(DEBUG_DEVELOPER, true);
115 $checklimits = empty($options['ignorelimits']);
117 core_php_time_limit::raise();
119 // Increase memory limit.
120 raise_memory_limit(MEMORY_EXTRA);
122 // Emulate normal session - we use admin account by default.
123 cron_setup_user();
125 $humantimenow = date('r', time());
126 $keepalive = (int)$options['keep-alive'];
128 \core\local\cli\shutdown::script_supports_graceful_exit();
130 mtrace("Server Time: {$humantimenow}\n");
131 cron_run_adhoc_tasks(time(), $keepalive, $checklimits);