ClaimedTaskRunner.php
1 month ago
Cli.php
1 month ago
CronCommand.php
1 month ago
DaemonRunner.php
1 month ago
ExecutionLimitOverride.php
1 month ago
ScheduledTaskResolver.php
1 month ago
ScheduledTasksLister.php
1 month ago
TaskAdder.php
1 month ago
TaskCanceller.php
1 month ago
TaskRunner.php
1 month ago
TaskTrigger.php
1 month ago
WorkerTypesCatalog.php
1 month ago
index.php
1 month ago
DaemonRunner.php
123 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\CliCommands; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Cron\CronHelper; |
| 9 | use MailPoet\Cron\CronWorkerRunner; |
| 10 | use MailPoet\Cron\CronWorkerScheduler; |
| 11 | use MailPoet\Cron\Daemon; |
| 12 | use MailPoet\Cron\Workers\WorkersFactory; |
| 13 | use MailPoet\Logging\LoggerFactory; |
| 14 | use MailPoet\Newsletter\Sending\ScheduledTasksRepository; |
| 15 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 16 | |
| 17 | /** |
| 18 | * Runs one full MailPoet daemon pass over all workers inside the current process. |
| 19 | * |
| 20 | * This is a single Daemon::run() pass: each worker runs once, processing its due tasks (a standard |
| 21 | * worker processes at most TASK_BATCH_SIZE tasks per run). It is not a backlog drain — large |
| 22 | * backlogs may need several passes or a targeted `wp mailpoet cron run <type>`. The daemon's own |
| 23 | * maintenance-mode early exit is preserved. The 20-second execution limit is lifted by default (or |
| 24 | * capped via $timeout); because the daemon sets its timer at construction, the cap applies to the |
| 25 | * whole pass with no extra tracking. |
| 26 | */ |
| 27 | class DaemonRunner { |
| 28 | private ExecutionLimitOverride $executionLimitOverride; |
| 29 | |
| 30 | private CronHelper $cronHelper; |
| 31 | |
| 32 | private CronWorkerScheduler $cronWorkerScheduler; |
| 33 | |
| 34 | private ScheduledTasksRepository $scheduledTasksRepository; |
| 35 | |
| 36 | private EntityManager $entityManager; |
| 37 | |
| 38 | private LoggerFactory $loggerFactory; |
| 39 | |
| 40 | private WorkersFactory $workersFactory; |
| 41 | |
| 42 | public function __construct( |
| 43 | ExecutionLimitOverride $executionLimitOverride, |
| 44 | CronHelper $cronHelper, |
| 45 | CronWorkerScheduler $cronWorkerScheduler, |
| 46 | ScheduledTasksRepository $scheduledTasksRepository, |
| 47 | EntityManager $entityManager, |
| 48 | LoggerFactory $loggerFactory, |
| 49 | WorkersFactory $workersFactory |
| 50 | ) { |
| 51 | $this->executionLimitOverride = $executionLimitOverride; |
| 52 | $this->cronHelper = $cronHelper; |
| 53 | $this->cronWorkerScheduler = $cronWorkerScheduler; |
| 54 | $this->scheduledTasksRepository = $scheduledTasksRepository; |
| 55 | $this->entityManager = $entityManager; |
| 56 | $this->loggerFactory = $loggerFactory; |
| 57 | $this->workersFactory = $workersFactory; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @return array{errors: array<array{worker: string, message: string}>} |
| 62 | */ |
| 63 | public function run(?int $timeout = null): array { |
| 64 | // createDaemon() resets last_error to null, so any last_error read back after the run is from |
| 65 | // this pass alone. Mirrors how the web cron (DaemonRun::process) builds its settings data. |
| 66 | $settingsDaemonData = $this->cronHelper->createDaemon($this->cronHelper->createToken()); |
| 67 | |
| 68 | $this->executionLimitOverride->overrideDuring($timeout, function () use ($settingsDaemonData): void { |
| 69 | $this->makeDaemon()->run($settingsDaemonData); |
| 70 | }); |
| 71 | |
| 72 | return [ |
| 73 | 'errors' => $this->readErrors(), |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return array<array{worker: string, message: string}> |
| 79 | */ |
| 80 | private function readErrors(): array { |
| 81 | $daemon = $this->cronHelper->getDaemon(); |
| 82 | $errors = is_array($daemon) ? ($daemon['last_error'] ?? null) : null; |
| 83 | if (!is_array($errors)) { |
| 84 | return []; |
| 85 | } |
| 86 | |
| 87 | // last_error is persisted untyped; normalise to the worker/message shape callers expect. |
| 88 | $normalised = []; |
| 89 | foreach ($errors as $error) { |
| 90 | $worker = is_array($error) ? ($error['worker'] ?? '') : ''; |
| 91 | $message = is_array($error) ? ($error['message'] ?? '') : ''; |
| 92 | $normalised[] = [ |
| 93 | 'worker' => is_string($worker) ? $worker : '', |
| 94 | 'message' => is_string($message) ? $message : '', |
| 95 | ]; |
| 96 | } |
| 97 | return $normalised; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * A fresh Daemon (and a fresh CronWorkerRunner) per invocation so both execution timers start at |
| 102 | * the run, not at container build. Otherwise the shared instances would carry a stale timer and a |
| 103 | * --timeout cap would be measured from the wrong moment. Protected so a test can substitute a |
| 104 | * daemon that persists a worker error and assert the error-retrieval path surfaces it. |
| 105 | */ |
| 106 | protected function makeDaemon(): Daemon { |
| 107 | $cronWorkerRunner = new CronWorkerRunner( |
| 108 | $this->cronHelper, |
| 109 | $this->cronWorkerScheduler, |
| 110 | $this->scheduledTasksRepository, |
| 111 | $this->loggerFactory |
| 112 | ); |
| 113 | |
| 114 | return new Daemon( |
| 115 | $this->cronHelper, |
| 116 | $cronWorkerRunner, |
| 117 | $this->entityManager, |
| 118 | $this->workersFactory, |
| 119 | $this->loggerFactory |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 |