Detect not accessible cron daemon [MAILPOET-801]
This adds a function to a CronHelper which detect accessibility problem based on timestamps which are stored during cron execution. The main idea is that the cron daemon should update run_started_at within the REQUEST_TIMEOUT after it was accessed (run_accessed_at).
This commit is contained in:
@@ -84,6 +84,26 @@ class CronHelper {
|
||||
return WPFunctions::wpRemoteRetrieveBody($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean|null
|
||||
*/
|
||||
static function isDaemonAccessible() {
|
||||
$daemon = self::getDaemon();
|
||||
if(!$daemon || $daemon['run_accessed_at'] === null) {
|
||||
return null;
|
||||
}
|
||||
if($daemon['run_accessed_at'] <= (int)$daemon['run_started_at']) {
|
||||
return true;
|
||||
}
|
||||
if(
|
||||
$daemon['run_accessed_at'] + self::DAEMON_REQUEST_TIMEOUT < time() &&
|
||||
$daemon['run_accessed_at'] > (int)$daemon['run_started_at']
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static function queryCronUrl($url) {
|
||||
$args = WPHooks::applyFilters(
|
||||
'mailpoet_cron_request_args',
|
||||
|
@@ -125,6 +125,77 @@ class CronHelperTest extends \MailPoetTest {
|
||||
}
|
||||
}
|
||||
|
||||
function testItDetectsNotAccessibleDaemon() {
|
||||
$time = time();
|
||||
$run_start_values = [null, $time - 20];
|
||||
foreach($run_start_values as $run_start) {
|
||||
$daemon = [
|
||||
'token' => 'some_token',
|
||||
'updated_at' => 12345678,
|
||||
'run_accessed_at' => $time - 10,
|
||||
'run_started_at' => $run_start,
|
||||
'run_completed_at' => null,
|
||||
'last_error' => null,
|
||||
];
|
||||
Setting::setValue(
|
||||
CronHelper::DAEMON_SETTING,
|
||||
$daemon
|
||||
);
|
||||
expect(CronHelper::isDaemonAccessible())->false();
|
||||
}
|
||||
}
|
||||
|
||||
function testItDetectsAccessibleDaemon() {
|
||||
$time = time();
|
||||
$daemon = [
|
||||
'token' => 'some_token',
|
||||
'updated_at' => 12345678,
|
||||
'run_accessed_at' => $time - 5,
|
||||
'run_started_at' => $time - 4,
|
||||
'run_completed_at' => null,
|
||||
'last_error' => null,
|
||||
];
|
||||
Setting::setValue(
|
||||
CronHelper::DAEMON_SETTING,
|
||||
$daemon
|
||||
);
|
||||
expect(CronHelper::isDaemonAccessible())->true();
|
||||
}
|
||||
|
||||
function testItDetectsUnknownStateOfTheDaemon() {
|
||||
$time = time();
|
||||
$test_inputs = [
|
||||
[
|
||||
'run_access' => null,
|
||||
'run_start' => null,
|
||||
],
|
||||
[
|
||||
'run_access' => $time - 4,
|
||||
'run_start' => null,
|
||||
],
|
||||
[
|
||||
'run_access' => $time - 4,
|
||||
'run_start' => $time - 10,
|
||||
],
|
||||
null,
|
||||
];
|
||||
foreach($test_inputs as $test_input) {
|
||||
$daemon = [
|
||||
'token' => 'some_token',
|
||||
'updated_at' => 12345678,
|
||||
'run_accessed_at' => $test_input['run_access'],
|
||||
'run_started_at' => $test_input['run_start'],
|
||||
'run_completed_at' => null,
|
||||
'last_error' => null,
|
||||
];
|
||||
Setting::setValue(
|
||||
CronHelper::DAEMON_SETTING,
|
||||
$daemon
|
||||
);
|
||||
expect(CronHelper::isDaemonAccessible())->null();
|
||||
}
|
||||
}
|
||||
|
||||
function testItCreatesRandomToken() {
|
||||
// random token is a string of 5 characters
|
||||
$token1 = CronHelper::createToken();
|
||||
|
Reference in New Issue
Block a user