- Moves cron timeout/execution limit to the central cron helper class

This commit is contained in:
Vlad
2016-01-29 14:56:01 -05:00
parent 21847ca875
commit 70de0a01bf
4 changed files with 12 additions and 10 deletions

View File

@ -7,6 +7,9 @@ use MailPoet\Util\Security;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class CronHelper { class CronHelper {
static $daemon_execution_limit = 30;
static $daemon_timeout_limit = 30;
static function createDaemon($token) { static function createDaemon($token) {
$daemon = array( $daemon = array(
'status' => 'starting', 'status' => 'starting',

View File

@ -11,7 +11,6 @@ class Daemon {
public $daemon; public $daemon;
public $request_payload; public $request_payload;
public $refreshed_token; public $refreshed_token;
private $execution_time_limit = 30;
private $timer; private $timer;
function __construct($request_payload = array()) { function __construct($request_payload = array()) {
@ -40,12 +39,12 @@ class Daemon {
} catch(Exception $e) { } catch(Exception $e) {
} }
$elapsed_time = microtime(true) - $this->timer; $elapsed_time = microtime(true) - $this->timer;
if($elapsed_time < $this->execution_time_limit) { if($elapsed_time < CronHelper::$daemon_execution_limit) {
sleep($this->execution_time_limit - $elapsed_time); sleep(CronHelper::$daemon_execution_limit - $elapsed_time);
} }
// after each execution, re-read daemon data in case its status has changed // after each execution, re-read daemon data in case it was deleted or
// its status has changed
$daemon = CronHelper::getDaemon(); $daemon = CronHelper::getDaemon();
// if the token has changed, abort further processing
if(!$daemon || $daemon['token'] !== $this->request_payload['token']) { if(!$daemon || $daemon['token'] !== $this->request_payload['token']) {
exit; exit;
} }

View File

@ -7,7 +7,6 @@ class Supervisor {
public $daemon; public $daemon;
public $token; public $token;
public $force_run; public $force_run;
private $timeout_limit = 40;
function __construct($force_run = false) { function __construct($force_run = false) {
$this->daemon = CronHelper::getDaemon(); $this->daemon = CronHelper::getDaemon();
@ -32,13 +31,13 @@ class Supervisor {
$elapsed_time = time() - (int) $daemon['updated_at']; $elapsed_time = time() - (int) $daemon['updated_at'];
// if it's been less than 40 seconds since last execution and we're not // if it's been less than 40 seconds since last execution and we're not
// force-running the daemon, return its status and do nothing // force-running the daemon, return its status and do nothing
if($elapsed_time < $this->timeout_limit && !$this->force_run) { if($elapsed_time < CronHelper::$daemon_timeout_limit && !$this->force_run) {
return $this->formatDaemonStatusMessage($daemon['status']); return $this->formatDaemonStatusMessage($daemon['status']);
} }
// if it's been less than 40 seconds since last execution, we are // if it's been less than 40 seconds since last execution, we are
// force-running the daemon and it's either being started or stopped, // force-running the daemon and it's either being started or stopped,
// return its status and do nothing // return its status and do nothing
elseif($elapsed_time < $this->timeout_limit && elseif($elapsed_time < CronHelper::$daemon_timeout_limit &&
$this->force_run && $this->force_run &&
in_array($daemon['status'], array( in_array($daemon['status'], array(
'stopping', 'stopping',

View File

@ -1,6 +1,7 @@
<?php <?php
namespace MailPoet\Cron\Workers; namespace MailPoet\Cron\Workers;
use MailPoet\Cron\CronHelper;
use MailPoet\Mailer\Mailer; use MailPoet\Mailer\Mailer;
use MailPoet\Models\Newsletter; use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterStatistics; use MailPoet\Models\NewsletterStatistics;
@ -11,7 +12,7 @@ use MailPoet\Newsletter\Shortcodes\Shortcodes;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class SendingQueue { class SendingQueue {
public $timer; private $timer;
function __construct($timer = false) { function __construct($timer = false) {
$this->timer = ($timer) ? $timer : microtime(true); $this->timer = ($timer) ? $timer : microtime(true);
@ -120,7 +121,7 @@ class SendingQueue {
function checkExecutionTimer() { function checkExecutionTimer() {
$elapsed_time = microtime(true) - $this->timer; $elapsed_time = microtime(true) - $this->timer;
if($elapsed_time >= 30) throw new \Exception('Maximum execution time reached.'); if($elapsed_time >= CronHelper::$daemon_execution_limit) throw new \Exception(__('Maximum execution time reached.'));
} }
function getQueues() { function getQueues() {