Use CronHelper service in cron workers [MAILPOET-2459]
This commit is contained in:
@@ -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)
|
||||
|
@@ -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;
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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,
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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)) {
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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();
|
||||
|
@@ -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,
|
||||
|
Reference in New Issue
Block a user