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