Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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   * Cron running check
  19   *
  20   * @package    tool_task
  21   * @copyright  2020 Brendan Heywood (brendan@catalyst-au.net)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_task\check;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core\check\check;
  30  use core\check\result;
  31  /**
  32   * Cron running check
  33   *
  34   * @package    tool_task
  35   * @copyright  2020 Brendan Heywood (brendan@catalyst-au.net)
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class cronrunning extends check {
  39  
  40      /**
  41       * A link the running tasks report
  42       *
  43       * @return action_link|null
  44       */
  45      public function get_action_link(): ?\action_link {
  46          return new \action_link(
  47              new \moodle_url('/admin/tool/task/runningtasks.php'),
  48              get_string('runningtasks', 'tool_task'));
  49      }
  50  
  51      /**
  52       * Return result
  53       * @return result
  54       */
  55      public function get_result() : result {
  56          global $CFG;
  57  
  58          // Eventually this should replace cron_overdue_warning and
  59          // cron_infrequent_warning.
  60          $lastcron = get_config('tool_task', 'lastcronstart');
  61          $expectedfrequency = $CFG->expectedcronfrequency ?? MINSECS;
  62  
  63          $delta = time() - $lastcron;
  64  
  65          $lastcroninterval = get_config('tool_task', 'lastcroninterval');
  66  
  67          $formatdelta    = format_time($delta);
  68          $formatexpected = format_time($expectedfrequency);
  69          $formatinterval = format_time($lastcroninterval);
  70  
  71          // Inform user the time since last cron start.
  72          $details = get_string('lastcronstart', 'tool_task', $formatdelta);
  73  
  74          if ($delta > $expectedfrequency + MINSECS) {
  75              $status = result::WARNING;
  76  
  77              if ($delta > DAYSECS) {
  78                  $status = result::CRITICAL;
  79              }
  80  
  81              if (empty($lastcron)) {
  82                  if (empty($CFG->cronclionly)) {
  83                      $url = new \moodle_url('/admin/cron.php');
  84                      $summary = get_string('cronwarningneverweb', 'admin', [
  85                          'url' => $url->out(),
  86                          'expected' => $formatexpected,
  87                      ]);
  88                  } else {
  89                      $summary = get_string('cronwarningnever', 'admin', [
  90                          'expected' => $formatexpected,
  91                      ]);
  92                  }
  93              } else if (empty($CFG->cronclionly)) {
  94                  $url = new \moodle_url('/admin/cron.php');
  95                  $summary = get_string('cronwarning', 'admin', [
  96                      'url' => $url->out(),
  97                      'actual'   => $formatdelta,
  98                      'expected' => $formatexpected,
  99                  ]);
 100              } else {
 101                  $summary = get_string('cronwarningcli', 'admin', [
 102                      'actual'   => $formatdelta,
 103                      'expected' => $formatexpected,
 104                  ]);
 105              }
 106              return new result($status, $summary, $details);
 107          }
 108  
 109          // Add MINSECS to avoid spurious warning if cron is only a few seconds overdue.
 110          if ($lastcroninterval > $expectedfrequency + MINSECS) {
 111              $status = result::WARNING;
 112              $summary = get_string('croninfrequent', 'admin', [
 113                  'actual'   => $formatinterval,
 114                  'expected' => $formatexpected,
 115              ]);
 116              return new result($status, $summary, $details);
 117          }
 118  
 119          $status = result::OK;
 120          $summary = get_string('cronok', 'tool_task');
 121  
 122          return new result($status, $summary, $details);
 123      }
 124  }
 125