Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  namespace tool_task\check;
  18  
  19  use core\check\check;
  20  use core\check\result;
  21  use core\task\manager;
  22  
  23  /**
  24   * Long running tasks check
  25   *
  26   * @package    tool_task
  27   * @author     Qihui Chan (qihuichan@catalyst-au.net)
  28   * @copyright  2022 Catalyst IT Pty Ltd
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class longrunningtasks extends check {
  32  
  33      /**
  34       * Links to the running task list
  35       *
  36       * @return \action_link|null
  37       * @throws \coding_exception
  38       */
  39      public function get_action_link(): ?\action_link {
  40          $url = new \moodle_url('/admin/tool/task/runningtasks.php');
  41          return new \action_link($url, get_string('runningtasks', 'tool_task'));
  42      }
  43  
  44      /**
  45       * Return result
  46       * @return result
  47       */
  48      public function get_result(): result {
  49          global $CFG;
  50          $status = result::OK;
  51          $slowtasks = 0;
  52          $details = '';
  53          $maxruntime = 0;
  54          $runtime = 0;
  55  
  56          $tasks = \core\task\manager::get_running_tasks();
  57          foreach ($tasks as $record) {
  58              $taskmethod = "{$record->type}_task_from_record";
  59              $task = manager::$taskmethod($record);
  60              $taskname = $task->get_name();
  61  
  62              $result = $task->get_runtime_result();
  63              $taskstatus = $result->get_status();
  64              $runtime = $task->get_runtime();
  65              $runtimedetails = get_string('taskrunningtime', 'tool_task', format_time($runtime));
  66              $maxruntime = ($runtime > $maxruntime) ? $runtime : $maxruntime;
  67  
  68              if ($taskstatus == result::OK) {
  69                  continue;
  70              }
  71  
  72              $slowtasks++;
  73              $details .= strtoupper($taskstatus) . ": {$taskname}. {$runtimedetails} <br>";
  74  
  75              // The overall check status is the worst tasks status.
  76              if ($status !== result::ERROR) {
  77                  $status = $taskstatus;
  78              }
  79          }
  80  
  81          $summary = get_string('checklongrunningtaskcount', 'tool_task', $slowtasks);
  82          $conclusion = get_string('taskdetails', 'tool_task', ['count' => $slowtasks,
  83              'time' => format_time($CFG->taskruntimewarn), 'maxtime' => format_time($maxruntime)]);
  84  
  85          $details = ($slowtasks ? $conclusion : $summary) . "<br>{$details}";
  86  
  87          return new result($status, $summary, $details);
  88      }
  89  }