Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/Entity/TaskLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions src/Entity/TaskRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Model/TaskLogModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down