Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * 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       * Constructor
  42       */
  43      public function __construct() {
  44          global $CFG;
  45          $this->id = 'cronrunning';
  46          $this->name = get_string('checkcronrunning', 'tool_task');
  47          if (empty($CFG->cronclionly)) {
  48              $this->actionlink = new \action_link(
  49                  new \moodle_url('/admin/cron.php'),
  50                  get_string('cron', 'admin'));
  51          }
  52      }
  53  
  54      /**
  55       * Return result
  56       * @return result
  57       */
  58      public function get_result() : result {
  59          global $CFG;
  60  
  61          // Eventually this should replace cron_overdue_warning and
  62          // cron_infrequent_warning.
  63          $lastcron = get_config('tool_task', 'lastcronstart');
  64          $expectedfrequency = $CFG->expectedcronfrequency ?? MINSECS;
  65  
  66          $delta = time() - $lastcron;
  67  
  68          $lastcroninterval = get_config('tool_task', 'lastcroninterval');
  69  
  70          $formatdelta    = format_time($delta);
  71          $formatexpected = format_time($expectedfrequency);
  72          $formatinterval = format_time($lastcroninterval);
  73  
  74          $details = format_time($delta);
  75  
  76          if ($delta > $expectedfrequency + MINSECS) {
  77              $status = result::WARNING;
  78  
  79              if ($delta > DAYSECS) {
  80                  $status = result::CRITICAL;
  81              }
  82  
  83              if (empty($lastcron)) {
  84                  if (empty($CFG->cronclionly)) {
  85                      $url = new \moodle_url('/admin/cron.php');
  86                      $summary = get_string('cronwarningneverweb', 'admin', [
  87                          'url' => $url->out(),
  88                          'expected' => $formatexpected,
  89                      ]);
  90                  } else {
  91                      $summary = get_string('cronwarningnever', 'admin', [
  92                          'expected' => $formatexpected,
  93                      ]);
  94                  }
  95              } else if (empty($CFG->cronclionly)) {
  96                  $url = new \moodle_url('/admin/cron.php');
  97                  $summary = get_string('cronwarning', 'admin', [
  98                      'url' => $url->out(),
  99                      'actual'   => $formatdelta,
 100                      'expected' => $formatexpected,
 101                  ]);
 102              } else {
 103                  $summary = get_string('cronwarningcli', 'admin', [
 104                      'actual'   => $formatdelta,
 105                      'expected' => $formatexpected,
 106                  ]);
 107              }
 108              return new result($status, $summary, $details);
 109          }
 110  
 111          if ($lastcroninterval > $expectedfrequency) {
 112              $status = result::WARNING;
 113              $summary = get_string('croninfrequent', 'admin', [
 114                  'actual'   => $formatinterval,
 115                  'expected' => $formatexpected,
 116              ]);
 117              return new result($status, $summary, $details);
 118          }
 119  
 120          $status = result::OK;
 121          $summary = get_string('cronok', 'tool_task');
 122  
 123          return new result($status, $summary, $details);
 124      }
 125  }
 126