From 2b44684c38c075d94f53f8d81afd6fa03fc214a9 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Wed, 18 May 2022 13:15:20 +0200 Subject: [PATCH] Add action scheduler method to cron trigger [MAILPOET-4274] --- mailpoet/lib/Cron/CronTrigger.php | 13 ++++++++++++- mailpoet/tests/unit/Cron/CronTriggerTest.php | 10 +++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/mailpoet/lib/Cron/CronTrigger.php b/mailpoet/lib/Cron/CronTrigger.php index 25ee520605..a79ebe6e56 100644 --- a/mailpoet/lib/Cron/CronTrigger.php +++ b/mailpoet/lib/Cron/CronTrigger.php @@ -8,10 +8,12 @@ use MailPoet\Settings\SettingsController; class CronTrigger { const METHOD_LINUX_CRON = 'Linux Cron'; const METHOD_WORDPRESS = 'WordPress'; + const METHOD_ACTION_SCHEDULER = 'Action Scheduler'; const METHODS = [ 'wordpress' => self::METHOD_WORDPRESS, 'linux_cron' => self::METHOD_LINUX_CRON, + 'action_scheduler' => self::METHOD_ACTION_SCHEDULER, 'none' => 'Disabled', ]; @@ -24,12 +26,17 @@ class CronTrigger { /** @var SettingsController */ private $settings; + /** @var DaemonActionSchedulerRunner */ + private $cronActionScheduler; + public function __construct( WordPress $wordpressTrigger, - SettingsController $settings + SettingsController $settings, + DaemonActionSchedulerRunner $cronActionScheduler ) { $this->wordpressTrigger = $wordpressTrigger; $this->settings = $settings; + $this->cronActionScheduler = $cronActionScheduler; } public function init() { @@ -37,6 +44,10 @@ class CronTrigger { try { if ($currentMethod === self::METHOD_WORDPRESS) { return $this->wordpressTrigger->run(); + } elseif ($currentMethod === self::METHOD_ACTION_SCHEDULER) { + // register Action Scheduler + require_once __DIR__ . '/../../vendor/woocommerce/action-scheduler/action-scheduler.php'; + $this->cronActionScheduler->init(); } return false; } catch (\Exception $e) { diff --git a/mailpoet/tests/unit/Cron/CronTriggerTest.php b/mailpoet/tests/unit/Cron/CronTriggerTest.php index 12ea3fe87a..3b58358d74 100644 --- a/mailpoet/tests/unit/Cron/CronTriggerTest.php +++ b/mailpoet/tests/unit/Cron/CronTriggerTest.php @@ -4,7 +4,7 @@ namespace MailPoet\Test\Cron; use Codeception\Stub; use MailPoet\Cron\CronTrigger; -use MailPoet\Cron\Triggers\MailPoet; +use MailPoet\Cron\DaemonActionSchedulerRunner; use MailPoet\Cron\Triggers\WordPress; use MailPoet\Settings\SettingsController; @@ -12,9 +12,11 @@ class CronTriggerTest extends \MailPoetUnitTest { public function testItDefinesConstants() { expect(CronTrigger::METHOD_LINUX_CRON)->same('Linux Cron'); expect(CronTrigger::METHOD_WORDPRESS)->same('WordPress'); + expect(CronTrigger::METHOD_ACTION_SCHEDULER)->same('Action Scheduler'); expect(CronTrigger::METHODS)->equals([ 'wordpress' => 'WordPress', 'linux_cron' => 'Linux Cron', + 'action_scheduler' => 'Action Scheduler', 'none' => 'Disabled', ]); expect(CronTrigger::DEFAULT_METHOD)->equals('WordPress'); @@ -52,9 +54,11 @@ class CronTriggerTest extends \MailPoetUnitTest { private function createCronTrigger( SettingsController $settings, - WordPress $wordpressTrigger = null + WordPress $wordpressTrigger = null, + DaemonActionSchedulerRunner $actionSchedulerRunner = null ) { $wordpressTrigger = $wordpressTrigger ?: $this->make(WordPress::class, ['run' => true]); - return new CronTrigger($wordpressTrigger, $settings); + $actionSchedulerRunner = $actionSchedulerRunner ?: $this->make(DaemonActionSchedulerRunner::class, ['init' => true]); + return new CronTrigger($wordpressTrigger, $settings, $actionSchedulerRunner); } }