Use CronHelper service in cron workers [MAILPOET-2459]

This commit is contained in:
wxa
2019-10-30 16:26:38 +03:00
committed by Jack Kitterhing
parent 3436fed6e7
commit 81caa04479
14 changed files with 131 additions and 60 deletions

View File

@ -59,7 +59,7 @@ class Bounce extends SimpleWorker {
foreach ($subscriber_batches as $subscribers_to_process_ids) {
// abort if execution limit is reached
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
$subscriber_emails = Subscriber::select('email')
->whereIn('id', $subscribers_to_process_ids)

View File

@ -55,10 +55,10 @@ class InactiveSubscribers extends SimpleWorker {
$last_subscriber_id += self::BATCH_SIZE;
$task->meta = ['last_subscriber_id' => $last_subscriber_id];
$task->save();
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
};
while ($this->inactive_subscribers_controller->markActiveSubscribers($days_to_inactive, self::BATCH_SIZE) === self::BATCH_SIZE) {
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
};
self::schedule();
return true;

View File

@ -28,10 +28,19 @@ class Scheduler {
/** @var LoggerFactory */
private $logger_factory;
function __construct(SubscribersFinder $subscribers_finder, LoggerFactory $logger_factory, $timer = false) {
/** @var CronHelper */
private $cron_helper;
function __construct(
SubscribersFinder $subscribers_finder,
LoggerFactory $logger_factory,
CronHelper $cron_helper,
$timer = false
) {
$this->timer = ($timer) ? $timer : microtime(true);
// abort if execution limit is reached
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper = $cron_helper;
$this->cron_helper->enforceExecutionLimit($this->timer);
$this->subscribers_finder = $subscribers_finder;
$this->logger_factory = $logger_factory;
}
@ -55,7 +64,7 @@ class Scheduler {
} elseif ($newsletter->type === Newsletter::TYPE_AUTOMATIC) {
$this->processScheduledAutomaticEmail($newsletter, $queue);
}
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
}
}

View File

@ -132,7 +132,7 @@ class Migration extends SimpleWorker {
if (!empty($queues)) {
foreach (array_chunk($queues, self::BATCH_SIZE) as $queue_batch) {
// abort if execution limit is reached
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
foreach ($queue_batch as $queue) {
// create a new scheduled task of type "sending"
@ -181,7 +181,7 @@ class Migration extends SimpleWorker {
if (!empty($task_ids)) {
foreach ($task_ids as $task_id) {
// abort if execution limit is reached
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
$this->migrateTaskSubscribers($task_id);
}
@ -216,7 +216,7 @@ class Migration extends SimpleWorker {
$subscribers_to_migrate = array_slice($subscribers['to_process'], $migrated_unprocessed_count);
foreach ($subscribers_to_migrate as $sub_id) {
// abort if execution limit is reached
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
ScheduledTaskSubscriber::createOrUpdate([
'task_id' => $task_id,
@ -230,7 +230,7 @@ class Migration extends SimpleWorker {
$subscribers_to_migrate = array_slice($subscribers['processed'], $migrated_processed_count);
foreach ($subscribers_to_migrate as $sub_id) {
// abort if execution limit is reached
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
ScheduledTaskSubscriber::createOrUpdate([
'task_id' => $task_id,

View File

@ -45,11 +45,15 @@ class SendingQueue {
/** @var NewslettersRepository */
private $newsletters_repository;
/** @var CronHelper */
private $cron_helper;
function __construct(
SendingErrorHandler $error_handler,
StatsNotificationsScheduler $stats_notifications_scheduler,
LoggerFactory $logger_factory,
NewslettersRepository $newsletters_repository,
CronHelper $cron_helper,
$timer = false,
$mailer_task = false,
$newsletter_task = false
@ -64,6 +68,7 @@ class SendingQueue {
$this->batch_size = $wp->applyFilters('mailpoet_cron_worker_sending_queue_batch_size', self::BATCH_SIZE);
$this->logger_factory = $logger_factory;
$this->newsletters_repository = $newsletters_repository;
$this->cron_helper = $cron_helper;
}
function process() {
@ -294,7 +299,7 @@ class SendingQueue {
function enforceSendingAndExecutionLimits() {
// abort if execution limit is reached
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
// abort if sending limit has been reached
MailerLog::enforceExecutionRequirements();
}

View File

@ -4,6 +4,7 @@ namespace MailPoet\Cron\Workers;
use Carbon\Carbon;
use MailPoet\Cron\CronHelper;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Models\ScheduledTask;
use MailPoet\WP\Functions as WPFunctions;
@ -18,13 +19,17 @@ abstract class SimpleWorker {
const TASK_RUN_TIMEOUT = 120;
const TIMED_OUT_TASK_RESCHEDULE_TIMEOUT = 5;
/** @var CronHelper */
protected $cron_helper;
function __construct($timer = false) {
if (static::TASK_TYPE === null) {
throw new \Exception('Constant TASK_TYPE is not defined on subclass ' . get_class($this));
}
$this->timer = ($timer) ? $timer : microtime(true);
// abort if execution limit is reached
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper = ContainerWrapper::getInstance()->get(CronHelper::class);
$this->cron_helper->enforceExecutionLimit($this->timer);
$this->wp = new WPFunctions();
}
@ -92,14 +97,14 @@ abstract class SimpleWorker {
$task->save();
// abort if execution limit is reached
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
return true;
}
function processTask(ScheduledTask $task) {
// abort if execution limit is reached
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
if (!static::SUPPORT_MULTIPLE_INSTANCES) {
if ($this->rescheduleOutdated($task)) {

View File

@ -36,6 +36,9 @@ class Worker {
/** @var SettingsController */
private $settings;
/** @var CronHelper */
private $cron_helper;
/** @var MetaInfo */
private $mailerMetaInfo;
@ -55,6 +58,7 @@ class Worker {
Mailer $mailer,
Renderer $renderer,
SettingsController $settings,
CronHelper $cron_helper,
MetaInfo $mailerMetaInfo,
StatsNotificationsRepository $repository,
NewsletterLinkRepository $newsletter_link_repository,
@ -66,6 +70,7 @@ class Worker {
$this->renderer = $renderer;
$this->mailer = $mailer;
$this->settings = $settings;
$this->cron_helper = $cron_helper;
$this->mailerMetaInfo = $mailerMetaInfo;
$this->repository = $repository;
$this->entity_manager = $entity_manager;
@ -89,7 +94,7 @@ class Worker {
} finally {
$this->markTaskAsFinished($stats_notification_entity->getTask());
}
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
}
}

View File

@ -18,13 +18,13 @@ class UnsubscribeTokens extends SimpleWorker {
function processTaskStrategy(ScheduledTask $task) {
$meta = $task->getMeta();
do {
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
$subscribers_count = $this->addTokens(Subscriber::class, $meta['last_subscriber_id']);
$task->meta = $meta;
$task->save();
} while ($subscribers_count === self::BATCH_SIZE);
do {
CronHelper::enforceExecutionLimit($this->timer);
$this->cron_helper->enforceExecutionLimit($this->timer);
$newsletters_count = $this->addTokens(Newsletter::class, $meta['last_newsletter_id']);
$task->meta = $meta;
$task->save();

View File

@ -3,6 +3,7 @@
namespace MailPoet\Cron\Workers;
use MailPoet\Config\Renderer;
use MailPoet\Cron\CronHelper;
use MailPoet\Cron\Workers\Bounce as BounceWorker;
use MailPoet\Cron\Workers\KeyCheck\PremiumKeyCheck as PremiumKeyCheckWorker;
use MailPoet\Cron\Workers\KeyCheck\SendingServiceKeyCheck as SendingServiceKeyCheckWorker;
@ -45,6 +46,9 @@ class WorkersFactory {
/** @var SettingsController */
private $settings;
/** @var CronHelper */
private $cron_helper;
/** @var WooCommerceSegment */
private $woocommerce_segment;
@ -95,6 +99,7 @@ class WorkersFactory {
Mailer $mailer,
Renderer $renderer,
SettingsController $settings,
CronHelper $cron_helper,
WooCommerceSegment $woocommerce_segment,
InactiveSubscribersController $inactive_subscribers_controller,
WooCommerceHelper $woocommerce_helper,
@ -114,6 +119,7 @@ class WorkersFactory {
$this->mailer = $mailer;
$this->renderer = $renderer;
$this->settings = $settings;
$this->cron_helper = $cron_helper;
$this->woocommerce_segment = $woocommerce_segment;
$this->inactive_subscribers_controller = $inactive_subscribers_controller;
$this->woocommerce_helper = $woocommerce_helper;
@ -131,7 +137,7 @@ class WorkersFactory {
/** @return SchedulerWorker */
function createScheduleWorker($timer) {
return new SchedulerWorker($this->subscribers_finder, $this->logger_factory, $timer);
return new SchedulerWorker($this->subscribers_finder, $this->logger_factory, $this->cron_helper, $timer);
}
/** @return SendingQueueWorker */
@ -141,6 +147,7 @@ class WorkersFactory {
$this->statsNotificationsScheduler,
$this->logger_factory,
$this->newsletters_repository,
$this->cron_helper,
$timer
);
}
@ -151,6 +158,7 @@ class WorkersFactory {
$this->mailer,
$this->renderer,
$this->settings,
$this->cron_helper,
$this->mailerMetaInfo,
$this->stats_notifications_repository,
$this->newsletter_link_repository,

View File

@ -6,6 +6,7 @@ use Carbon\Carbon;
use Codeception\Stub;
use MailPoet\Cron\CronHelper;
use MailPoet\Cron\Workers\InactiveSubscribers;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Models\ScheduledTask;
use MailPoet\Settings\SettingsController;
use MailPoet\Subscribers\InactiveSubscribersController;
@ -19,6 +20,7 @@ class InactiveSubscribersTest extends \MailPoetTest {
$this->settings = new SettingsController();
\ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
$this->settings->set('tracking.enabled', true);
$this->cron_helper = ContainerWrapper::getInstance()->get(CronHelper::class);
parent::_before();
}
@ -115,7 +117,7 @@ class InactiveSubscribersTest extends \MailPoetTest {
'reactivateInactiveSubscribers' => Stub\Expected::never(),
], $this);
$worker = new InactiveSubscribers($controller_mock, $this->settings, microtime(true) - (CronHelper::getDaemonExecutionLimit() - 1));
$worker = new InactiveSubscribers($controller_mock, $this->settings, microtime(true) - ($this->cron_helper->getDaemonExecutionLimit() - 1));
sleep(1);
$this->setExpectedException(\Exception::class, 'Maximum execution time has been reached.');
$worker->processTaskStrategy(ScheduledTask::createOrUpdate([]));

View File

@ -7,6 +7,7 @@ use Codeception\Stub;
use Codeception\Stub\Expected;
use MailPoet\Cron\CronHelper;
use MailPoet\Cron\Workers\Scheduler;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Logging\LoggerFactory;
use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterOption;
@ -30,19 +31,20 @@ class SchedulerTest extends \MailPoetTest {
function _before() {
parent::_before();
$this->logger_factory = LoggerFactory::getInstance();
$this->cron_helper = ContainerWrapper::getInstance()->get(CronHelper::class);
}
function testItConstructs() {
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
expect($scheduler->timer)->greaterOrEquals(5);
$timer = microtime(true) - 2;
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $timer);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper, $timer);
expect($scheduler->timer)->equals($timer);
}
function testItThrowsExceptionWhenExecutionLimitIsReached() {
try {
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, microtime(true) - CronHelper::getDaemonExecutionLimit());
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper, microtime(true) - $this->cron_helper->getDaemonExecutionLimit());
self::fail('Maximum execution time limit exception was not thrown.');
} catch (\Exception $e) {
expect($e->getMessage())->equals('Maximum execution time has been reached.');
@ -71,7 +73,7 @@ class SchedulerTest extends \MailPoetTest {
expect($notification_history)->isEmpty();
// create notification history and ensure that it exists
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
$scheduler->createNotificationHistory($newsletter->id);
$notification_history = Newsletter::where('type', Newsletter::TYPE_NOTIFICATION_HISTORY)
->where('parent_id', $newsletter->id)
@ -87,7 +89,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_WELCOME)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
// queue and associated newsletter should be deleted when interval type is set to "immediately"
expect(SendingQueue::findMany())->notEmpty();
@ -103,7 +105,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_WELCOME)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
// queue's next run date should change when interval type is set to anything
// other than "immediately"
@ -133,7 +135,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_WELCOME)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
// return false and delete queue when subscriber is not a WP user
$result = $scheduler->verifyWPSubscriber($subscriber->id, $newsletter, $queue);
@ -156,7 +158,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_WELCOME)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
// return false and delete queue when subscriber role is different from the one
// specified for the welcome email
@ -178,7 +180,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_WELCOME)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
// return true when user exists and WP role matches the one specified for the welcome email
$result = $scheduler->verifyWPSubscriber($subscriber->id, $newsletter, $queue);
@ -199,7 +201,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_WELCOME)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
// true when user exists and has any role
$result = $scheduler->verifyWPSubscriber($subscriber->id, $newsletter, $queue);
@ -213,7 +215,7 @@ class SchedulerTest extends \MailPoetTest {
$queue->setSubscribers([]);
// delete queue when the list of subscribers to process is blank
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
$result = $scheduler->processWelcomeNewsletter($newsletter, $queue);
expect($result)->false();
expect(SendingQueue::findMany())->count(0);
@ -283,7 +285,7 @@ class SchedulerTest extends \MailPoetTest {
}
function testItFailsMailpoetSubscriberVerificationWhenSubscriberDoesNotExist() {
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
$newsletter = $this->_createNewsletter();
$queue = $this->_createQueue($newsletter->id);
@ -303,7 +305,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_NOTIFICATION)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
// return false
$result = $scheduler->verifyMailpoetSubscriber($subscriber->id, $newsletter, $queue);
@ -328,7 +330,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_NOTIFICATION)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
// return false
$result = $scheduler->verifyMailpoetSubscriber($subscriber->id, $newsletter, $queue);
@ -357,7 +359,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_NOTIFICATION)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
// return false
$result = $scheduler->verifyMailpoetSubscriber($subscriber->id, $newsletter, $queue);
@ -380,7 +382,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_NOTIFICATION)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
// return true after successful verification
$result = $scheduler->verifyMailpoetSubscriber($subscriber->id, $newsletter, $queue);
@ -402,7 +404,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_NOTIFICATION)
->findOne($newsletter->id);
$queue = $this->_createQueue($newsletter->id);
$scheduler = new Scheduler(new SubscribersFinder(), $this->logger_factory);
$scheduler = new Scheduler(new SubscribersFinder(), $this->logger_factory, $this->cron_helper);
// return true
expect($scheduler->processScheduledStandardNewsletter($newsletter, $queue))->true();
@ -427,6 +429,7 @@ class SchedulerTest extends \MailPoetTest {
return false;
}),
'logger_factory' => $this->logger_factory,
'cron_helper' => $this->cron_helper,
], $this);
expect($scheduler->processPostNotificationNewsletter($newsletter, $queue))->false();
}
@ -438,7 +441,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter_segment = $this->_createNewsletterSegment($newsletter->id, $segment->id);
// delete or reschedule queue when there are no subscribers in segments
$scheduler = $this->construct(Scheduler::class, [new SubscribersFinder(), $this->logger_factory], [
$scheduler = $this->construct(Scheduler::class, [new SubscribersFinder(), $this->logger_factory, $this->cron_helper], [
'deleteQueueOrUpdateNextRunDate' => Expected::exactly(1, function() {
return false;
}),
@ -461,7 +464,7 @@ class SchedulerTest extends \MailPoetTest {
);
$newsletter = Newsletter::filter('filterWithOptions', Newsletter::TYPE_NOTIFICATION)
->findOne($newsletter->id);
$scheduler = new Scheduler(new SubscribersFinder(), $this->logger_factory);
$scheduler = new Scheduler(new SubscribersFinder(), $this->logger_factory, $this->cron_helper);
// return true
expect($scheduler->processPostNotificationNewsletter($newsletter, $queue))->true();
@ -482,7 +485,7 @@ class SchedulerTest extends \MailPoetTest {
}
function testItFailsToProcessWhenScheduledQueuesNotFound() {
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
expect($scheduler->process())->false();
}
@ -490,7 +493,7 @@ class SchedulerTest extends \MailPoetTest {
$queue = $this->_createQueue(1);
$queue->scheduled_at = Carbon::createFromTimestamp(current_time('timestamp'));
$queue->save();
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
$scheduler->process();
expect(SendingQueue::findMany())->count(0);
}
@ -502,7 +505,7 @@ class SchedulerTest extends \MailPoetTest {
$queue = $this->_createQueue($newsletter->id);
$queue->scheduled_at = Carbon::createFromTimestamp(current_time('timestamp'));
$queue->save();
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
$scheduler->process();
expect(SendingQueue::findMany())->count(0);
}
@ -514,6 +517,7 @@ class SchedulerTest extends \MailPoetTest {
$queue->save();
$scheduler = Stub::make(Scheduler::class, [
'processWelcomeNewsletter' => Expected::exactly(1),
'cron_helper' => $this->cron_helper,
], $this);
$scheduler->timer = microtime(true);
$scheduler->process();
@ -526,6 +530,7 @@ class SchedulerTest extends \MailPoetTest {
$queue->save();
$scheduler = Stub::make(Scheduler::class, [
'processPostNotificationNewsletter' => Expected::exactly(1),
'cron_helper' => $this->cron_helper,
], $this);
$scheduler->timer = microtime(true);
$scheduler->process();
@ -538,6 +543,7 @@ class SchedulerTest extends \MailPoetTest {
$queue->save();
$scheduler = Stub::make(Scheduler::class, [
'processScheduledStandardNewsletter' => Expected::exactly(1),
'cron_helper' => $this->cron_helper,
], $this);
$scheduler->timer = microtime(true);
$scheduler->process();
@ -550,8 +556,9 @@ class SchedulerTest extends \MailPoetTest {
$queue->save();
$scheduler = Stub::make(Scheduler::class, [
'processPostNotificationNewsletter' => Expected::exactly(1),
'cron_helper' => $this->cron_helper,
], $this);
$scheduler->timer = microtime(true) - CronHelper::getDaemonExecutionLimit();
$scheduler->timer = microtime(true) - $this->cron_helper->getDaemonExecutionLimit();
try {
$scheduler->process();
self::fail('Maximum execution time limit exception was not thrown.');
@ -568,6 +575,7 @@ class SchedulerTest extends \MailPoetTest {
$scheduler = Stub::make(Scheduler::class, [
'processScheduledStandardNewsletter' => Expected::never(),
'cron_helper' => $this->cron_helper,
], $this);
// scheduled job is not processed
$scheduler->timer = microtime(true);
@ -582,6 +590,7 @@ class SchedulerTest extends \MailPoetTest {
$scheduler = Stub::make(Scheduler::class, [
'processScheduledStandardNewsletter' => Expected::once(),
'cron_helper' => $this->cron_helper,
], $this);
// scheduled job is processed
$scheduler->timer = microtime(true);
@ -597,7 +606,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = $this->_createNewsletter(Newsletter::TYPE_STANDARD, Newsletter::STATUS_DRAFT);
$queue = $this->_createQueue($newsletter->id);
$finder = $this->makeEmpty(SubscribersFinder::class);
$scheduler = new Scheduler($finder, $this->logger_factory);
$scheduler = new Scheduler($finder, $this->logger_factory, $this->cron_helper);
$scheduler->processScheduledStandardNewsletter($newsletter, $queue);
$refetched_task = ScheduledTask::where('id', $task->id)->findOne();
@ -613,7 +622,7 @@ class SchedulerTest extends \MailPoetTest {
$newsletter = $this->_createNewsletter(Newsletter::TYPE_STANDARD, Newsletter::STATUS_DRAFT);
$queue = $this->_createQueue($newsletter->id);
$finder = $this->makeEmpty(SubscribersFinder::class);
$scheduler = new Scheduler($finder, $this->logger_factory);
$scheduler = new Scheduler($finder, $this->logger_factory, $this->cron_helper);
$scheduler->processScheduledStandardNewsletter($newsletter, $queue);
$refetched_task = ScheduledTask::where('id', $task->id)->findOne();
@ -628,6 +637,7 @@ class SchedulerTest extends \MailPoetTest {
$scheduler = Stub::make(Scheduler::class, [
'processScheduledStandardNewsletter' => Expected::once(),
'cron_helper' => $this->cron_helper,
], $this);
// scheduled job is processed
$scheduler->timer = microtime(true);
@ -651,7 +661,7 @@ class SchedulerTest extends \MailPoetTest {
expect($task->getSubscribers())->equals([$subscriber->id]);
// task should have its status set to null (i.e., sending)
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
$scheduler->process();
$task = SendingTask::getByNewsletterId($newsletter->id);
expect($task->status)->null();
@ -675,7 +685,7 @@ class SchedulerTest extends \MailPoetTest {
expect($task->getSubscribers())->equals([$subscriber->id]);
// task should be deleted
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
$scheduler->process();
$task = SendingTask::getByNewsletterId($newsletter->id);
expect($task)->false();
@ -703,7 +713,7 @@ class SchedulerTest extends \MailPoetTest {
expect($task->newsletter_id)->equals($newsletter->id);
// task should have its status set to null (i.e., sending)
$scheduler = new Scheduler(new SubscribersFinder(), $this->logger_factory);
$scheduler = new Scheduler(new SubscribersFinder(), $this->logger_factory, $this->cron_helper);
$scheduler->process();
$task = SendingTask::getByNewsletterId($newsletter->id);
expect($task->status)->null();
@ -720,7 +730,7 @@ class SchedulerTest extends \MailPoetTest {
$queue->scheduled_at = Carbon::createFromTimestamp(current_time('timestamp'));
$queue->updated_at = $originalUpdated;
$queue->save();
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory);
$scheduler = new Scheduler($this->makeEmpty(SubscribersFinder::class), $this->logger_factory, $this->cron_helper);
$scheduler->timer = microtime(true);
$scheduler->process();
$newQueue = ScheduledTask::findOne($queue->task_id);

View File

@ -7,11 +7,13 @@ use Codeception\Stub;
use Codeception\Stub\Expected;
use Codeception\Util\Fixtures;
use MailPoet\Config\Populator;
use MailPoet\Cron\CronHelper;
use MailPoet\Cron\Workers\SendingQueue\SendingErrorHandler;
use MailPoet\Cron\Workers\SendingQueue\SendingQueue as SendingQueueWorker;
use MailPoet\Cron\Workers\SendingQueue\Tasks\Mailer as MailerTask;
use MailPoet\Cron\Workers\SendingQueue\Tasks\Newsletter as NewsletterTask;
use MailPoet\Cron\Workers\StatsNotifications\Scheduler as StatsNotificationsScheduler;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Entities\NewsletterEntity;
use MailPoet\Features\FeaturesController;
use MailPoet\Logging\LoggerFactory;
@ -100,11 +102,13 @@ class SendingQueueTest extends \MailPoetTest {
$this->sending_error_handler = new SendingErrorHandler();
$this->stats_notifications_worker = Stub::makeEmpty(StatsNotificationsScheduler::class);
$this->logger_factory = LoggerFactory::getInstance();
$this->cron_helper = ContainerWrapper::getInstance()->get(CronHelper::class);
$this->sending_queue_worker = new SendingQueueWorker(
$this->sending_error_handler,
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()])
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cron_helper
);
}
@ -141,6 +145,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cron_helper,
$timer
);
expect($sending_queue_worker->timer)->equals($timer);
@ -152,7 +157,8 @@ class SendingQueueTest extends \MailPoetTest {
$this->sending_error_handler,
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class)
Stub::makeEmpty(NewslettersRepository::class),
$this->cron_helper
),
[
'processQueue' => Expected::never(),
@ -160,7 +166,7 @@ class SendingQueueTest extends \MailPoetTest {
throw new \Exception();
}),
], $this);
$sending_queue_worker->__construct($this->sending_error_handler, $this->stats_notifications_worker, $this->logger_factory, Stub::makeEmpty(NewslettersRepository::class));
$sending_queue_worker->__construct($this->sending_error_handler, $this->stats_notifications_worker, $this->logger_factory, Stub::makeEmpty(NewslettersRepository::class), $this->cron_helper);
try {
$sending_queue_worker->process();
self::fail('Execution limits function was not called.');
@ -171,7 +177,7 @@ class SendingQueueTest extends \MailPoetTest {
function testItEnforcesExecutionLimitsAfterSendingWhenQueueStatusIsNotSetToComplete() {
$sending_queue_worker = Stub::make(
new SendingQueueWorker($this->sending_error_handler, $this->stats_notifications_worker, $this->logger_factory, Stub::makeEmpty(NewslettersRepository::class)),
new SendingQueueWorker($this->sending_error_handler, $this->stats_notifications_worker, $this->logger_factory, Stub::makeEmpty(NewslettersRepository::class), $this->cron_helper),
[
'enforceSendingAndExecutionLimits' => Expected::exactly(1),
], $this);
@ -180,6 +186,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -208,7 +215,7 @@ class SendingQueueTest extends \MailPoetTest {
$queue = $this->queue;
$queue->status = SendingQueue::STATUS_COMPLETED;
$sending_queue_worker = Stub::make(
new SendingQueueWorker($this->sending_error_handler, $this->stats_notifications_worker, $this->logger_factory, Stub::makeEmpty(NewslettersRepository::class)),
new SendingQueueWorker($this->sending_error_handler, $this->stats_notifications_worker, $this->logger_factory, Stub::makeEmpty(NewslettersRepository::class), $this->cron_helper),
[
'enforceSendingAndExecutionLimits' => Expected::never(),
], $this);
@ -217,6 +224,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -240,7 +248,7 @@ class SendingQueueTest extends \MailPoetTest {
function testItEnforcesExecutionLimitsAfterQueueProcessing() {
$sending_queue_worker = Stub::make(
new SendingQueueWorker($this->sending_error_handler, $this->stats_notifications_worker, $this->logger_factory, Stub::makeEmpty(NewslettersRepository::class)),
new SendingQueueWorker($this->sending_error_handler, $this->stats_notifications_worker, $this->logger_factory, Stub::makeEmpty(NewslettersRepository::class), $this->cron_helper),
[
'processQueue' => function() {
// this function returns a queue object
@ -248,7 +256,7 @@ class SendingQueueTest extends \MailPoetTest {
},
'enforceSendingAndExecutionLimits' => Expected::exactly(2),
], $this);
$sending_queue_worker->__construct($this->sending_error_handler, $this->stats_notifications_worker, $this->logger_factory, Stub::makeEmpty(NewslettersRepository::class));
$sending_queue_worker->__construct($this->sending_error_handler, $this->stats_notifications_worker, $this->logger_factory, Stub::makeEmpty(NewslettersRepository::class), $this->cron_helper);
$sending_queue_worker->process();
}
@ -275,6 +283,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -305,6 +314,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -333,6 +343,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -380,6 +391,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -430,6 +442,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -487,6 +500,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cron_helper,
$timer = false,
Stub::makeEmpty(new MailerTask(), [], $this)
);
@ -505,6 +519,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -557,6 +572,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -723,13 +739,15 @@ class SendingQueueTest extends \MailPoetTest {
$this->sending_error_handler,
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class)
Stub::makeEmpty(NewslettersRepository::class),
$this->cron_helper
));
$sending_queue_worker->__construct(
$this->sending_error_handler,
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -766,6 +784,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class, ['findOneById' => new NewsletterEntity()]),
$this->cron_helper,
$timer = false,
Stub::make(
new MailerTask(),
@ -794,7 +813,8 @@ class SendingQueueTest extends \MailPoetTest {
$this->sending_error_handler,
$this->stats_notifications_worker,
$this->logger_factory,
Stub::makeEmpty(NewslettersRepository::class)
Stub::makeEmpty(NewslettersRepository::class),
$this->cron_helper
);
expect($sending_queue_worker->batch_size)->equals($custom_batch_size_value);
$wp->removeFilter('mailpoet_cron_worker_sending_queue_batch_size', $filter);

View File

@ -7,6 +7,7 @@ use Codeception\Stub;
use Codeception\Stub\Expected;
use MailPoet\Cron\CronHelper;
use MailPoet\Cron\Workers\SimpleWorkerMockImplementation as MockSimpleWorker;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Models\ScheduledTask;
use MailPoet\Models\Setting;
@ -15,6 +16,7 @@ require_once('SimpleWorkerMockImplementation.php');
class SimpleWorkerTest extends \MailPoetTest {
function _before() {
parent::_before();
$this->cron_helper = ContainerWrapper::getInstance()->get(CronHelper::class);
$this->worker = new MockSimpleWorker();
}
@ -40,7 +42,7 @@ class SimpleWorkerTest extends \MailPoetTest {
function testItThrowsExceptionWhenExecutionLimitIsReached() {
try {
new MockSimpleWorker(
microtime(true) - CronHelper::getDaemonExecutionLimit()
microtime(true) - $this->cron_helper->getDaemonExecutionLimit()
);
self::fail('Maximum execution time limit exception was not thrown.');
} catch (\Exception $e) {
@ -213,7 +215,7 @@ class SimpleWorkerTest extends \MailPoetTest {
[],
[
'processTaskStrategy' => function () {
CronHelper::enforceExecutionLimit(microtime(true) - CronHelper::DAEMON_EXECUTION_LIMIT - 1);
$this->cron_helper->enforceExecutionLimit(microtime(true) - CronHelper::DAEMON_EXECUTION_LIMIT - 1);
},
],
$this

View File

@ -3,6 +3,7 @@
namespace MailPoet\Cron\Workers\StatsNotifications;
use MailPoet\Config\Renderer;
use MailPoet\Cron\CronHelper;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Entities\StatsNotificationEntity;
use MailPoet\Mailer\Mailer;
@ -16,7 +17,6 @@ use MailPoet\Models\StatisticsOpens;
use MailPoet\Models\StatisticsUnsubscribes;
use MailPoet\Newsletter\Statistics\NewsletterStatisticsRepository;
use MailPoet\Settings\SettingsController;
use MailPoet\WooCommerce\Helper as WCHelper;
use PHPUnit\Framework\MockObject\MockObject;
class WorkerTest extends \MailPoetTest {
@ -33,6 +33,9 @@ class WorkerTest extends \MailPoetTest {
/** @var SettingsController */
private $settings;
/** @var CronHelper */
private $cron_helper;
/** @var Newsletter */
private $newsletter;
@ -57,10 +60,12 @@ class WorkerTest extends \MailPoetTest {
$this->mailer = $this->createMock(Mailer::class);
$this->renderer = $this->createMock(Renderer::class);
$this->settings = new SettingsController();
$this->cron_helper = ContainerWrapper::getInstance()->get(CronHelper::class);
$this->stats_notifications = new Worker(
$this->mailer,
$this->renderer,
$this->settings,
$this->cron_helper,
new MetaInfo,
$this->repository,
$this->newsletter_link_repository,