Helper method to support migration from Paris to Doctrine

This commit adds a helper method called convertTaskClass() to
CronWorkerRunner to support the migration of the code inside this class
from Paris to Doctrine. This method receives a ScheduledTaskEntity and
returns a ScheduledTask. It will be remove once the migration is
finished.

[MAILPOET-2996]
This commit is contained in:
Rodrigo Primo
2021-09-23 14:27:13 -03:00
committed by Veljko V
parent 65eef3dfbe
commit 60ae98e09c

View File

@@ -2,6 +2,7 @@
namespace MailPoet\Cron;
use MailPoet\Entities\ScheduledTaskEntity;
use MailPoet\Models\ScheduledTask;
use MailPoet\Newsletter\Sending\ScheduledTasksRepository;
use MailPoet\WP\Functions as WPFunctions;
@@ -63,17 +64,24 @@ class CronWorkerRunner {
return false;
}
$task = null;
try {
foreach ($dueTasks as $i => $task) {
$this->prepareTask($worker, $task);
$parisTask = null;
foreach ($dueTasks as $task) {
$parisTask = $this->convertTaskClass($task);
if ($parisTask) {
$this->prepareTask($worker, $parisTask);
}
}
foreach ($runningTasks as $i => $task) {
$this->processTask($worker, $task);
foreach ($runningTasks as $task) {
$parisTask = $this->convertTaskClass($task);
if ($parisTask) {
$this->processTask($worker, $parisTask);
}
}
} catch (\Exception $e) {
if ($task && $e->getCode() !== CronHelper::DAEMON_EXECUTION_LIMIT_REACHED) {
$task->rescheduleProgressively();
if ($parisTask && $e->getCode() !== CronHelper::DAEMON_EXECUTION_LIMIT_REACHED) {
$parisTask->rescheduleProgressively();
}
throw $e;
}
@@ -173,4 +181,16 @@ class CronWorkerRunner {
$task->status = ScheduledTask::STATUS_COMPLETED;
$task->save();
}
// temporary function to convert an ScheduledTaskEntity object to ScheduledTask while we don't migrate the rest of
// the code in this class to use Doctrine entities
private function convertTaskClass(ScheduledTaskEntity $doctrineTask): ?ScheduledTask {
$parisTask = ScheduledTask::findOne($doctrineTask->getId());
if (!$parisTask instanceof ScheduledTask) {
return null;
}
return $parisTask;
}
}