ActionScheduler
6 days ago
CliCommands
2 months ago
Triggers
3 months ago
Workers
2 weeks ago
CronHelper.php
3 months ago
CronTrigger.php
2 years ago
CronWorkerInterface.php
3 years ago
CronWorkerRunner.php
3 months ago
CronWorkerScheduler.php
11 months ago
Daemon.php
1 month ago
DaemonActionSchedulerRunner.php
6 days ago
DaemonHttpRunner.php
3 months ago
Supervisor.php
3 years ago
index.php
3 years ago
DaemonActionSchedulerRunner.php
99 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Cron\ActionScheduler\Actions\DaemonRun; |
| 9 | use MailPoet\Cron\ActionScheduler\Actions\DaemonTrigger; |
| 10 | use MailPoet\Cron\ActionScheduler\ActionScheduler; |
| 11 | use MailPoet\Cron\ActionScheduler\RemoteExecutorHandler; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | |
| 14 | class DaemonActionSchedulerRunner { |
| 15 | public const DEACTIVATION_FLAG_OPTION = 'mailpoet_cron_deactivating'; |
| 16 | |
| 17 | // The flag guards a short race window while cron actions are being unscheduled. |
| 18 | // An older flag is stale — e.g. left behind by an update flow that died before |
| 19 | // rescheduling — and must not block the daemon trigger forever. |
| 20 | public const DEACTIVATION_FLAG_TTL = 5 * 60; |
| 21 | |
| 22 | /** @var ActionScheduler */ |
| 23 | private $actionScheduler; |
| 24 | |
| 25 | /** @var RemoteExecutorHandler */ |
| 26 | private $remoteExecutorHandler; |
| 27 | |
| 28 | /** @var DaemonTrigger */ |
| 29 | private $daemonTriggerAction; |
| 30 | |
| 31 | /** @var DaemonRun */ |
| 32 | private $daemonRunAction; |
| 33 | |
| 34 | /** @var WPFunctions */ |
| 35 | private $wp; |
| 36 | |
| 37 | public function __construct( |
| 38 | ActionScheduler $actionScheduler, |
| 39 | RemoteExecutorHandler $remoteExecutorHandler, |
| 40 | DaemonTrigger $daemonTriggerAction, |
| 41 | DaemonRun $daemonRunAction, |
| 42 | WPFunctions $wp |
| 43 | ) { |
| 44 | $this->actionScheduler = $actionScheduler; |
| 45 | $this->remoteExecutorHandler = $remoteExecutorHandler; |
| 46 | $this->daemonTriggerAction = $daemonTriggerAction; |
| 47 | $this->daemonRunAction = $daemonRunAction; |
| 48 | $this->wp = $wp; |
| 49 | } |
| 50 | |
| 51 | public function init(bool $isActive = true): void { |
| 52 | if (!$isActive) { |
| 53 | $this->deactivateOnTrigger(); |
| 54 | return; |
| 55 | } |
| 56 | $this->daemonRunAction->init(); |
| 57 | $this->daemonTriggerAction->init(); |
| 58 | $this->remoteExecutorHandler->init(); |
| 59 | } |
| 60 | |
| 61 | public function deactivate(): void { |
| 62 | // Set flag BEFORE unscheduling to prevent race condition with parallel requests |
| 63 | $this->wp->updateOption(self::DEACTIVATION_FLAG_OPTION, $this->wp->currentTime('timestamp', true)); |
| 64 | $this->actionScheduler->unscheduleAllCronActions(); |
| 65 | } |
| 66 | |
| 67 | public function clearDeactivationFlag(): void { |
| 68 | $this->wp->deleteOption(self::DEACTIVATION_FLAG_OPTION); |
| 69 | } |
| 70 | |
| 71 | public function isDeactivating(): bool { |
| 72 | return self::isDeactivationFlagFresh($this->wp); |
| 73 | } |
| 74 | |
| 75 | // Static so DaemonTrigger can use it without a circular dependency. |
| 76 | // Legacy boolean values are treated as stale timestamps and expire immediately. |
| 77 | public static function isDeactivationFlagFresh(WPFunctions $wp): bool { |
| 78 | $flagValue = $wp->getOption(self::DEACTIVATION_FLAG_OPTION, false); |
| 79 | if (!$flagValue) { |
| 80 | return false; |
| 81 | } |
| 82 | return (int)$flagValue > $wp->currentTime('timestamp', true) - self::DEACTIVATION_FLAG_TTL; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Unschedule all MailPoet actions when next "trigger" action is processed. |
| 87 | * Note: We can't unschedule the actions directly inside the trigger action itself, |
| 88 | * because the action is recurring and would reschedule itself anyway. |
| 89 | * We need do the deactivation after the action scheduler process finishes. |
| 90 | */ |
| 91 | private function deactivateOnTrigger(): void { |
| 92 | $this->wp->addAction(DaemonTrigger::NAME, [$this, 'deactivateAfterProcess']); |
| 93 | } |
| 94 | |
| 95 | public function deactivateAfterProcess(): void { |
| 96 | $this->wp->addAction('action_scheduler_after_process_queue', [$this, 'deactivate']); |
| 97 | } |
| 98 | } |
| 99 |