Differences Between: [Versions 39 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 * Database logger for task logging. 19 * 20 * @package core 21 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 namespace core\task; 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 /** 29 * Database logger for task logging. 30 * 31 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk> 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class database_logger implements task_logger { 35 36 /** @var int Type constant for a scheduled task */ 37 const TYPE_SCHEDULED = 0; 38 39 /** @var int Type constant for an adhoc task */ 40 const TYPE_ADHOC = 1; 41 42 /** 43 * Whether the task is configured and ready to log. 44 * 45 * @return bool 46 */ 47 public static function is_configured() : bool { 48 return true; 49 } 50 51 /** 52 * Store the log for the specified task. 53 * 54 * @param task_base $task The task that the log belongs to. 55 * @param string $logpath The path to the log on disk 56 * @param bool $failed Whether the task failed 57 * @param int $dbreads The number of DB reads 58 * @param int $dbwrites The number of DB writes 59 * @param float $timestart The start time of the task 60 * @param float $timeend The end time of the task 61 */ 62 public static function store_log_for_task(task_base $task, string $logpath, bool $failed, 63 int $dbreads, int $dbwrites, float $timestart, float $timeend) { 64 global $DB; 65 66 // Write this log to the database. 67 $logdata = (object) [ 68 'type' => is_a($task, scheduled_task::class) ? self::TYPE_SCHEDULED : self::TYPE_ADHOC, 69 'component' => $task->get_component(), 70 'classname' => get_class($task), 71 'userid' => 0, 72 'timestart' => $timestart, 73 'timeend' => $timeend, 74 'dbreads' => $dbreads, 75 'dbwrites' => $dbwrites, 76 'result' => (int) $failed, 77 'output' => file_get_contents($logpath), 78 'hostname' => $task->get_hostname(), 79 'pid' => $task->get_pid(), 80 ]; 81 82 if (is_a($task, adhoc_task::class) && $userid = $task->get_userid()) { 83 $logdata->userid = $userid; 84 } 85 86 $logdata->id = $DB->insert_record('task_log', $logdata); 87 } 88 89 /** 90 * Whether this task logger has a report available. 91 * 92 * @return bool 93 */ 94 public static function has_log_report() : bool { 95 return true; 96 } 97 98 /** 99 * Get any URL available for viewing relevant task log reports. 100 * 101 * @param string $classname The task class to fetch for 102 * @return \moodle_url 103 */ 104 public static function get_url_for_task_class(string $classname) : \moodle_url { 105 global $CFG; 106 107 return new \moodle_url("/{$CFG->admin}/tasklogs.php", [ 108 'filter' => $classname, 109 ]); 110 } 111 112 /** 113 * Cleanup old task logs. 114 */ 115 public static function cleanup() { 116 global $CFG, $DB; 117 118 // Delete logs older than the retention period. 119 $params = [ 120 'retentionperiod' => time() - $CFG->task_logretention, 121 ]; 122 $logids = $DB->get_fieldset_select('task_log', 'id', 'timestart < :retentionperiod', $params); 123 self::delete_task_logs($logids); 124 125 // Delete logs to retain a minimum number of logs. 126 $sql = "SELECT classname FROM {task_log} GROUP BY classname HAVING COUNT(classname) > :retaincount"; 127 $params = [ 128 'retaincount' => $CFG->task_logretainruns, 129 ]; 130 $classes = $DB->get_fieldset_sql($sql, $params); 131 132 foreach ($classes as $classname) { 133 $notinsql = ""; 134 $params = [ 135 'classname' => $classname, 136 ]; 137 138 $retaincount = (int) $CFG->task_logretainruns; 139 if ($retaincount) { 140 $keeplogs = $DB->get_records('task_log', [ 141 'classname' => $classname, 142 ], 'timestart DESC', 'id', 0, $retaincount); 143 144 if ($keeplogs) { 145 list($notinsql, $params) = $DB->get_in_or_equal(array_keys($keeplogs), SQL_PARAMS_NAMED, 'p', false); 146 $params['classname'] = $classname; 147 $notinsql = " AND id {$notinsql}"; 148 } 149 } 150 151 $logids = $DB->get_fieldset_select('task_log', 'id', "classname = :classname {$notinsql}", $params); 152 self::delete_task_logs($logids); 153 } 154 } 155 156 /** 157 * Delete task logs for the specified logs. 158 * 159 * @param array $logids 160 */ 161 public static function delete_task_logs(array $logids) { 162 global $DB; 163 164 if (empty($logids)) { 165 return; 166 } 167 168 $chunks = array_chunk($logids, 1000); 169 foreach ($chunks as $chunk) { 170 $DB->delete_records_list('task_log', 'id', $chunk); 171 } 172 } 173 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body