- Updates Daemon to execute workers via WP's action hook

- Bootstraps Scheduler class
This commit is contained in:
Vlad
2016-03-03 15:42:10 -05:00
parent 9970ad7fb6
commit c4b728f4e1
6 changed files with 58 additions and 13 deletions

View File

@ -1,5 +1,7 @@
<?php
namespace MailPoet\Config;
use MailPoet\Cron\Workers\Scheduler;
use MailPoet\Cron\Workers\SendingQueue;
use \MailPoet\Models\Setting;
class Hooks {
@ -11,6 +13,7 @@ class Hooks {
$this->setupWPUsers();
$this->setupImageSize();
$this->setupListing();
$this->setupCronWorkers();
}
function setupSubscribe() {
@ -144,4 +147,15 @@ class Hooks {
return $status;
}
}
function setupCronWorkers() {
add_action('mailpoet_cron_worker', function($timer) {
$scheduler = new Scheduler($timer);
$scheduler->process();
}, 10, 1);
add_action('mailpoet_cron_worker', function($timer) {
$sending_queue = new SendingQueue($timer);
$sending_queue->process();
}, 10, 1);
}
}

View File

@ -34,12 +34,13 @@ class Initializer {
$this->setupLocalizer();
$this->setupMenu();
$this->setupPermissions();
$this->setupPublicAPI();
$this->setupAnalytics();
$this->setupChangelog();
$this->setupShortcodes();
$this->setupHooks();
$this->setupImages();
$this->setupPublicAPI();
$this->runQueueSupervisor();
} catch(\Exception $e) {
// if anything goes wrong during init
// automatically deactivate the plugin

View File

@ -68,4 +68,11 @@ class CronHelper {
// throw an error if all connection attempts failed
throw new \Exception(__('Site URL is unreachable.'));
}
static function checkExecutionTimer($timer) {
$elapsed_time = microtime(true) - $timer;
if($elapsed_time >= self::daemon_execution_limit) {
throw new \Exception(__('Maximum execution time reached.'));
}
}
}

View File

@ -1,7 +1,9 @@
<?php
namespace MailPoet\Cron;
use MailPoet\Cron\Workers\Scheduler;
use MailPoet\Cron\Workers\SendingQueue;
use MailPoet\Models\Newsletter;
require_once(ABSPATH . 'wp-includes/pluggable.php');
@ -35,8 +37,7 @@ class Daemon {
}
$this->abortIfStopped($daemon);
try {
$sending_queue = new SendingQueue($this->timer);
$sending_queue->process();
do_action('mailpoet_cron_worker', $this->timer);
} catch(\Exception $e) {
}
$elapsed_time = microtime(true) - $this->timer;

View File

@ -0,0 +1,27 @@
<?php
namespace MailPoet\Cron\Workers;
use MailPoet\Cron\CronHelper;
use MailPoet\Models\Setting;
use MailPoet\Util\Security;
if(!defined('ABSPATH')) exit;
class Scheduler {
public $timer;
function __construct($timer = false) {
$this->timer = ($timer) ? $timer : microtime(true);
CronHelper::checkExecutionTimer($this->timer);
}
function process() {
}
function checkExecutionTimer() {
$elapsed_time = microtime(true) - $this->timer;
if($elapsed_time >= CronHelper::daemon_execution_limit) {
throw new \Exception(__('Maximum execution time reached.'));
}
}
}

View File

@ -10,6 +10,7 @@ use MailPoet\Models\Subscriber;
use MailPoet\Newsletter\Renderer\Renderer;
use MailPoet\Newsletter\Shortcodes\Shortcodes;
use MailPoet\Util\Helpers;
use MailPoet\Util\Security;
if(!defined('ABSPATH')) exit;
@ -27,6 +28,7 @@ class SendingQueue {
'processBulkSubscribers' :
'processIndividualSubscriber';
$this->timer = ($timer) ? $timer : microtime(true);
CronHelper::checkExecutionTimer($this->timer);
}
function process() {
@ -102,7 +104,7 @@ class SendingQueue {
}
$this->updateQueue($queue);
$this->checkSendingLimit();
$this->checkExecutionTimer();
CronHelper::checkExecutionTimer($this->timer);
return $queue->subscribers;
}
@ -129,7 +131,7 @@ class SendingQueue {
$this->updateNewsletterStatistics($newsletter_statistics);
}
$this->updateQueue($queue);
$this->checkExecutionTimer();
CronHelper::checkExecutionTimer($this->timer);
}
return $queue->subscribers;
}
@ -259,11 +261,4 @@ class SendingQueue {
}
return;
}
function checkExecutionTimer() {
$elapsed_time = microtime(true) - $this->timer;
if($elapsed_time >= CronHelper::daemon_execution_limit) {
throw new \Exception(__('Maximum execution time reached.'));
}
}
}