Skip to content

Commit

Permalink
[#174] Add error log check back in
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhilton committed Nov 14, 2024
1 parent a187c2c commit a90deb8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions classes/hook_callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ public static function before_http_headers(\core\hook\output\before_http_headers
if (class_exists('\core\check\manager')) {
\tool_heartbeat\check\cachecheck::ping('web');
}

lib::process_error_log_ping();
}
}
28 changes: 28 additions & 0 deletions classes/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,33 @@ public static function record_cache_checked(int $valueincache, int $valueindb, s
error_log("Heartbeat cache was checked: " . json_encode($details));
// @codingStandardsIgnoreEnd
}

/**
* Handles error logging pinging. This happens on a regular schedule e.g. every 30 mins.
* This is used in conjunction with external monitoring services to monitor if the error log is fresh
* (or alternatively if it is stale, because the logs are not coming through anymore).
*/
public static function process_error_log_ping() {
$lastpinged = get_config('tool_heartbeat', 'errorloglastpinged') ?: 0;
$errorperiod = get_config('tool_heartbeat', 'errorlog');

// If zero/null - disabled - don't do anything.
if (empty($errorperiod)) {
return;
}

if (($lastpinged + $errorperiod) < time()) {
// Update the last pinged time.
set_config('errorloglastpinged', time(), 'tool_heartbeat');

// Log to error_log.
$now = userdate(time());
$period = format_time($errorperiod);

// @codingStandardsIgnoreStart
error_log("Heartbeat error log test $now, next test expected in $period");
// @codingStandardsIgnoreEnd
}
}
}

1 change: 1 addition & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function tool_heartbeat_before_http_headers() {
if (class_exists('\core\check\manager')) {
\tool_heartbeat\check\cachecheck::ping('web');
}
\tool_heartbeat\lib::process_error_log_ping();
}

/**
Expand Down
59 changes: 59 additions & 0 deletions tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,63 @@ public function test_get_allowed_ips() {
set_config('allowedips_forced', '', 'tool_heartbeat');
$this->assertEquals('', lib::get_allowed_ips());
}

/**
* Provides values to test error log ping.
* @return array
*/
public function process_error_log_ping_provider(): array {
return [
'no period set - disabled' => [
'errorloglastpinged' => null,
'errorlog' => null,
'expectedtimebefore' => null,
],
'only period set' => [
'errorloglastpinged' => null,
'errorlog' => 1 * MINSECS,
// Update to latest time.
'expectedtimebefore' => time() + 10,
],
'period has passed, time should change' => [
'errorloglastpinged' => 1,
'errorlog' => 1 * MINSECS,
// Update to latest time.
'expectedtimebefore' => time() + 10,
],
'period not passed yet, time unchanged' => [
'errorloglastpinged' => time(),
'errorlog' => 1 * MINSECS,
// Remain unchanged, i.e. exactly equal.
'expectedtimebefore' => time(),
],
];
}

/**
* Tests process_error_log_ping function
*
* @param int|null $errorloglastpinged next error value to set
* @param int|null $errorlog error log value to set
* @param bool $expectrun
* @dataProvider process_error_log_ping_provider
*/
public function test_process_error_log_ping(?int $errorloglastpinged, ?int $errorlog, ?int $expectedtimebefore) {
$this->resetAfterTest(true);
set_config('errorloglastpinged', $errorloglastpinged, 'tool_heartbeat');
set_config('errorlog', $errorlog, 'tool_heartbeat');
lib::process_error_log_ping();

$valueafter = get_config('tool_heartbeat', 'errorloglastpinged');

// Assert the time was not set at all.
if (is_null($expectedtimebefore)) {
$this->assertFalse($valueafter);
} else {
// New value should have set current time as the last pinged time.
// We use less than and add some buffer in the test cases to account
// for tests that might happen over a few seconds.
$this->assertLessThanOrEqual($expectedtimebefore, $valueafter);
}
}
}

0 comments on commit a90deb8

Please sign in to comment.