- Implements sending frequency

- Updates sending queue worker
This commit is contained in:
Vlad
2016-02-11 22:46:45 -05:00
parent b29e31fdd6
commit d11badf3ce
2 changed files with 43 additions and 29 deletions

View File

@ -14,16 +14,16 @@ use MailPoet\Util\Helpers;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class SendingQueue { class SendingQueue {
public $mailer_config; public $mta_config;
public $mailer_log; public $mta_log;
public $processing_method; public $processing_method;
private $timer; private $timer;
const batch_size = 50; const batch_size = 50;
function __construct($timer = false) { function __construct($timer = false) {
$this->mailer_config = $this->getMailerConfig(); $this->mta_config = $this->getMailerConfig();
$this->mailer_log = $this->getMailerLog(); $this->mta_log = $this->getMailerLog();
$this->processing_method = ($this->mailer_config['method'] === 'MailPoet') ? $this->processing_method = ($this->mta_config['method'] === 'MailPoet') ?
'processBulkSubscribers' : 'processBulkSubscribers' :
'processIndividualSubscriber'; 'processIndividualSubscriber';
$this->timer = ($timer) ? $timer : microtime(true); $this->timer = ($timer) ? $timer : microtime(true);
@ -61,7 +61,6 @@ class SendingQueue {
$queue $queue
) )
); );
die('aaaa');
} }
} }
} }
@ -104,12 +103,12 @@ class SendingQueue {
$this->updateQueue($queue); $this->updateQueue($queue);
$this->checkSendingLimit(); $this->checkSendingLimit();
$this->checkExecutionTimer(); $this->checkExecutionTimer();
die('zzz');
return $queue->subscribers; return $queue->subscribers;
} }
function processIndividualSubscriber($mailer, $newsletter, $subscribers, $queue) { function processIndividualSubscriber($mailer, $newsletter, $subscribers, $queue) {
foreach($subscribers as $subscriber) { foreach($subscribers as $subscriber) {
$this->checkSendingLimit();
$processed_newsletter = $this->processNewsletter($newsletter, $subscriber); $processed_newsletter = $this->processNewsletter($newsletter, $subscriber);
$transformed_subscriber = $mailer->transformSubscriber($subscriber); $transformed_subscriber = $mailer->transformSubscriber($subscriber);
$result = $this->sendNewsletter( $result = $this->sendNewsletter(
@ -130,7 +129,6 @@ class SendingQueue {
$this->updateNewsletterStatistics($newsletter_statistics); $this->updateNewsletterStatistics($newsletter_statistics);
} }
$this->updateQueue($queue); $this->updateQueue($queue);
$this->checkSendingLimit();
$this->checkExecutionTimer(); $this->checkExecutionTimer();
} }
return $queue->subscribers; return $queue->subscribers;
@ -187,13 +185,6 @@ class SendingQueue {
return $mailer; return $mailer;
} }
function checkExecutionTimer() {
$elapsed_time = microtime(true) - $this->timer;
if($elapsed_time >= CronHelper::daemon_execution_limit) {
throw new \Exception(__('Maximum execution time reached.'));
}
}
function getQueues() { function getQueues() {
return \MailPoet\Models\SendingQueue::orderByDesc('priority') return \MailPoet\Models\SendingQueue::orderByDesc('priority')
->whereNull('deleted_at') ->whereNull('deleted_at')
@ -226,30 +217,53 @@ class SendingQueue {
} }
function updateMailerLog() { function updateMailerLog() {
$this->mailer_log['sent']++; $this->mta_log['sent']++;
return Setting::setValue('mailer_log', $this->mailer_log); return Setting::setValue('mta_log', $this->mta_log);
}
function checkSendingLimit() {
// TODO: enforce sending frequency
} }
function getMailerConfig() { function getMailerConfig() {
$mailer_config = Setting::getValue('mta'); $mta_config = Setting::getValue('mta');
if(!$mailer_config) { if(!$mta_config) {
throw new \Exception(__('Mailer is not configured.')); throw new \Exception(__('Mailer is not configured.'));
} }
return $mailer_config; return $mta_config;
} }
function getMailerLog() { function getMailerLog() {
$mailer_log = Setting::getValue('mta_log'); $mta_log = Setting::getValue('mta_log');
if(!$mailer_log) { if(!$mta_log) {
$mailer_log = array( $mta_log = array(
'sent' => 0, 'sent' => 0,
'started' => time() 'started' => time()
); );
Setting::setValue('mta_log', $mta_log);
}
return $mta_log;
}
function checkSendingLimit() {
$frequency_interval = (int) $this->mta_config['frequency']['interval'] * 60;
$frequency_limit = (int) $this->mta_config['frequency']['emails'];
$elapsed_time = time() - (int) $this->mta_log['started'];
if($this->mta_log['sent'] === $frequency_limit &&
$elapsed_time <= $frequency_interval
) {
throw new \Exception(__('Sending frequency limit reached.'));
}
if($elapsed_time > $frequency_interval) {
$this->mta_log = array(
'sent' => 0,
'started' => time()
);
Setting::setValue('mta_log', $this->mta_log);
}
return;
}
function checkExecutionTimer() {
$elapsed_time = microtime(true) - $this->timer;
if($elapsed_time >= CronHelper::daemon_execution_limit) {
throw new \Exception(__('Maximum execution time reached.'));
} }
return $mailer_log;
} }
} }

View File

@ -52,7 +52,7 @@ class SendingQueue {
} }
$subscriber_ids = array_unique($subscriber_ids); $subscriber_ids = array_unique($subscriber_ids);
$queue->subscribers = json_encode( $queue->subscribers = serialize(
array( array(
'to_process' => $subscriber_ids 'to_process' => $subscriber_ids
) )