diff --git a/mailpoet/lib/Cron/ActionScheduler/ActionScheduler.php b/mailpoet/lib/Cron/ActionScheduler/ActionScheduler.php index a99abaa342..73742135db 100644 --- a/mailpoet/lib/Cron/ActionScheduler/ActionScheduler.php +++ b/mailpoet/lib/Cron/ActionScheduler/ActionScheduler.php @@ -2,13 +2,28 @@ namespace MailPoet\Cron\ActionScheduler; +use MailPoet\WP\Functions as WPFunctions; + class ActionScheduler { public const GROUP_ID = 'mailpoet-cron'; + /** @var WPFunctions */ + private $wp; + + public function __construct( + WPFunctions $wp + ) { + $this->wp = $wp; + } + public function scheduleRecurringAction(int $timestamp, int $interval_in_seconds, string $hook, array $args = [], bool $unique = true): int { return as_schedule_recurring_action($timestamp, $interval_in_seconds, $hook, $args, self::GROUP_ID, $unique); } + public function scheduleImmediateSingleAction(string $hook, array $args = [], bool $unique = true): int { + return as_schedule_single_action($this->wp->currentTime('timestamp', true), $hook, $args, self::GROUP_ID, $unique); + } + public function unscheduleAction(string $hook, array $args = []): ?int { $id = as_unschedule_action($hook, $args, self::GROUP_ID); return $id !== null ? intval($id) : null; diff --git a/mailpoet/lib/Cron/ActionScheduler/Actions/DaemonRun.php b/mailpoet/lib/Cron/ActionScheduler/Actions/DaemonRun.php index 8c15157cfb..d51f8c5fd3 100644 --- a/mailpoet/lib/Cron/ActionScheduler/Actions/DaemonRun.php +++ b/mailpoet/lib/Cron/ActionScheduler/Actions/DaemonRun.php @@ -79,12 +79,11 @@ class DaemonRun { */ public function afterProcess(): void { if ($this->wordpressTrigger->checkExecutionRequirements()) { + $this->actionScheduler->scheduleImmediateSingleAction(self::NAME); // The automatic rescheduling schedules the next recurring action to run after 1 second. // So we need to wait before we trigger new remote executor to avoid skipping the action sleep(2); $this->remoteExecutorHandler->triggerExecutor(); - } else { - $this->actionScheduler->unscheduleAction(self::NAME); } } diff --git a/mailpoet/lib/Cron/ActionScheduler/Actions/DaemonTrigger.php b/mailpoet/lib/Cron/ActionScheduler/Actions/DaemonTrigger.php index 924b9fdbbc..670c8be7ea 100644 --- a/mailpoet/lib/Cron/ActionScheduler/Actions/DaemonTrigger.php +++ b/mailpoet/lib/Cron/ActionScheduler/Actions/DaemonTrigger.php @@ -55,8 +55,8 @@ class DaemonTrigger { if ($this->actionScheduler->hasScheduledAction(DaemonRun::NAME)) { return; } - // Start recurring action with minimal interval to ensure continuous execution of the daemon - $this->actionScheduler->scheduleRecurringAction($this->wp->currentTime('timestamp', true) - 1, 1, DaemonRun::NAME); + // Schedule immediate action for execution of the daemon + $this->actionScheduler->scheduleImmediateSingleAction(DaemonRun::NAME); $this->remoteExecutorHandler->triggerExecutor(); } } diff --git a/mailpoet/tasks/phpstan/phpstan.neon b/mailpoet/tasks/phpstan/phpstan.neon index 53d2380145..401fd26f88 100644 --- a/mailpoet/tasks/phpstan/phpstan.neon +++ b/mailpoet/tasks/phpstan/phpstan.neon @@ -68,6 +68,10 @@ parameters: message: '/^Function as_schedule_recurring_action invoked with 6 parameters, 3-5 required.$/' count: 1 path: ../../lib/Cron/ActionScheduler/ActionScheduler.php + - # WooCommerce stubs contains stubs for older ActionScheduler version + message: '/^Function as_schedule_single_action invoked with 5 parameters, 2-4 required.$/' + count: 1 + path: ../../lib/Cron/ActionScheduler/ActionScheduler.php reportUnmatchedIgnoredErrors: true dynamicConstantNames: - MAILPOET_PREMIUM_INITIALIZED diff --git a/mailpoet/tests/integration/Cron/ActionScheduler/Actions/DaemonRunTest.php b/mailpoet/tests/integration/Cron/ActionScheduler/Actions/DaemonRunTest.php index 8b3e3e82c3..fb125a2d6a 100644 --- a/mailpoet/tests/integration/Cron/ActionScheduler/Actions/DaemonRunTest.php +++ b/mailpoet/tests/integration/Cron/ActionScheduler/Actions/DaemonRunTest.php @@ -41,7 +41,7 @@ class DaemonRunTest extends \MailPoetTest { $this->daemonRun->init(); expect($this->daemonRun->getDaemonExecutionLimit())->equals(20); // Verify initial execution limit - $this->actionScheduler->scheduleRecurringAction(time() - 1, 100, DaemonRun::NAME); + $this->actionScheduler->scheduleImmediateSingleAction(DaemonRun::NAME); $actions = $this->actionSchedulerHelper->getMailPoetScheduledActions(); expect($actions)->count(1); $doneActions = $this->actionSchedulerHelper->getMailPoetCompleteActions(); diff --git a/mailpoet/tests/integration/Cron/DaemonActionSchedulerRunnerTest.php b/mailpoet/tests/integration/Cron/DaemonActionSchedulerRunnerTest.php index df5ebc6f71..6b32248f2f 100644 --- a/mailpoet/tests/integration/Cron/DaemonActionSchedulerRunnerTest.php +++ b/mailpoet/tests/integration/Cron/DaemonActionSchedulerRunnerTest.php @@ -50,7 +50,7 @@ class DaemonActionSchedulerRunnerTest extends \MailPoetTest { public function testItDeactivatesAllTasksOnTrigger(): void { $this->actionScheduler->scheduleRecurringAction(time() - 1, 100, DaemonTrigger::NAME); - $this->actionScheduler->scheduleRecurringAction(time() - 1, 100, DaemonRun::NAME); + $this->actionScheduler->scheduleImmediateSingleAction(DaemonRun::NAME); $actions = $this->actionSchedulerHelper->getMailPoetScheduledActions(); expect($actions)->count(2); $this->actionSchedulerRunner->init(false);