1 |
efrain |
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.
|
|
|
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 |
* CLI cron
|
|
|
19 |
*
|
|
|
20 |
* This script looks through all the module directories for cron.php files
|
|
|
21 |
* and runs them. These files can contain cleanup functions, email functions
|
|
|
22 |
* or anything that needs to be run on a regular basis.
|
|
|
23 |
*
|
|
|
24 |
* @package core
|
|
|
25 |
* @subpackage cli
|
|
|
26 |
* @copyright 2009 Petr Skoda (http://skodak.org)
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
define('CLI_SCRIPT', true);
|
|
|
31 |
|
|
|
32 |
require(__DIR__ . '/../../config.php');
|
|
|
33 |
require_once($CFG->libdir . '/clilib.php');
|
|
|
34 |
|
|
|
35 |
// Now get cli option.
|
|
|
36 |
[$options, $unrecognized] = cli_get_params(
|
|
|
37 |
[
|
|
|
38 |
'help' => false,
|
|
|
39 |
'stop' => false,
|
|
|
40 |
'list' => false,
|
|
|
41 |
'force' => false,
|
|
|
42 |
'enable' => false,
|
|
|
43 |
'disable' => false,
|
|
|
44 |
'disable-wait' => false,
|
|
|
45 |
'keep-alive' => null,
|
|
|
46 |
],
|
|
|
47 |
[
|
|
|
48 |
'h' => 'help',
|
|
|
49 |
's' => 'stop',
|
|
|
50 |
'l' => 'list',
|
|
|
51 |
'f' => 'force',
|
|
|
52 |
'e' => 'enable',
|
|
|
53 |
'd' => 'disable',
|
|
|
54 |
'w' => 'disable-wait',
|
|
|
55 |
'k' => 'keep-alive',
|
|
|
56 |
]
|
|
|
57 |
);
|
|
|
58 |
|
|
|
59 |
if ($unrecognized) {
|
|
|
60 |
$unrecognized = implode("\n ", $unrecognized);
|
|
|
61 |
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
if ($options['help']) {
|
|
|
65 |
$help =
|
|
|
66 |
"Execute periodic cron actions.
|
|
|
67 |
|
|
|
68 |
Options:
|
|
|
69 |
-h, --help Print out this help
|
|
|
70 |
-s, --stop Notify all other running cron processes to stop after the current task
|
|
|
71 |
-l, --list Show the list of currently running tasks and how long they have been running
|
|
|
72 |
-f, --force Execute task even if cron is disabled
|
|
|
73 |
-e, --enable Enable cron
|
|
|
74 |
-d, --disable Disable cron
|
|
|
75 |
-w, --disable-wait=600 Disable cron and wait until all tasks finished or fail after N seconds (optional param)
|
|
|
76 |
-k, --keep-alive=N Keep this script alive for N seconds and poll for new tasks
|
|
|
77 |
The default value can be set by administrators in:
|
|
|
78 |
Site administration > Server > Tasks > Task processing > Keep alive time
|
|
|
79 |
|
|
|
80 |
Example:
|
|
|
81 |
\$sudo -u www-data /usr/bin/php admin/cli/cron.php
|
|
|
82 |
";
|
|
|
83 |
|
|
|
84 |
echo $help;
|
|
|
85 |
die;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
if ($options['stop']) {
|
|
|
89 |
// By clearing the caches this signals to other running processes
|
|
|
90 |
// to exit after finishing the current task.
|
|
|
91 |
\core\task\manager::clear_static_caches();
|
|
|
92 |
die;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
if ($options['enable']) {
|
|
|
96 |
set_config('cron_enabled', 1);
|
|
|
97 |
mtrace('Cron has been enabled for the site.');
|
|
|
98 |
exit(0);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
if ($options['disable']) {
|
|
|
102 |
set_config('cron_enabled', 0);
|
|
|
103 |
\core\task\manager::clear_static_caches();
|
|
|
104 |
mtrace('Cron has been disabled for the site.');
|
|
|
105 |
exit(0);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
if ($options['list']) {
|
|
|
109 |
$tasks = \core\task\manager::get_running_tasks();
|
|
|
110 |
mtrace('The list of currently running tasks:');
|
|
|
111 |
$format = "%7s %-12s %-9s %-20s %-52s\n";
|
|
|
112 |
printf(
|
|
|
113 |
$format,
|
|
|
114 |
'PID',
|
|
|
115 |
'HOST',
|
|
|
116 |
'TYPE',
|
|
|
117 |
'TIME',
|
|
|
118 |
'CLASSNAME'
|
|
|
119 |
);
|
|
|
120 |
foreach ($tasks as $task) {
|
|
|
121 |
printf(
|
|
|
122 |
$format,
|
|
|
123 |
$task->pid,
|
|
|
124 |
substr($task->hostname, 0, 12),
|
|
|
125 |
$task->type,
|
|
|
126 |
format_time(time() - $task->timestarted),
|
|
|
127 |
substr($task->classname, 0, 52)
|
|
|
128 |
);
|
|
|
129 |
}
|
|
|
130 |
exit(0);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
if ($wait = $options['disable-wait']) {
|
|
|
134 |
$started = time();
|
|
|
135 |
if (true === $wait) {
|
|
|
136 |
// Default waiting time.
|
|
|
137 |
$waitsec = 600;
|
|
|
138 |
} else {
|
|
|
139 |
$waitsec = $wait;
|
|
|
140 |
$wait = true;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
set_config('cron_enabled', 0);
|
|
|
144 |
\core\task\manager::clear_static_caches();
|
|
|
145 |
mtrace('Cron has been disabled for the site.');
|
|
|
146 |
mtrace('Allocating ' . format_time($waitsec) . ' for the tasks to finish.');
|
|
|
147 |
|
|
|
148 |
$lastcount = 0;
|
|
|
149 |
while ($wait) {
|
|
|
150 |
$tasks = \core\task\manager::get_running_tasks();
|
|
|
151 |
|
|
|
152 |
if (count($tasks) == 0) {
|
|
|
153 |
mtrace('');
|
|
|
154 |
mtrace('All scheduled and adhoc tasks finished.');
|
|
|
155 |
exit(0);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
if (time() - $started >= $waitsec) {
|
|
|
159 |
mtrace('');
|
|
|
160 |
mtrace('Wait time (' . format_time($waitsec) . ') elapsed, but ' . count($tasks) . ' task(s) still running.');
|
|
|
161 |
mtrace('Exiting with code 1.');
|
|
|
162 |
exit(1);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
if (count($tasks) !== $lastcount) {
|
|
|
166 |
mtrace('');
|
|
|
167 |
mtrace(count($tasks) . " tasks currently running.", '');
|
|
|
168 |
$lastcount = count($tasks);
|
|
|
169 |
} else {
|
|
|
170 |
mtrace('.', '');
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
sleep(1);
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
if (!get_config('core', 'cron_enabled') && !$options['force']) {
|
|
|
178 |
mtrace('Cron is disabled. Use --force to override.');
|
|
|
179 |
exit(1);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
\core\local\cli\shutdown::script_supports_graceful_exit();
|
|
|
183 |
|
|
|
184 |
|
|
|
185 |
$keepalive = $options['keep-alive'];
|
|
|
186 |
\core\cron::run_main_process($keepalive);
|