2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Task executor for adhoc tasks.
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(
38 'showdebugging' => false,
39 'ignorelimits' => false,
45 'i' => 'ignorelimits',
51 $unrecognized = implode("\n ", $unrecognized);
52 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
55 if ($options['help'] or empty($options['execute'])) {
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
69 \$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --execute
77 if ($options['showdebugging']) {
78 set_debugging(DEBUG_DEVELOPER
, true);
81 if ($options['showsql']) {
85 if (CLI_MAINTENANCE
) {
86 echo "CLI maintenance mode active, cron execution suspended.\n";
90 if (moodle_needs_upgrading()) {
91 echo "Moodle upgrade pending, cron execution suspended.\n";
95 if (empty($options['execute'])) {
99 if (!get_config('core', 'cron_enabled') && !$options['force']) {
100 mtrace('Cron is disabled. Use --force to override.');
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.
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);