Use DI for cron triggers
[MAILPOET-2538]
This commit is contained in:
committed by
Jack Kitterhing
parent
7a91641d11
commit
c1d5c8068d
@ -2,12 +2,11 @@
|
|||||||
|
|
||||||
namespace MailPoet\Cron;
|
namespace MailPoet\Cron;
|
||||||
|
|
||||||
|
use MailPoet\Cron\Triggers\MailPoet;
|
||||||
|
use MailPoet\Cron\Triggers\WordPress;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
|
|
||||||
class CronTrigger {
|
class CronTrigger {
|
||||||
/** @var SettingsController */
|
|
||||||
private $settings;
|
|
||||||
|
|
||||||
const METHOD_LINUX_CRON = 'Linux Cron';
|
const METHOD_LINUX_CRON = 'Linux Cron';
|
||||||
const METHOD_MAILPOET = 'MailPoet';
|
const METHOD_MAILPOET = 'MailPoet';
|
||||||
const METHOD_WORDPRESS = 'WordPress';
|
const METHOD_WORDPRESS = 'WordPress';
|
||||||
@ -22,17 +21,34 @@ class CronTrigger {
|
|||||||
const DEFAULT_METHOD = 'WordPress';
|
const DEFAULT_METHOD = 'WordPress';
|
||||||
const SETTING_NAME = 'cron_trigger';
|
const SETTING_NAME = 'cron_trigger';
|
||||||
|
|
||||||
function __construct(SettingsController $settings) {
|
/** @var MailPoet */
|
||||||
|
private $mailpoet_trigger;
|
||||||
|
|
||||||
|
/** @var WordPress */
|
||||||
|
private $wordpress_trigger;
|
||||||
|
|
||||||
|
/** @var SettingsController */
|
||||||
|
private $settings;
|
||||||
|
|
||||||
|
function __construct(
|
||||||
|
MailPoet $mailpoet_trigger,
|
||||||
|
WordPress $wordpress_trigger,
|
||||||
|
SettingsController $settings
|
||||||
|
) {
|
||||||
|
$this->mailpoet_trigger = $mailpoet_trigger;
|
||||||
|
$this->wordpress_trigger = $wordpress_trigger;
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
$current_method = $this->settings->get(self::SETTING_NAME . '.method');
|
$current_method = $this->settings->get(self::SETTING_NAME . '.method');
|
||||||
try {
|
try {
|
||||||
$trigger_class = __NAMESPACE__ . '\Triggers\\' . $current_method;
|
if ($current_method === self::METHOD_MAILPOET) {
|
||||||
return (class_exists($trigger_class)) ?
|
return $this->mailpoet_trigger->run();
|
||||||
(new $trigger_class)->run() :
|
} elseif ($current_method === self::METHOD_WORDPRESS) {
|
||||||
false;
|
return $this->wordpress_trigger->run();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
// cron exceptions should not prevent the rest of the site from loading
|
// cron exceptions should not prevent the rest of the site from loading
|
||||||
}
|
}
|
||||||
|
@ -23,13 +23,17 @@ class DaemonHttpRunner {
|
|||||||
|
|
||||||
const PING_SUCCESS_RESPONSE = 'pong';
|
const PING_SUCCESS_RESPONSE = 'pong';
|
||||||
|
|
||||||
function __construct(Daemon $daemon = null, CronHelper $cron_helper, SettingsController $settings) {
|
/** @var WordPress */
|
||||||
|
private $wordpress_trigger;
|
||||||
|
|
||||||
|
function __construct(Daemon $daemon = null, CronHelper $cron_helper, SettingsController $settings, WordPress $wordpress_trigger) {
|
||||||
$this->cron_helper = $cron_helper;
|
$this->cron_helper = $cron_helper;
|
||||||
$this->settings_daemon_data = $this->cron_helper->getDaemon();
|
$this->settings_daemon_data = $this->cron_helper->getDaemon();
|
||||||
$this->token = $this->cron_helper->createToken();
|
$this->token = $this->cron_helper->createToken();
|
||||||
$this->timer = microtime(true);
|
$this->timer = microtime(true);
|
||||||
$this->daemon = $daemon;
|
$this->daemon = $daemon;
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
|
$this->wordpress_trigger = $wordpress_trigger;
|
||||||
}
|
}
|
||||||
|
|
||||||
function ping() {
|
function ping() {
|
||||||
@ -113,11 +117,11 @@ class DaemonHttpRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function checkWPTriggerExecutionRequirements() {
|
function checkWPTriggerExecutionRequirements() {
|
||||||
return WordPress::checkExecutionRequirements();
|
return $this->wordpress_trigger->checkExecutionRequirements();
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopCron() {
|
function stopCron() {
|
||||||
return WordPress::stop();
|
return $this->wordpress_trigger->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,12 +3,17 @@
|
|||||||
namespace MailPoet\Cron\Triggers;
|
namespace MailPoet\Cron\Triggers;
|
||||||
|
|
||||||
use MailPoet\Cron\Supervisor;
|
use MailPoet\Cron\Supervisor;
|
||||||
use MailPoet\DI\ContainerWrapper;
|
|
||||||
|
|
||||||
class MailPoet {
|
class MailPoet {
|
||||||
|
/** @var Supervisor */
|
||||||
|
private $supervisor;
|
||||||
|
|
||||||
|
function __construct(Supervisor $supervisor) {
|
||||||
|
$this->supervisor = $supervisor;
|
||||||
|
}
|
||||||
|
|
||||||
function run() {
|
function run() {
|
||||||
$supervisor = ContainerWrapper::getInstance()->get(Supervisor::class);
|
$this->supervisor->init();
|
||||||
$supervisor->init();
|
return $this->supervisor->checkDaemon();
|
||||||
return $supervisor->checkDaemon();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@ use MailPoet\Cron\Workers\SubscriberLinkTokens;
|
|||||||
use MailPoet\Cron\Workers\UnsubscribeTokens;
|
use MailPoet\Cron\Workers\UnsubscribeTokens;
|
||||||
use MailPoet\Cron\Workers\WooCommercePastOrders;
|
use MailPoet\Cron\Workers\WooCommercePastOrders;
|
||||||
use MailPoet\Cron\Workers\WooCommerceSync as WooCommerceSyncWorker;
|
use MailPoet\Cron\Workers\WooCommerceSync as WooCommerceSyncWorker;
|
||||||
use MailPoet\DI\ContainerWrapper;
|
|
||||||
use MailPoet\Mailer\MailerLog;
|
use MailPoet\Mailer\MailerLog;
|
||||||
use MailPoet\Models\ScheduledTask;
|
use MailPoet\Models\ScheduledTask;
|
||||||
use MailPoet\Services\Bridge;
|
use MailPoet\Services\Bridge;
|
||||||
@ -34,22 +33,45 @@ class WordPress {
|
|||||||
|
|
||||||
private $tasks_counts;
|
private $tasks_counts;
|
||||||
|
|
||||||
|
/** @var CronHelper */
|
||||||
|
private $cron_helper;
|
||||||
|
|
||||||
|
/** @var MailPoet */
|
||||||
|
private $mailpoet_trigger;
|
||||||
|
|
||||||
|
/** @var SettingsController */
|
||||||
|
private $settings;
|
||||||
|
|
||||||
|
/** @var WPFunctions */
|
||||||
|
private $wp;
|
||||||
|
|
||||||
|
function __construct(
|
||||||
|
CronHelper $cron_helper,
|
||||||
|
MailPoet $mailpoet_trigger,
|
||||||
|
SettingsController $settings,
|
||||||
|
WPFunctions $wp
|
||||||
|
) {
|
||||||
|
$this->mailpoet_trigger = $mailpoet_trigger;
|
||||||
|
$this->settings = $settings;
|
||||||
|
$this->wp = $wp;
|
||||||
|
$this->cron_helper = $cron_helper;
|
||||||
|
}
|
||||||
|
|
||||||
function run() {
|
function run() {
|
||||||
if (!$this->checkRunInterval()) {
|
if (!$this->checkRunInterval()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return ($this->checkExecutionRequirements()) ?
|
return ($this->checkExecutionRequirements()) ?
|
||||||
(new MailPoet)->run() :
|
$this->mailpoet_trigger->run() :
|
||||||
self::stop();
|
self::stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkRunInterval() {
|
private function checkRunInterval() {
|
||||||
$settings = SettingsController::getInstance();
|
$last_run_at = (int)$this->settings->get(self::LAST_RUN_AT_SETTING, 0);
|
||||||
$last_run_at = (int)$settings->get(self::LAST_RUN_AT_SETTING, 0);
|
$run_interval = $this->wp->applyFilters('mailpoet_cron_trigger_wordpress_run_interval', self::RUN_INTERVAL);
|
||||||
$run_interval = WPFunctions::get()->applyFilters('mailpoet_cron_trigger_wordpress_run_interval', self::RUN_INTERVAL);
|
|
||||||
$run_interval_elapsed = (time() - $last_run_at) >= $run_interval;
|
$run_interval_elapsed = (time() - $last_run_at) >= $run_interval;
|
||||||
if ($run_interval_elapsed) {
|
if ($run_interval_elapsed) {
|
||||||
$settings->set(self::LAST_RUN_AT_SETTING, time());
|
$this->settings->set(self::LAST_RUN_AT_SETTING, time());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -60,12 +82,11 @@ class WordPress {
|
|||||||
$settings->set(self::LAST_RUN_AT_SETTING, 0);
|
$settings->set(self::LAST_RUN_AT_SETTING, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkExecutionRequirements(WPFunctions $wp = null) {
|
function checkExecutionRequirements() {
|
||||||
$this->loadTasksCounts($wp ?: new WPFunctions);
|
$this->loadTasksCounts();
|
||||||
|
|
||||||
// migration
|
// migration
|
||||||
$settings = SettingsController::getInstance();
|
$migration_disabled = $this->settings->get('cron_trigger.method') === 'none';
|
||||||
$migration_disabled = $settings->get('cron_trigger.method') === 'none';
|
|
||||||
$migration_due_tasks = $this->getTasksCount([
|
$migration_due_tasks = $this->getTasksCount([
|
||||||
'type' => MigrationWorker::TASK_TYPE,
|
'type' => MigrationWorker::TASK_TYPE,
|
||||||
'scheduled_in' => [self::SCHEDULED_IN_THE_PAST],
|
'scheduled_in' => [self::SCHEDULED_IN_THE_PAST],
|
||||||
@ -210,15 +231,14 @@ class WordPress {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function stop() {
|
function stop() {
|
||||||
$cron_helper = ContainerWrapper::getInstance()->get(CronHelper::class);
|
$cron_daemon = $this->cron_helper->getDaemon();
|
||||||
$cron_daemon = $cron_helper->getDaemon();
|
|
||||||
if ($cron_daemon) {
|
if ($cron_daemon) {
|
||||||
$cron_helper->deactivateDaemon($cron_daemon);
|
$this->cron_helper->deactivateDaemon($cron_daemon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function loadTasksCounts(WPFunctions $wp) {
|
private function loadTasksCounts() {
|
||||||
$query = sprintf(
|
$query = sprintf(
|
||||||
"select
|
"select
|
||||||
type,
|
type,
|
||||||
@ -229,7 +249,7 @@ class WordPress {
|
|||||||
where deleted_at is null
|
where deleted_at is null
|
||||||
group by type, status, scheduled_in
|
group by type, status, scheduled_in
|
||||||
",
|
",
|
||||||
date('Y-m-d H:i:s', $wp->currentTime('timestamp')),
|
date('Y-m-d H:i:s', $this->wp->currentTime('timestamp')),
|
||||||
self::SCHEDULED_IN_THE_PAST,
|
self::SCHEDULED_IN_THE_PAST,
|
||||||
self::SCHEDULED_IN_THE_FUTURE,
|
self::SCHEDULED_IN_THE_FUTURE,
|
||||||
ScheduledTask::$_table
|
ScheduledTask::$_table
|
||||||
|
@ -131,6 +131,8 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Cron\Daemon::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\Daemon::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Cron\DaemonHttpRunner::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\DaemonHttpRunner::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Cron\Supervisor::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\Supervisor::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Cron\Triggers\MailPoet::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Cron\Triggers\WordPress::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Cron\Workers\WorkersFactory::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\Workers\WorkersFactory::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Cron\Workers\SendingQueue\SendingErrorHandler::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\Workers\SendingQueue\SendingErrorHandler::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Cron\Workers\StatsNotifications\Scheduler::class);
|
$container->autowire(\MailPoet\Cron\Workers\StatsNotifications\Scheduler::class);
|
||||||
|
Reference in New Issue
Block a user