Differences Between: [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * CLI cron 20 * 21 * This script looks through all the module directories for cron.php files 22 * and runs them. These files can contain cleanup functions, email functions 23 * or anything that needs to be run on a regular basis. 24 * 25 * @package core 26 * @subpackage cli 27 * @copyright 2009 Petr Skoda (http://skodak.org) 28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 */ 30 31 define('CLI_SCRIPT', true); 32 33 require(__DIR__.'/../../config.php'); 34 require_once($CFG->libdir.'/clilib.php'); // cli only functions 35 require_once($CFG->libdir.'/cronlib.php'); 36 37 // now get cli options 38 list($options, $unrecognized) = cli_get_params( 39 [ 40 'help' => false, 41 'stop' => false, 42 'list' => false, 43 'force' => false, 44 'enable' => false, 45 'disable' => false, 46 'disable-wait' => false, 47 ], [ 48 'h' => 'help', 49 's' => 'stop', 50 'l' => 'list', 51 'f' => 'force', 52 'e' => 'enable', 53 'd' => 'disable', 54 'w' => 'disable-wait', 55 ] 56 ); 57 58 if ($unrecognized) { 59 $unrecognized = implode("\n ", $unrecognized); 60 cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); 61 } 62 63 if ($options['help']) { 64 $help = 65 "Execute periodic cron actions. 66 67 Options: 68 -h, --help Print out this help 69 -s, --stop Notify all other running cron processes to stop after the current task 70 -l, --list Show the list of currently running tasks and how long they have been running 71 -f, --force Execute task even if cron is disabled 72 -e, --enable Enable cron 73 -d, --disable Disable cron 74 -w, --disable-wait=600 Disable cron and wait until all tasks finished or fail after N seconds (optional param) 75 76 Example: 77 \$sudo -u www-data /usr/bin/php admin/cli/cron.php 78 "; 79 80 echo $help; 81 die; 82 } 83 84 if ($options['stop']) { 85 // By clearing the caches this signals to other running processes 86 // to exit after finishing the current task. 87 \core\task\manager::clear_static_caches(); 88 die; 89 } 90 91 if ($options['enable']) { 92 set_config('cron_enabled', 1); 93 mtrace('Cron has been enabled for the site.'); 94 exit(0); 95 } 96 97 if ($options['disable']) { 98 set_config('cron_enabled', 0); 99 \core\task\manager::clear_static_caches(); 100 mtrace('Cron has been disabled for the site.'); 101 exit(0); 102 } 103 104 if ($options['list']) { 105 $tasks = \core\task\manager::get_running_tasks(); 106 mtrace('The list of currently running tasks:'); 107 $format = "%7s %-12s %-9s %-20s %-52s\n"; 108 printf ($format, 109 'PID', 110 'HOST', 111 'TYPE', 112 'TIME', 113 'CLASSNAME' 114 ); 115 foreach ($tasks as $task) { 116 printf ($format, 117 $task->pid, 118 substr($task->hostname, 0, 12), 119 $task->type, 120 format_time(time() - $task->timestarted), 121 substr($task->classname, 0, 52) 122 ); 123 } 124 exit(0); 125 } 126 127 if ($wait = $options['disable-wait']) { 128 $started = time(); 129 if (true === $wait) { 130 // Default waiting time. 131 $waitsec = 600; 132 } else { 133 $waitsec = $wait; 134 $wait = true; 135 } 136 137 set_config('cron_enabled', 0); 138 \core\task\manager::clear_static_caches(); 139 mtrace('Cron has been disabled for the site.'); 140 mtrace('Allocating '. format_time($waitsec) . ' for the tasks to finish.'); 141 142 $lastcount = 0; 143 while ($wait) { 144 $tasks = \core\task\manager::get_running_tasks(); 145 146 if (count($tasks) == 0) { 147 mtrace(''); 148 mtrace('All scheduled and adhoc tasks finished.'); 149 exit(0); 150 } 151 152 if (time() - $started >= $waitsec) { 153 mtrace(''); 154 mtrace('Wait time ('. format_time($waitsec) . ') elapsed, but ' . count($tasks) . ' task(s) still running.'); 155 mtrace('Exiting with code 1.'); 156 exit(1); 157 } 158 159 if (count($tasks) !== $lastcount) { 160 mtrace(''); 161 mtrace(count($tasks) . " tasks currently running.", ''); 162 $lastcount = count($tasks); 163 } else { 164 mtrace('.', ''); 165 } 166 167 sleep(1); 168 } 169 } 170 171 if (!get_config('core', 'cron_enabled') && !$options['force']) { 172 mtrace('Cron is disabled. Use --force to override.'); 173 exit(1); 174 } 175 176 \core\local\cli\shutdown::script_supports_graceful_exit(); 177 178 cron_run();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body