ActionScheduler
2 months ago
CliCommands
1 month ago
Triggers
2 months ago
Workers
2 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
2 days ago
DaemonActionSchedulerRunner.php
6 months ago
DaemonHttpRunner.php
2 months ago
Supervisor.php
3 years ago
index.php
3 years ago
CronTrigger.php
79 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Cron; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Cron\Triggers\WordPress; |
| 9 | use MailPoet\Settings\SettingsController; |
| 10 | use MailPoet\Util\Helpers; |
| 11 | |
| 12 | class CronTrigger { |
| 13 | const METHOD_LINUX_CRON = 'Linux Cron'; |
| 14 | const METHOD_WORDPRESS = 'WordPress'; |
| 15 | const METHOD_ACTION_SCHEDULER = 'Action Scheduler'; |
| 16 | |
| 17 | const METHODS = [ |
| 18 | 'wordpress' => self::METHOD_WORDPRESS, |
| 19 | 'linux_cron' => self::METHOD_LINUX_CRON, |
| 20 | 'action_scheduler' => self::METHOD_ACTION_SCHEDULER, |
| 21 | 'none' => 'Disabled', |
| 22 | ]; |
| 23 | |
| 24 | const DEFAULT_METHOD = self::METHOD_ACTION_SCHEDULER; |
| 25 | const SETTING_NAME = 'cron_trigger'; |
| 26 | const SETTING_CURRENT_METHOD = self::SETTING_NAME . '.method'; |
| 27 | |
| 28 | /** @var WordPress */ |
| 29 | private $wordpressTrigger; |
| 30 | |
| 31 | /** @var SettingsController */ |
| 32 | private $settings; |
| 33 | |
| 34 | /** @var DaemonActionSchedulerRunner */ |
| 35 | private $cronActionScheduler; |
| 36 | |
| 37 | public function __construct( |
| 38 | WordPress $wordpressTrigger, |
| 39 | SettingsController $settings, |
| 40 | DaemonActionSchedulerRunner $cronActionScheduler |
| 41 | ) { |
| 42 | $this->wordpressTrigger = $wordpressTrigger; |
| 43 | $this->settings = $settings; |
| 44 | $this->cronActionScheduler = $cronActionScheduler; |
| 45 | } |
| 46 | |
| 47 | public function init(string $environment = '') { |
| 48 | $currentMethod = $this->settings->get(self::SETTING_CURRENT_METHOD); |
| 49 | try { |
| 50 | $this->cronActionScheduler->init($currentMethod === self::METHOD_ACTION_SCHEDULER); |
| 51 | // setup WordPress cron method trigger only outside of cli environment |
| 52 | if ($currentMethod === self::METHOD_WORDPRESS && $environment !== 'cli') { |
| 53 | return $this->wordpressTrigger->run(); |
| 54 | } |
| 55 | return false; |
| 56 | } catch (\Exception $e) { |
| 57 | // cron exceptions should not prevent the rest of the site from loading |
| 58 | Helpers::mySqlGoneAwayExceptionHandler($e); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public function disable() { |
| 63 | $currentMethod = $this->settings->get(self::SETTING_CURRENT_METHOD); |
| 64 | try { |
| 65 | if ($currentMethod === self::METHOD_ACTION_SCHEDULER) { |
| 66 | // deactivate the action Scheduler |
| 67 | $this->cronActionScheduler->deactivate(); |
| 68 | } |
| 69 | |
| 70 | if ($currentMethod === self::METHOD_WORDPRESS) { |
| 71 | // deactivate WordPress cron method trigger |
| 72 | $this->wordpressTrigger->stop(); |
| 73 | } |
| 74 | } catch (\Exception $e) { |
| 75 | // cron exceptions should not prevent the rest of the site from loading |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 |