Differences Between: [Versions 39 and 310]
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 * @deprecated since Moodle 3.9 MDL-63580. Please use the admin/cli/schedule_task.php. 21 * @todo final deprecation. To be removed in Moodle 4.1 MDL-63594. 22 * 23 * @package tool_task 24 * @copyright 2014 Petr Skoda 25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 26 */ 27 28 define('CLI_SCRIPT', true); 29 30 require(__DIR__ . '/../../../../config.php'); 31 require_once("$CFG->libdir/clilib.php"); 32 require_once("$CFG->libdir/cronlib.php"); 33 34 list($options, $unrecognized) = cli_get_params( 35 array('help' => false, 'list' => false, 'execute' => false, 'showsql' => false, 'showdebugging' => false), 36 array('h' => 'help') 37 ); 38 39 debugging('admin/tool/task/cli/schedule_task.php is deprecated. Please use admin/cli/scheduled_task.php instead.', DEBUG_DEVELOPER); 40 41 if ($unrecognized) { 42 $unrecognized = implode("\n ", $unrecognized); 43 cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); 44 } 45 46 if ($options['help'] or (!$options['list'] and !$options['execute'])) { 47 $help = 48 "Scheduled cron tasks. 49 50 Options: 51 --execute=\\\\some\\\\task Execute scheduled task manually 52 --list List all scheduled tasks 53 --showsql Show sql queries before they are executed 54 --showdebugging Show developer level debugging information 55 -h, --help Print out this help 56 57 Example: 58 \$sudo -u www-data /usr/bin/php admin/tool/task/cli/schedule_task.php --execute=\\\\core\\\\task\\\\session_cleanup_task 59 60 "; 61 62 echo $help; 63 die; 64 } 65 66 if ($options['showdebugging']) { 67 set_debugging(DEBUG_DEVELOPER, true); 68 } 69 70 if ($options['showsql']) { 71 $DB->set_debug(true); 72 } 73 74 if ($options['list']) { 75 cli_heading("List of scheduled tasks ($CFG->wwwroot)"); 76 77 $shorttime = get_string('strftimedatetimeshort'); 78 79 $tasks = \core\task\manager::get_all_scheduled_tasks(); 80 echo str_pad(get_string('scheduledtasks', 'tool_task'), 50, ' ') . ' ' . str_pad(get_string('runpattern', 'tool_task'), 17, ' ') 81 . ' ' . str_pad(get_string('lastruntime', 'tool_task'), 40, ' ') . get_string('nextruntime', 'tool_task') . "\n"; 82 foreach ($tasks as $task) { 83 $class = '\\' . get_class($task); 84 $schedule = $task->get_minute() . ' ' 85 . $task->get_hour() . ' ' 86 . $task->get_day() . ' ' 87 . $task->get_day_of_week() . ' ' 88 . $task->get_month() . ' ' 89 . $task->get_day_of_week(); 90 $nextrun = $task->get_next_run_time(); 91 $lastrun = $task->get_last_run_time(); 92 93 $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component()); 94 $plugindisabled = $plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled(); 95 96 if ($plugindisabled) { 97 $nextrun = get_string('plugindisabled', 'tool_task'); 98 } else if ($task->get_disabled()) { 99 $nextrun = get_string('taskdisabled', 'tool_task'); 100 } else if ($nextrun > time()) { 101 $nextrun = userdate($nextrun); 102 } else { 103 $nextrun = get_string('asap', 'tool_task'); 104 } 105 106 if ($lastrun) { 107 $lastrun = userdate($lastrun); 108 } else { 109 $lastrun = get_string('never'); 110 } 111 112 echo str_pad($class, 50, ' ') . ' ' . str_pad($schedule, 17, ' ') . 113 ' ' . str_pad($lastrun, 40, ' ') . ' ' . $nextrun . "\n"; 114 } 115 exit(0); 116 } 117 118 if ($execute = $options['execute']) { 119 if (!$task = \core\task\manager::get_scheduled_task($execute)) { 120 mtrace("Task '$execute' not found"); 121 exit(1); 122 } 123 124 if (moodle_needs_upgrading()) { 125 mtrace("Moodle upgrade pending, cannot execute tasks."); 126 exit(1); 127 } 128 129 // Increase memory limit. 130 raise_memory_limit(MEMORY_EXTRA); 131 132 // Emulate normal session - we use admin account by default. 133 cron_setup_user(); 134 135 $predbqueries = $DB->perf_get_queries(); 136 $pretime = microtime(true); 137 138 \core\task\logmanager::start_logging($task); 139 $fullname = $task->get_name() . ' (' . get_class($task) . ')'; 140 mtrace('Execute scheduled task: ' . $fullname); 141 // NOTE: it would be tricky to move this code to \core\task\manager class, 142 // because we want to do detailed error reporting. 143 $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron'); 144 if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) { 145 mtrace('Cannot obtain cron lock'); 146 exit(129); 147 } 148 if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) { 149 $cronlock->release(); 150 mtrace('Cannot obtain task lock'); 151 exit(130); 152 } 153 154 $task->set_lock($lock); 155 if (!$task->is_blocking()) { 156 $cronlock->release(); 157 } else { 158 $task->set_cron_lock($cronlock); 159 } 160 161 try { 162 get_mailer('buffer'); 163 $task->execute(); 164 if (isset($predbqueries)) { 165 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries"); 166 mtrace("... used " . (microtime(1) - $pretime) . " seconds"); 167 } 168 mtrace('Scheduled task complete: ' . $fullname); 169 \core\task\manager::scheduled_task_complete($task); 170 get_mailer('close'); 171 exit(0); 172 } catch (Exception $e) { 173 if ($DB->is_transaction_started()) { 174 $DB->force_transaction_rollback(); 175 } 176 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries"); 177 mtrace("... used " . (microtime(true) - $pretime) . " seconds"); 178 mtrace('Scheduled task failed: ' . $fullname . ',' . $e->getMessage()); 179 if ($CFG->debugdeveloper) { 180 if (!empty($e->debuginfo)) { 181 mtrace("Debug info:"); 182 mtrace($e->debuginfo); 183 } 184 mtrace("Backtrace:"); 185 mtrace(format_backtrace($e->getTrace(), true)); 186 } 187 \core\task\manager::scheduled_task_failed($task); 188 get_mailer('close'); 189 exit(1); 190 } 191 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body