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
TaskTrigger.php
73 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\CliCommands; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use InvalidArgumentException; |
| 9 | use MailPoet\Entities\ScheduledTaskEntity; |
| 10 | use MailPoet\Newsletter\Sending\ScheduledTasksRepository; |
| 11 | use MailPoetVendor\Carbon\Carbon; |
| 12 | |
| 13 | class TaskTrigger { |
| 14 | private ScheduledTasksRepository $scheduledTasksRepository; |
| 15 | |
| 16 | private WorkerTypesCatalog $workerTypesCatalog; |
| 17 | |
| 18 | private ScheduledTaskResolver $taskResolver; |
| 19 | |
| 20 | public function __construct( |
| 21 | ScheduledTasksRepository $scheduledTasksRepository, |
| 22 | WorkerTypesCatalog $workerTypesCatalog, |
| 23 | ScheduledTaskResolver $taskResolver |
| 24 | ) { |
| 25 | $this->scheduledTasksRepository = $scheduledTasksRepository; |
| 26 | $this->workerTypesCatalog = $workerTypesCatalog; |
| 27 | $this->taskResolver = $taskResolver; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Marks a task as due now so the site's own cron processor picks it up. Does not kick the pipeline. |
| 32 | * |
| 33 | * @return array{id: int, type: string} |
| 34 | */ |
| 35 | public function trigger(string $type, ?int $taskId = null): array { |
| 36 | $this->workerTypesCatalog->assertValidType($type); |
| 37 | |
| 38 | $task = $taskId !== null |
| 39 | ? $this->resolveTaskById($taskId, $type) |
| 40 | : $this->resolveTaskByType($type); |
| 41 | |
| 42 | $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); |
| 43 | $task->setScheduledAt(Carbon::now()->millisecond(0)); |
| 44 | $this->scheduledTasksRepository->persist($task); |
| 45 | $this->scheduledTasksRepository->flush(); |
| 46 | |
| 47 | return [ |
| 48 | 'id' => (int)$task->getId(), |
| 49 | 'type' => (string)$task->getType(), |
| 50 | ]; |
| 51 | } |
| 52 | |
| 53 | private function resolveTaskByType(string $type): ScheduledTaskEntity { |
| 54 | $task = $this->scheduledTasksRepository->findSoonestScheduledTaskByType($type); |
| 55 | if (!$task instanceof ScheduledTaskEntity) { |
| 56 | throw new InvalidArgumentException("No scheduled task of type '{$type}' found. Run `wp mailpoet cron list` to see existing tasks."); |
| 57 | } |
| 58 | return $task; |
| 59 | } |
| 60 | |
| 61 | private function resolveTaskById(int $taskId, string $type): ScheduledTaskEntity { |
| 62 | $task = $this->taskResolver->resolveById($taskId, $type); |
| 63 | |
| 64 | $status = $task->getStatus(); |
| 65 | if (!in_array($status, [ScheduledTaskEntity::STATUS_SCHEDULED, ScheduledTaskEntity::STATUS_PAUSED], true)) { |
| 66 | $current = $this->taskResolver->nameStatus($task); |
| 67 | throw new InvalidArgumentException("Task {$taskId} is '{$current}' and cannot be triggered. Only scheduled or paused tasks can be triggered."); |
| 68 | } |
| 69 | |
| 70 | return $task; |
| 71 | } |
| 72 | } |
| 73 |