Deactivate action scheduler actions when another method is active

[MAILPOET-4274]
This commit is contained in:
Rostislav Wolny
2022-05-24 13:18:51 +02:00
committed by Veljko V
parent 138354cfd8
commit b525f9f876
4 changed files with 17 additions and 11 deletions

View File

@@ -7,7 +7,6 @@ use MailPoet\AutomaticEmails\AutomaticEmails;
use MailPoet\Automation\Engine\Engine; use MailPoet\Automation\Engine\Engine;
use MailPoet\Automation\Engine\Hooks as AutomationHooks; use MailPoet\Automation\Engine\Hooks as AutomationHooks;
use MailPoet\Automation\Integrations\MailPoet\MailPoetIntegration; use MailPoet\Automation\Integrations\MailPoet\MailPoetIntegration;
use MailPoet\Cron\ActionScheduler\ActionScheduler;
use MailPoet\Cron\CronTrigger; use MailPoet\Cron\CronTrigger;
use MailPoet\Cron\DaemonActionSchedulerRunner; use MailPoet\Cron\DaemonActionSchedulerRunner;
use MailPoet\Features\FeaturesController; use MailPoet\Features\FeaturesController;
@@ -107,8 +106,8 @@ class Initializer {
/** @var PersonalDataExporters */ /** @var PersonalDataExporters */
private $personalDataExporters; private $personalDataExporters;
/** @var ActionScheduler */ /** @var DaemonActionSchedulerRunner */
private $actionScheduler; private $actionSchedulerRunner;
const INITIALIZED = 'MAILPOET_INITIALIZED'; const INITIALIZED = 'MAILPOET_INITIALIZED';
@@ -139,7 +138,7 @@ class Initializer {
MailPoetIntegration $automationMailPoetIntegration, MailPoetIntegration $automationMailPoetIntegration,
FeaturesController $featuresController, FeaturesController $featuresController,
PersonalDataExporters $personalDataExporters, PersonalDataExporters $personalDataExporters,
ActionScheduler $actionScheduler DaemonActionSchedulerRunner $actionSchedulerRunner
) { ) {
$this->rendererFactory = $rendererFactory; $this->rendererFactory = $rendererFactory;
$this->accessControl = $accessControl; $this->accessControl = $accessControl;
@@ -167,7 +166,7 @@ class Initializer {
$this->automationMailPoetIntegration = $automationMailPoetIntegration; $this->automationMailPoetIntegration = $automationMailPoetIntegration;
$this->featuresController = $featuresController; $this->featuresController = $featuresController;
$this->personalDataExporters = $personalDataExporters; $this->personalDataExporters = $personalDataExporters;
$this->actionScheduler = $actionScheduler; $this->actionSchedulerRunner = $actionSchedulerRunner;
} }
public function init() { public function init() {
@@ -456,9 +455,7 @@ class Initializer {
} }
public function runDeactivation() { public function runDeactivation() {
// Unschedule recurring action scheduler actions $this->actionSchedulerRunner->deactivate();
$this->actionScheduler->unscheduleAction(DaemonActionSchedulerRunner::DAEMON_RUN_SCHEDULER_ACTION);
$this->actionScheduler->unscheduleAction(DaemonActionSchedulerRunner::DAEMON_TRIGGER_SCHEDULER_ACTION);
} }
private function setupWoocommerceTransactionalEmails() { private function setupWoocommerceTransactionalEmails() {

View File

@@ -42,10 +42,14 @@ class CronTrigger {
public function init() { public function init() {
$currentMethod = $this->settings->get(self::SETTING_NAME . '.method'); $currentMethod = $this->settings->get(self::SETTING_NAME . '.method');
try { try {
if ($currentMethod === self::METHOD_ACTION_SCHEDULER) {
$this->cronActionScheduler->init();
return true;
} else {
$this->cronActionScheduler->deactivate();
}
if ($currentMethod === self::METHOD_WORDPRESS) { if ($currentMethod === self::METHOD_WORDPRESS) {
return $this->wordpressTrigger->run(); return $this->wordpressTrigger->run();
} elseif ($currentMethod === self::METHOD_ACTION_SCHEDULER) {
$this->cronActionScheduler->init();
} }
return false; return false;
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@@ -49,6 +49,11 @@ class DaemonActionSchedulerRunner {
} }
} }
public function deactivate() {
$this->actionScheduler->unscheduleAction(self::DAEMON_TRIGGER_SCHEDULER_ACTION);
$this->actionScheduler->unscheduleAction(self::DAEMON_RUN_SCHEDULER_ACTION);
}
/** /**
* In regular intervals checks if there are scheduled tasks to execute. * In regular intervals checks if there are scheduled tasks to execute.
* In case there are tasks it spawns a recurring action. * In case there are tasks it spawns a recurring action.

View File

@@ -58,7 +58,7 @@ class CronTriggerTest extends \MailPoetUnitTest {
DaemonActionSchedulerRunner $actionSchedulerRunner = null DaemonActionSchedulerRunner $actionSchedulerRunner = null
) { ) {
$wordpressTrigger = $wordpressTrigger ?: $this->make(WordPress::class, ['run' => true]); $wordpressTrigger = $wordpressTrigger ?: $this->make(WordPress::class, ['run' => true]);
$actionSchedulerRunner = $actionSchedulerRunner ?: $this->make(DaemonActionSchedulerRunner::class, ['init' => true]); $actionSchedulerRunner = $actionSchedulerRunner ?: $this->make(DaemonActionSchedulerRunner::class, ['init' => true, 'deactivate' => true]);
return new CronTrigger($wordpressTrigger, $settings, $actionSchedulerRunner); return new CronTrigger($wordpressTrigger, $settings, $actionSchedulerRunner);
} }
} }