diff --git a/lib/Config/Hooks.php b/lib/Config/Hooks.php index 4bf324d620..b073153408 100644 --- a/lib/Config/Hooks.php +++ b/lib/Config/Hooks.php @@ -1,5 +1,7 @@ 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); + } +} \ No newline at end of file diff --git a/lib/Config/Initializer.php b/lib/Config/Initializer.php index 78b0865374..091312ca40 100644 --- a/lib/Config/Initializer.php +++ b/lib/Config/Initializer.php @@ -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 diff --git a/lib/Cron/CronHelper.php b/lib/Cron/CronHelper.php index f2286a84ec..f2a27181d0 100644 --- a/lib/Cron/CronHelper.php +++ b/lib/Cron/CronHelper.php @@ -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.')); + } + } } \ No newline at end of file diff --git a/lib/Cron/Daemon.php b/lib/Cron/Daemon.php index 7e64a6b662..50519cc48c 100644 --- a/lib/Cron/Daemon.php +++ b/lib/Cron/Daemon.php @@ -1,7 +1,9 @@ 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; diff --git a/lib/Cron/Workers/Scheduler.php b/lib/Cron/Workers/Scheduler.php new file mode 100644 index 0000000000..235331fd4f --- /dev/null +++ b/lib/Cron/Workers/Scheduler.php @@ -0,0 +1,27 @@ +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.')); + } + } +} \ No newline at end of file diff --git a/lib/Cron/Workers/SendingQueue.php b/lib/Cron/Workers/SendingQueue.php index 9e19db3da5..a81fc0c8ca 100644 --- a/lib/Cron/Workers/SendingQueue.php +++ b/lib/Cron/Workers/SendingQueue.php @@ -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.')); - } - } } \ No newline at end of file