Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]
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 task execution. 19 * 20 * @package core 21 * @subpackage cli 22 * @copyright 2014 Petr Skoda 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 define('CLI_SCRIPT', true); 27 28 require(__DIR__ . '/../../config.php'); 29 require_once("$CFG->libdir/clilib.php"); 30 31 list($options, $unrecognized) = cli_get_params( 32 [ 33 'help' => false, 34 'list' => false, 35 'execute' => false, 36 'showsql' => false, 37 'showdebugging' => false, 38 'force' => false, 39 ], [ 40 'h' => 'help', 41 'f' => 'force', 42 ] 43 ); 44 45 if ($unrecognized) { 46 $unrecognized = implode("\n ", $unrecognized); 47 cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); 48 } 49 50 if ($options['help'] or (!$options['list'] and !$options['execute'])) { 51 $help = 52 "Scheduled cron tasks. 53 54 Options: 55 --execute=\\some\\task Execute scheduled task manually 56 --list List all scheduled tasks 57 --showsql Show sql queries before they are executed 58 --showdebugging Show developer level debugging information 59 -h, --help Print out this help 60 -f, --force Execute task even if cron is disabled 61 62 Example: 63 \$sudo -u www-data /usr/bin/php admin/cli/scheduled_task.php --execute=\\core\\task\\session_cleanup_task 64 65 "; 66 67 echo $help; 68 die; 69 } 70 71 if ($options['showdebugging'] || !empty($CFG->showcrondebugging)) { 72 set_debugging(DEBUG_DEVELOPER, true); 73 } 74 75 if ($options['showsql'] || !empty($CFG->showcronsql)) { 76 $DB->set_debug(true); 77 } 78 if ($options['list']) { 79 cli_heading("List of scheduled tasks ($CFG->wwwroot)"); 80 81 $shorttime = get_string('strftimedatetimeshort'); 82 83 $tasks = \core\task\manager::get_all_scheduled_tasks(); 84 echo str_pad(get_string('scheduledtasks', 'tool_task'), 50, ' ') . ' ' . str_pad(get_string('runpattern', 'tool_task'), 17, ' ') 85 . ' ' . str_pad(get_string('lastruntime', 'tool_task'), 40, ' ') . get_string('nextruntime', 'tool_task') . "\n"; 86 foreach ($tasks as $task) { 87 $class = '\\' . get_class($task); 88 $schedule = $task->get_minute() . ' ' 89 . $task->get_hour() . ' ' 90 . $task->get_day() . ' ' 91 . $task->get_day_of_week() . ' ' 92 . $task->get_month() . ' ' 93 . $task->get_day_of_week(); 94 $nextrun = $task->get_next_run_time(); 95 $lastrun = $task->get_last_run_time(); 96 97 $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component()); 98 $plugindisabled = $plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled(); 99 100 if ($plugindisabled) { 101 $nextrun = get_string('plugindisabled', 'tool_task'); 102 } else if ($task->get_disabled()) { 103 $nextrun = get_string('taskdisabled', 'tool_task'); 104 } else if ($nextrun > time()) { 105 $nextrun = userdate($nextrun); 106 } else { 107 $nextrun = get_string('asap', 'tool_task'); 108 } 109 110 if ($lastrun) { 111 $lastrun = userdate($lastrun); 112 } else { 113 $lastrun = get_string('never'); 114 } 115 116 echo str_pad($class, 50, ' ') . ' ' . str_pad($schedule, 17, ' ') . 117 ' ' . str_pad($lastrun, 40, ' ') . ' ' . $nextrun . "\n"; 118 } 119 exit(0); 120 } 121 122 if ($execute = $options['execute']) { 123 if (!$task = \core\task\manager::get_scheduled_task($execute)) { 124 mtrace("Task '$execute' not found"); 125 exit(1); 126 } 127 128 if (moodle_needs_upgrading()) { 129 mtrace("Moodle upgrade pending, cannot execute tasks."); 130 exit(1); 131 } 132 133 if (!get_config('core', 'cron_enabled') && !$options['force']) { 134 mtrace('Cron is disabled. Use --force to override.'); 135 exit(1); 136 } 137 138 \core\task\manager::scheduled_task_starting($task); 139 140 // Increase memory limit. 141 raise_memory_limit(MEMORY_EXTRA); 142 143 // Emulate normal session - we use admin account by default. 144 \core\cron::setup_user(); 145 146 // Execute the task. 147 \core\local\cli\shutdown::script_supports_graceful_exit(); 148 $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron'); 149 if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) { 150 mtrace('Cannot obtain cron lock'); 151 exit(129); 152 } 153 if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) { 154 $cronlock->release(); 155 mtrace('Cannot obtain task lock'); 156 exit(130); 157 } 158 159 $task->set_lock($lock); 160 if (!$task->is_blocking()) { 161 $cronlock->release(); 162 } else { 163 $task->set_cron_lock($cronlock); 164 } 165 166 \core\cron::run_inner_scheduled_task($task); 167 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body