ActionScheduler
2 months ago
CliCommands
1 month ago
Triggers
2 months ago
Workers
3 days ago
CronHelper.php
2 months ago
CronTrigger.php
2 years ago
CronWorkerInterface.php
3 years ago
CronWorkerRunner.php
2 months ago
CronWorkerScheduler.php
10 months ago
Daemon.php
3 days ago
DaemonActionSchedulerRunner.php
6 months ago
DaemonHttpRunner.php
2 months ago
Supervisor.php
3 years ago
index.php
3 years ago
DaemonActionSchedulerRunner.php
84 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 | /** @var ActionScheduler */ |
| 18 | private $actionScheduler; |
| 19 | |
| 20 | /** @var RemoteExecutorHandler */ |
| 21 | private $remoteExecutorHandler; |
| 22 | |
| 23 | /** @var DaemonTrigger */ |
| 24 | private $daemonTriggerAction; |
| 25 | |
| 26 | /** @var DaemonRun */ |
| 27 | private $daemonRunAction; |
| 28 | |
| 29 | /** @var WPFunctions */ |
| 30 | private $wp; |
| 31 | |
| 32 | public function __construct( |
| 33 | ActionScheduler $actionScheduler, |
| 34 | RemoteExecutorHandler $remoteExecutorHandler, |
| 35 | DaemonTrigger $daemonTriggerAction, |
| 36 | DaemonRun $daemonRunAction, |
| 37 | WPFunctions $wp |
| 38 | ) { |
| 39 | $this->actionScheduler = $actionScheduler; |
| 40 | $this->remoteExecutorHandler = $remoteExecutorHandler; |
| 41 | $this->daemonTriggerAction = $daemonTriggerAction; |
| 42 | $this->daemonRunAction = $daemonRunAction; |
| 43 | $this->wp = $wp; |
| 44 | } |
| 45 | |
| 46 | public function init(bool $isActive = true): void { |
| 47 | if (!$isActive) { |
| 48 | $this->deactivateOnTrigger(); |
| 49 | return; |
| 50 | } |
| 51 | $this->daemonRunAction->init(); |
| 52 | $this->daemonTriggerAction->init(); |
| 53 | $this->remoteExecutorHandler->init(); |
| 54 | } |
| 55 | |
| 56 | public function deactivate(): void { |
| 57 | // Set flag BEFORE unscheduling to prevent race condition with parallel requests |
| 58 | $this->wp->updateOption(self::DEACTIVATION_FLAG_OPTION, true); |
| 59 | $this->actionScheduler->unscheduleAllCronActions(); |
| 60 | } |
| 61 | |
| 62 | public function clearDeactivationFlag(): void { |
| 63 | $this->wp->deleteOption(self::DEACTIVATION_FLAG_OPTION); |
| 64 | } |
| 65 | |
| 66 | public function isDeactivating(): bool { |
| 67 | return (bool)$this->wp->getOption(self::DEACTIVATION_FLAG_OPTION, false); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Unschedule all MailPoet actions when next "trigger" action is processed. |
| 72 | * Note: We can't unschedule the actions directly inside the trigger action itself, |
| 73 | * because the action is recurring and would reschedule itself anyway. |
| 74 | * We need do the deactivation after the action scheduler process finishes. |
| 75 | */ |
| 76 | private function deactivateOnTrigger(): void { |
| 77 | $this->wp->addAction(DaemonTrigger::NAME, [$this, 'deactivateAfterProcess']); |
| 78 | } |
| 79 | |
| 80 | public function deactivateAfterProcess(): void { |
| 81 | $this->wp->addAction('action_scheduler_after_process_queue', [$this, 'deactivate']); |
| 82 | } |
| 83 | } |
| 84 |