Skip to content

Commit

Permalink
MDL-64454 Admin: Warning if cron does not run frequently
Browse files Browse the repository at this point in the history
There is an existing warning if cron doesn't run at all (hasn't run
for 24 hours). This commit adds a warning if cron hasn't run for 3
minutes, based on the recommendation that cron should run every
minute.
  • Loading branch information
sammarshallou committed Jan 14, 2019
1 parent 4c5b60a commit b18034e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
7 changes: 4 additions & 3 deletions admin/index.php
Expand Up @@ -823,9 +823,10 @@
// Print default admin page with notifications.
$errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');

// We make the assumption that at least one schedule task should run once per day.
$lastcron = $DB->get_field_sql('SELECT MAX(lastruntime) FROM {task_scheduled}');
$lastcron = get_config('tool_task', 'lastcronstart');
$cronoverdue = ($lastcron < time() - 3600 * 24);
$lastcroninterval = get_config('tool_task', 'lastcroninterval');
$croninfrequent = !$cronoverdue && ($lastcroninterval > 200 || $lastcron < time() - 200);
$dbproblems = $DB->diagnose();
$maintenancemode = !empty($CFG->maintenance_enabled);

Expand Down Expand Up @@ -886,4 +887,4 @@
echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
$maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
$registered, $cachewarnings, $eventshandlers, $themedesignermode, $devlibdir,
$mobileconfigured, $overridetossl, $invalidforgottenpasswordurl);
$mobileconfigured, $overridetossl, $invalidforgottenpasswordurl, $croninfrequent);
19 changes: 18 additions & 1 deletion admin/renderer.php
Expand Up @@ -281,14 +281,15 @@ public function upgrade_confirm_abort_install_page(array $abortable, moodle_url
* @param bool $mobileconfigured Whether the mobile web services have been enabled
* @param bool $overridetossl Whether or not ssl is being forced.
* @param bool $invalidforgottenpasswordurl Whether the forgotten password URL does not link to a valid URL.
* @param bool $croninfrequent If true, warn that cron hasn't run in the past few minutes
*
* @return string HTML to output.
*/
public function admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
$cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch,
$buggyiconvnomb, $registered, array $cachewarnings = array(), $eventshandlers = 0,
$themedesignermode = false, $devlibdir = false, $mobileconfigured = false,
$overridetossl = false, $invalidforgottenpasswordurl = false) {
$overridetossl = false, $invalidforgottenpasswordurl = false, $croninfrequent = false) {
global $CFG;
$output = '';

Expand All @@ -302,6 +303,7 @@ public function admin_notifications_page($maturity, $insecuredataroot, $errorsdi
$output .= $this->display_errors_warning($errorsdisplayed);
$output .= $this->buggy_iconv_warning($buggyiconvnomb);
$output .= $this->cron_overdue_warning($cronoverdue);
$output .= $this->cron_infrequent_warning($croninfrequent);
$output .= $this->db_problems($dbproblems);
$output .= $this->maintenance_mode_warning($maintenancemode);
$output .= $this->overridetossl_warning($overridetossl);
Expand Down Expand Up @@ -614,6 +616,21 @@ public function cron_overdue_warning($cronoverdue) {
$this->help_icon('cron', 'admin'));
}

/**
* Render an appropriate message if cron is not being run frequently (recommended every minute).
*
* @param bool $croninfrequent
* @return string HTML to output.
*/
public function cron_infrequent_warning(bool $croninfrequent) : string {
if (!$croninfrequent) {
return '';
}

return $this->warning(get_string('croninfrequent', 'admin') . '&nbsp;' .
$this->help_icon('cron', 'admin'));
}

/**
* Render an appropriate message if there are any problems with the DB set-up.
* @param bool $dbproblems
Expand Down
1 change: 1 addition & 0 deletions lang/en/admin.php
Expand Up @@ -419,6 +419,7 @@
$string['cronremotepassword'] = 'Cron password for remote access';
$string['cronwarning'] = 'The <a href="{$a}">cron.php maintenance script</a> has not been run for at least 24 hours.';
$string['cronwarningcli'] = 'The cli/cron.php maintenance script has not been run for at least 24 hours.';
$string['croninfrequent'] = 'The time between the last two runs of the cron maintenance script was over 3 minutes. We recommend configuring it to run more frequently.';
$string['ctyperequired'] = 'The ctype PHP extension is now required by Moodle, in order to improve site performance and to offer multilingual compatibility.';
$string['curlsecurityallowedport'] = 'cURL allowed ports list';
$string['curlsecurityallowedportsyntax'] = 'List of port numbers that cURL can connect to. Valid entries are integer numbers only. Put each entry on a new line. If left empty, then all ports are allowed. If set, in almost all cases, both 443 and 80 should be specified for cURL to connect to standard HTTPS and HTTP ports.';
Expand Down
8 changes: 8 additions & 0 deletions lib/cronlib.php
Expand Up @@ -61,6 +61,14 @@ function cron_run() {
$timenow = time();
mtrace("Server Time: ".date('r', $timenow)."\n\n");

// Record start time and interval between the last cron runs.
$laststart = get_config('tool_task', 'lastcronstart');
set_config('lastcronstart', $timenow, 'tool_task');
if ($laststart) {
// Record the interval between last two runs (always store at least 1 second).
set_config('lastcroninterval', max(1, $timenow - $laststart), 'tool_task');
}

// Run all scheduled tasks.
while (!\core\task\manager::static_caches_cleared_since($timenow) &&
$task = \core\task\manager::get_next_scheduled_task($timenow)) {
Expand Down

0 comments on commit b18034e

Please sign in to comment.