Allow modifing cron limits

[MAILPOET-2367]
This commit is contained in:
Pavel Dohnal
2019-09-23 14:29:51 +02:00
committed by Jack Kitterhing
parent 4d3a005b20
commit 1bedf1fd1a
5 changed files with 19 additions and 7 deletions

View File

@@ -6,16 +6,29 @@ use MailPoet\Router\Endpoints\CronDaemon as CronDaemonEndpoint;
use MailPoet\Router\Router;
use MailPoet\Settings\SettingsController;
use MailPoet\Util\Security;
use MailPoet\WP\Functions;
use MailPoet\WP\Functions as WPFunctions;
class CronHelper {
const DAEMON_EXECUTION_LIMIT = 20; // seconds
const DAEMON_EXECUTION_TIMEOUT = 35; // seconds
const DAEMON_REQUEST_TIMEOUT = 5; // seconds
const DAEMON_SETTING = 'cron_daemon';
const DAEMON_STATUS_ACTIVE = 'active';
const DAEMON_STATUS_INACTIVE = 'inactive';
static function getDaemonExecutionLimit() {
$wp = Functions::get();
$limit = $wp->applyFilters('mailpoet_cron_get_execution_limit', self::DAEMON_EXECUTION_LIMIT);
return $limit;
}
static function getDaemonExecutionTimeout() {
$limit = self::getDaemonExecutionLimit();
$timeout = $limit * 1.75;
$wp = Functions::get();
return $wp->applyFilters('mailpoet_cron_get_execution_timeout', $timeout);
}
static function createDaemon($token) {
$daemon = [
'token' => $token,
@@ -189,7 +202,7 @@ class CronHelper {
static function enforceExecutionLimit($timer) {
$elapsed_time = microtime(true) - $timer;
if ($elapsed_time >= self::DAEMON_EXECUTION_LIMIT) {
if ($elapsed_time >= self::getDaemonExecutionLimit()) {
throw new \Exception(__('Maximum execution time has been reached.', 'mailpoet'));
}
}

View File

@@ -73,8 +73,8 @@ class DaemonHttpRunner {
// if workers took less time to execute than the daemon execution limit,
// pause daemon execution to ensure that daemon runs only once every X seconds
$elapsed_time = microtime(true) - $this->timer;
if ($elapsed_time < CronHelper::DAEMON_EXECUTION_LIMIT) {
$this->pauseExecution(CronHelper::DAEMON_EXECUTION_LIMIT - $elapsed_time);
if ($elapsed_time < CronHelper::getDaemonExecutionLimit()) {
$this->pauseExecution(CronHelper::getDaemonExecutionLimit() - $elapsed_time);
}
}
// after each execution, re-read daemon data in case it changed

View File

@@ -13,7 +13,7 @@ class Supervisor {
function checkDaemon() {
$daemon = $this->daemon;
$execution_timeout_exceeded =
(time() - (int)$daemon['updated_at']) >= CronHelper::DAEMON_EXECUTION_TIMEOUT;
(time() - (int)$daemon['updated_at']) >= CronHelper::getDaemonExecutionTimeout();
$daemon_is_inactive =
isset($daemon['status']) && $daemon['status'] === CronHelper::DAEMON_STATUS_INACTIVE;
if ($execution_timeout_exceeded || $daemon_is_inactive) {

View File

@@ -31,7 +31,6 @@ class CronHelperTest extends \MailPoetTest {
function testItDefinesConstants() {
expect(CronHelper::DAEMON_EXECUTION_LIMIT)->equals(20);
expect(CronHelper::DAEMON_EXECUTION_TIMEOUT)->equals(35);
expect(CronHelper::DAEMON_REQUEST_TIMEOUT)->equals(5);
expect(CronHelper::DAEMON_SETTING)->equals('cron_daemon');
}

View File

@@ -52,7 +52,7 @@ class SupervisorTest extends \MailPoetTest {
function testRestartsDaemonWhenExecutionDurationIsAboveLimit() {
if (getenv('WP_TEST_ENABLE_NETWORK_TESTS') !== 'true') $this->markTestSkipped();
$supervisor = new Supervisor();
$supervisor->daemon['updated_at'] = time() - CronHelper::DAEMON_EXECUTION_TIMEOUT;
$supervisor->daemon['updated_at'] = time() - CronHelper::getDaemonExecutionTimeout();
$daemon = $supervisor->checkDaemon();
expect(is_int($daemon['updated_at']))->true();
expect($daemon['updated_at'])->notEquals($supervisor->daemon['updated_at']);