Refactor daemon trigger and daemon run actions to extra classes
[MAILPOET-4274]
This commit is contained in:
committed by
Veljko V
parent
476ee1ede9
commit
d3e2bcdf34
100
mailpoet/lib/Cron/ActionScheduler/Actions/DaemonRun.php
Normal file
100
mailpoet/lib/Cron/ActionScheduler/Actions/DaemonRun.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Cron\ActionScheduler\Actions;
|
||||
|
||||
use MailPoet\Cron\ActionScheduler\ActionScheduler;
|
||||
use MailPoet\Cron\ActionScheduler\RemoteExecutorHandler;
|
||||
use MailPoet\Cron\CronHelper;
|
||||
use MailPoet\Cron\Daemon;
|
||||
use MailPoet\Cron\Triggers\WordPress;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class DaemonRun {
|
||||
const NAME = 'mailpoet/cron/daemon-run';
|
||||
const EXECUTION_LIMIT_MARGIN = 10; // 10 seconds
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
/** @var Daemon */
|
||||
private $daemon;
|
||||
|
||||
/** @var WordPress */
|
||||
private $wordpressTrigger;
|
||||
|
||||
/** @var CronHelper */
|
||||
private $cronHelper;
|
||||
|
||||
/** @var RemoteExecutorHandler */
|
||||
private $remoteExecutorHandler;
|
||||
|
||||
/** @var ActionScheduler */
|
||||
private $actionScheduler;
|
||||
|
||||
/**
|
||||
* Default 20 seconds
|
||||
* @var int
|
||||
*/
|
||||
private $remainingExecutionLimit = 20;
|
||||
|
||||
public function __construct(
|
||||
WPFunctions $wp,
|
||||
Daemon $daemon,
|
||||
WordPress $wordpressTrigger,
|
||||
CronHelper $cronHelper,
|
||||
RemoteExecutorHandler $remoteExecutorHandler,
|
||||
ActionScheduler $actionScheduler
|
||||
) {
|
||||
$this->wp = $wp;
|
||||
$this->daemon = $daemon;
|
||||
$this->wordpressTrigger = $wordpressTrigger;
|
||||
$this->cronHelper = $cronHelper;
|
||||
$this->remoteExecutorHandler = $remoteExecutorHandler;
|
||||
$this->actionScheduler = $actionScheduler;
|
||||
}
|
||||
|
||||
public function init(): void {
|
||||
$this->wp->addAction(self::NAME, [$this, 'process']);
|
||||
$this->wp->addFilter('action_scheduler_maximum_execution_time_likely_to_be_exceeded', [$this, 'storeRemainingExecutionLimit'], 10, 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run daemon that processes scheduled tasks for limited time
|
||||
*/
|
||||
public function process(): void {
|
||||
$this->wp->addAction('action_scheduler_after_process_queue', [$this, 'afterProcess']);
|
||||
$this->wp->addAction('mailpoet_cron_get_execution_limit', [$this, 'getDaemonExecutionLimit']);
|
||||
$this->daemon->run($this->cronHelper->createDaemon($this->cronHelper->createToken()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for adjusting the execution for the cron daemon (MailPoet\Cron\Daemon)
|
||||
*/
|
||||
public function getDaemonExecutionLimit(): int {
|
||||
return $this->remainingExecutionLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* After Action Scheduler finishes queue always check there is more work to do and in case there is we trigger additional runner.
|
||||
*/
|
||||
public function afterProcess(): void {
|
||||
if ($this->wordpressTrigger->checkExecutionRequirements()) {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is hooked into action_scheduler_maximum_execution_time_likely_to_be_exceeded
|
||||
* It checks how much action scheduler execution time is left for the daemon to run
|
||||
*/
|
||||
public function storeRemainingExecutionLimit($likelyExceeded, $runner, $processedActions, $executionTime, $maxExecutionTime): bool {
|
||||
$newLimit = floor(($maxExecutionTime - $executionTime) - self::EXECUTION_LIMIT_MARGIN);
|
||||
$this->remainingExecutionLimit = intval(max($newLimit, 0));
|
||||
return (bool)$likelyExceeded;
|
||||
}
|
||||
}
|
61
mailpoet/lib/Cron/ActionScheduler/Actions/DaemonTrigger.php
Normal file
61
mailpoet/lib/Cron/ActionScheduler/Actions/DaemonTrigger.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Cron\ActionScheduler\Actions;
|
||||
|
||||
use MailPoet\Cron\ActionScheduler\ActionScheduler;
|
||||
use MailPoet\Cron\ActionScheduler\RemoteExecutorHandler;
|
||||
use MailPoet\Cron\Triggers\WordPress;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class DaemonTrigger {
|
||||
const NAME = 'mailpoet/cron/daemon-trigger';
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
/** @var WordPress */
|
||||
private $wordpressTrigger;
|
||||
|
||||
/** @var RemoteExecutorHandler */
|
||||
private $remoteExecutorHandler;
|
||||
|
||||
/** @var ActionScheduler */
|
||||
private $actionScheduler;
|
||||
|
||||
public function __construct(
|
||||
WPFunctions $wp,
|
||||
WordPress $wordpressTrigger,
|
||||
RemoteExecutorHandler $remoteExecutorHandler,
|
||||
ActionScheduler $actionScheduler
|
||||
) {
|
||||
$this->wp = $wp;
|
||||
$this->wordpressTrigger = $wordpressTrigger;
|
||||
$this->remoteExecutorHandler = $remoteExecutorHandler;
|
||||
$this->actionScheduler = $actionScheduler;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
$this->wp->addAction(self::NAME, [$this, 'process']);
|
||||
if (!$this->actionScheduler->hasScheduledAction(self::NAME)) {
|
||||
$this->actionScheduler->scheduleRecurringAction($this->wp->currentTime('timestamp'), 20, self::NAME);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In regular intervals checks if there are scheduled tasks to execute.
|
||||
* In case there are tasks it spawns a recurring action.
|
||||
*/
|
||||
public function process(): void {
|
||||
$hasJobsToDo = $this->wordpressTrigger->checkExecutionRequirements();
|
||||
if (!$hasJobsToDo) {
|
||||
$this->actionScheduler->unscheduleAction(DaemonRun::NAME);
|
||||
return;
|
||||
}
|
||||
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') - 1, 1, DaemonRun::NAME);
|
||||
$this->remoteExecutorHandler->triggerExecutor();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user