From 24c7c15097e35841f202809f157dd858bbbbfe7e Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Mon, 12 Jan 2026 15:30:11 +0100 Subject: [PATCH] Transform exception to log entry, so that the worker doesn't constantly fail --- CHANGELOG.md | 2 +- composer.json | 1 + src/Entity/TaskLog.php | 5 +++-- src/Entity/TaskRun.php | 15 ++++++++++++--- src/Model/TaskLogModel.php | 4 +++- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ec9b0e..7a01f77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ===== * (improvement) Simplify task run duration calculation. - +* (improvement) Transform exception to log entry, so that the worker doesn't constantly fail. 2.3.2 diff --git a/composer.json b/composer.json index eee78bd..a2f046c 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "doctrine/doctrine-bundle": "^2.14", "doctrine/orm": "^3.3", "dragonmantank/cron-expression": "^3.4", + "psr/log": "^3.0", "symfony/clock": "^7.2", "symfony/config": "^7.2", "symfony/console": "^7.2", diff --git a/src/Entity/TaskLog.php b/src/Entity/TaskLog.php index ad32116..dedcb71 100644 --- a/src/Entity/TaskLog.php +++ b/src/Entity/TaskLog.php @@ -6,6 +6,7 @@ use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; +use Psr\Log\LoggerInterface; use Torr\TaskManager\Exception\Log\InvalidLogActionException; use Torr\TaskManager\Task\Task; @@ -140,14 +141,14 @@ public function isFinished () : bool /** * */ - public function startRun () : TaskRun + public function createRun (?LoggerInterface $logger = null) : TaskRun { if ($this->isSuccess()) { throw new InvalidLogActionException("Can't start a run for a task #{$this->id} that is already finished."); } - $run = new TaskRun($this); + $run = new TaskRun($this, $logger); $this->runs->add($run); return $run; diff --git a/src/Entity/TaskRun.php b/src/Entity/TaskRun.php index fefc519..4168c8a 100644 --- a/src/Entity/TaskRun.php +++ b/src/Entity/TaskRun.php @@ -4,8 +4,8 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; +use Psr\Log\LoggerInterface; use Torr\TaskManager\Duration\DurationCalculator; -use Torr\TaskManager\Exception\Log\InvalidLogActionException; use function Symfony\Component\Clock\now; @@ -60,7 +60,10 @@ class TaskRun /** */ - public function __construct (TaskLog $taskLog) + public function __construct ( + TaskLog $taskLog, + private ?LoggerInterface $logger = null, + ) { $this->taskLog = $taskLog; $this->timeStarted = now(); @@ -156,7 +159,13 @@ private function finalizeRun ( { if ($this->isFinished()) { - throw new InvalidLogActionException("Can't finalize task run #{$this->id} as it is already finished."); + $this->logger?->error("Can't finalize task run {id} as it is already finished.", [ + "id" => $this->id, + "success" => $success, + "finishedProperly" => $finishedProperly, + ]); + + return; } $this->success = $success; diff --git a/src/Model/TaskLogModel.php b/src/Model/TaskLogModel.php index e216bae..8843266 100644 --- a/src/Model/TaskLogModel.php +++ b/src/Model/TaskLogModel.php @@ -6,6 +6,7 @@ use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Tools\Pagination\Paginator; use Psr\Clock\ClockInterface; +use Psr\Log\LoggerInterface; use Torr\TaskManager\Entity\TaskLog; use Torr\TaskManager\Entity\TaskRun; use Torr\TaskManager\Task\Task; @@ -20,6 +21,7 @@ final class TaskLogModel public function __construct ( private readonly EntityManagerInterface $entityManager, private readonly ClockInterface $clock, + private readonly LoggerInterface $logger, ) { $this->repository = $this->entityManager->getRepository(TaskLog::class); @@ -79,7 +81,7 @@ public function getMostRecentEntries (int $limit = 100) : array */ public function createRunForTask (TaskLog $log) : TaskRun { - $run = new TaskRun($log); + $run = $log->createRun($this->logger); $this->entityManager->persist($run); return $run;