Permalink
Browse files

feature(cron): improved cron logging

Cron output is now logged and displayed in admin statistics, so as to
indicate cron completion.

Cron hooks triggered by _elgg_cron_run() now receive correct return value
for a given period
rather than the global output value.

Cron monitor now echoes cron period name and completion timestamp rather
than 1.

Fixes #9474
  • Loading branch information...
hypeJunction committed Mar 11, 2016
1 parent b96c736 commit 5305b60dadceb9056b684d7359ffd389b4d50f8b
Showing with 36 additions and 12 deletions.
  1. +25 −10 engine/lib/cron.php
  2. +3 −0 languages/en.php
  3. +8 −2 views/default/admin/statistics/cron.php
View
@@ -53,10 +53,7 @@ function _elgg_cron_run() {
$params = array();
$params['time'] = $now;
- // Data to return to
- $old_stdout = "";
- $std_out = "";
- ob_start();
+ $all_std_out = "";
$periods = array(
'minute' => 60,
@@ -75,15 +72,25 @@ function _elgg_cron_run() {
$key = "cron_latest:$period:ts";
$ts = elgg_get_site_entity()->getPrivateSetting($key);
$deadline = $ts + $interval;
+
if ($now > $deadline) {
- $old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, $old_stdout);
- $std_out .= ob_clean();
- $std_out .= $old_stdout;
+ $msg_key = "cron_latest:$period:msg";
+ $msg = elgg_echo('admin:cron:started', [$period, date('r', time())]);
+ elgg_get_site_entity()->setPrivateSetting($msg_key, $msg);
+
+ ob_start();
+
+ $old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, '');
+ $std_out = ob_get_clean();
+
+ $period_std_out = $std_out . $old_stdout;
+ $all_std_out .= $period_std_out;
+
+ elgg_get_site_entity()->setPrivateSetting($msg_key, $period_std_out);
}
}
- $std_out .= ob_get_clean();
- echo $std_out;
+ echo $all_std_out;
}
/**
@@ -119,10 +126,17 @@ function _elgg_cron_page_handler($page) {
$old_stdout = "";
ob_start();
+ $msg_key = "cron_latest:$period:msg";
+ $msg = elgg_echo('admin:cron:started', [$period, date('r', time())]);
+ elgg_get_site_entity()->setPrivateSetting($msg_key, $msg);
+
$old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, $old_stdout);
$std_out = ob_get_clean();
- echo $std_out . $old_stdout;
+ $msg = $std_out . $old_stdout;
+ echo $msg;
+
+ elgg_get_site_entity()->setPrivateSetting($msg_key, $msg);
}
return true;
}
@@ -144,6 +158,7 @@ function _elgg_cron_monitor($hook, $period, $output, $params) {
if (in_array($period, $periods)) {
$key = "cron_latest:$period:ts";
elgg_get_site_entity()->setPrivateSetting($key, $time);
+ echo elgg_echo('admin:cron:complete', [$period, date('r', $time)]);
}
}
View
@@ -477,6 +477,9 @@
'admin:cron:period' => 'Cron period',
'admin:cron:friendly' => 'Last completed',
'admin:cron:date' => 'Date and time',
+ 'admin:cron:msg' => 'Message',
+ 'admin:cron:started' => 'Cron jobs for "%s" started at %s',
+ 'admin:cron:complete' => 'Cron jobs for "%s" completed at %s',
'admin:appearance' => 'Appearance',
'admin:administer_utilities' => 'Utilities',
@@ -18,16 +18,22 @@
$date = '';
}
- $table_content .= "<tr><td>$name</td><td>$friendly_time</td><td>$date</td><tr>";
+ $msg_key = "cron_latest:$period:msg";
+ $msg = elgg_get_site_entity()->getPrivateSetting($msg_key);
+ if ($msg) {
+ $msg = nl2br($msg);
+ }
+ $table_content .= "<tr><td>$name</td><td>$friendly_time</td><td>$date</td><td>$msg</td><tr>";
}
$period_hd = elgg_echo('admin:cron:period');
$friendly_hd = elgg_echo('admin:cron:friendly');
$date_hd = elgg_echo('admin:cron:date');
+$msg_hd = elgg_echo('admin:cron:msg');
$table = <<<HTML
<table class="elgg-table">
- <tr><th>$period_hd</th><th>$friendly_hd</th><th>$date_hd</th></tr>
+ <tr><th>$period_hd</th><th>$friendly_hd</th><th>$date_hd</th><th>$msg_hd</th></tr>
$table_content
</table>
HTML;

0 comments on commit 5305b60

Please sign in to comment.