diff --git a/lib/API/Endpoints/Cron.php b/lib/API/Endpoints/Cron.php index 58039aad16..7a9dde6886 100644 --- a/lib/API/Endpoints/Cron.php +++ b/lib/API/Endpoints/Cron.php @@ -28,14 +28,9 @@ class Cron { } function getStatus() { - $task_scheduler = CronTrigger::getCurrentMethod(); $daemon = Setting::getValue(CronHelper::DAEMON_SETTING); - if($daemon) { - return $daemon; - } - $status = ($task_scheduler === CronTrigger::METHOD_WORDPRESS) ? - 'wordpress_task_scheduler_enabled' : - false; - return array('status' => $status); + return ($daemon) ? + $daemon : + array('status' => false); } } \ No newline at end of file diff --git a/lib/Config/Initializer.php b/lib/Config/Initializer.php index 9dacf23c58..01b47b9ba9 100644 --- a/lib/Config/Initializer.php +++ b/lib/Config/Initializer.php @@ -193,8 +193,8 @@ class Initializer { } function setupCronTrigger() { - $task_scheduler = new CronTrigger(); - $task_scheduler->init(); + $cron_trigger = new CronTrigger(); + $cron_trigger->init(); } function setupImages() { diff --git a/lib/Config/Populator.php b/lib/Config/Populator.php index 537c674632..1670114aa1 100644 --- a/lib/Config/Populator.php +++ b/lib/Config/Populator.php @@ -78,7 +78,7 @@ class Populator { if(!Setting::getValue(CronTrigger::SETTING_NAME)) { // disable task scheduler (cron) be default Setting::setValue(CronTrigger::SETTING_NAME, array( - 'method' => CronTrigger::METHOD_WORDPRESS + 'method' => 'WordPress' )); } diff --git a/lib/Cron/CronTrigger.php b/lib/Cron/CronTrigger.php index 75afc47167..c87ceaf990 100644 --- a/lib/Cron/CronTrigger.php +++ b/lib/Cron/CronTrigger.php @@ -1,70 +1,43 @@ 'MailPoet', + 'wordpress' => 'WordPress' + ); const SETTING_NAME = 'cron_trigger'; function __construct() { - $this->method = self::getCurrentMethod(); + $this->current_method = self::getCurrentMethod(); + if(!in_array($this->current_method, self::$available_methods)) { + throw new \Exception(__('Task scheduler is not configured')); + } } function init() { try { - // configure task scheduler only outside of cli environment + // configure cron trigger only outside of cli environment if(php_sapi_name() === 'cli') return; - switch($this->method) { - case self::METHOD_MAILPOET: - return $this->configureMailpoetTrigger(); - case self::METHOD_WORDPRESS: - return $this->configureWordpressTrigger(); - default: - throw new \Exception(__('Task scheduler is not configured')); - }; + $trigger_class = __NAMESPACE__ . '\Triggers\\' . $this->current_method; + return (class_exists($trigger_class)) ? + $trigger_class::run() : + false; } catch(\Exception $e) { - // ignore exceptions as they should not prevent the rest of the site from loading - } - } - - function configureMailpoetTrigger() { - $supervisor = new Supervisor(); - $supervisor->checkDaemon(); - } - - function configureWordpressTrigger() { - $scheduled_queues = SchedulerWorker::getScheduledQueues(); - $running_queues = SendingQueueWorker::getRunningQueues(); - $sending_limit_reached = MailerLog::isSendingLimitReached(); - // run cron only when: - // 1) there are scheduled queues ready to be processed - // 2) queues are already being processed - // 3) sending limit has not been reached - if(($scheduled_queues || $running_queues) && !$sending_limit_reached) { - return $this->configureMailpoetTrigger(); - } - // in all other cases stop (delete) the daemon - $cron_daemon = CronHelper::getDaemon(); - if($cron_daemon) { - CronHelper::deleteDaemon(); + // cron exceptions should not prevent the rest of the site from loading } } static function getAvailableMethods() { - return array( - 'mailpoet' => self::METHOD_MAILPOET, - 'wordpress' => self::METHOD_WORDPRESS - ); + return self::$available_methods; } static function getCurrentMethod() { - return Setting::getValue('cron_trigger.method'); + return Setting::getValue(self::SETTING_NAME . '.method'); } } \ No newline at end of file diff --git a/lib/Cron/Triggers/MailPoet.php b/lib/Cron/Triggers/MailPoet.php new file mode 100644 index 0000000000..ab9d8cf01a --- /dev/null +++ b/lib/Cron/Triggers/MailPoet.php @@ -0,0 +1,13 @@ +checkDaemon(); + } +} \ No newline at end of file diff --git a/lib/Cron/Triggers/WordPress.php b/lib/Cron/Triggers/WordPress.php new file mode 100644 index 0000000000..5885cfa9fc --- /dev/null +++ b/lib/Cron/Triggers/WordPress.php @@ -0,0 +1,31 @@ +timer); } } - Cron::complete(); } function processQueue($queue, $newsletter, $subscribers) { diff --git a/lib/Cron/Workers/SendingQueue/Tasks/Cron.php b/lib/Cron/Workers/SendingQueue/Tasks/Cron.php deleted file mode 100644 index 1b9d19ed58..0000000000 --- a/lib/Cron/Workers/SendingQueue/Tasks/Cron.php +++ /dev/null @@ -1,18 +0,0 @@ -