Add method for immediate scheduling/rescheduling cron tasks

[MAILPOET-3726]
This commit is contained in:
Rostislav Wolny
2021-09-06 13:18:33 +02:00
committed by Veljko V
parent b9815c609f
commit c2c383e0b5
2 changed files with 54 additions and 0 deletions

View File

@@ -23,6 +23,23 @@ class CronWorkerScheduler {
$this->scheduledTaskRepository = $scheduledTaskRepository;
}
public function scheduleImmediatelyIfNotRunning($taskType, $priority = ScheduledTaskEntity::PRIORITY_LOW): ScheduledTaskEntity {
$task = $this->scheduledTaskRepository->findScheduledOrRunningTask($taskType);
// Do nothing when task is running
if (($task instanceof ScheduledTaskEntity) && $task->getStatus() === null) {
return $task;
}
$now = Carbon::createFromTimestamp($this->wp->currentTime('timestamp'));
// Reschedule existing scheduled task
if ($task instanceof ScheduledTaskEntity) {
$task->setScheduledAt($now);
$task->setPriority($priority);
$this->scheduledTaskRepository->flush();
}
// Schedule new task
return $this->schedule($taskType, $now, $priority);
}
public function schedule($taskType, $nextRunDate, $priority = ScheduledTaskEntity::PRIORITY_LOW): ScheduledTaskEntity {
$alreadyScheduled = $this->scheduledTaskRepository->findScheduledTask($taskType);
if ($alreadyScheduled) {