From 5655998734feeb8447cf3e8335e94ca7e5482d54 Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Thu, 16 May 2024 16:10:05 +0200 Subject: [PATCH] Add task log entities and model --- src/Entity/TaskLog.php | 128 +++++++++++++++ src/Entity/TaskRun.php | 153 ++++++++++++++++++ .../Log/InvalidLogActionException.php | 9 ++ src/Model/TaskLogModel.php | 68 ++++++++ 4 files changed, 358 insertions(+) create mode 100644 src/Entity/TaskLog.php create mode 100644 src/Entity/TaskRun.php create mode 100644 src/Exception/Log/InvalidLogActionException.php create mode 100644 src/Model/TaskLogModel.php diff --git a/src/Entity/TaskLog.php b/src/Entity/TaskLog.php new file mode 100644 index 0000000..922b9df --- /dev/null +++ b/src/Entity/TaskLog.php @@ -0,0 +1,128 @@ + */ + #[ORM\OneToMany(mappedBy: "taskLog", targetEntity: TaskRun::class)] + #[ORM\OrderBy(["timeStarted" => "asc"])] + private Collection $runs; + + + public function __construct ( + string $taskId, + ) + { + $this->taskId = $taskId; + $this->runs = new ArrayCollection(); + $this->timeQueued = now(); + } + + /** + */ + public function getId () : ?int + { + return $this->id; + } + + /** + */ + public function getTaskId () : string + { + return $this->taskId; + } + + /** + */ + public function getTimeQueued () : \DateTimeImmutable + { + return $this->timeQueued; + } + + + /** + * @return Collection + */ + public function getRuns () : Collection + { + return $this->runs; + } + + + /** + */ + public function isFinishedSuccessfully () : ?bool + { + foreach ($this->runs as $run) + { + if ($run->isFinishedSuccessfully()) + { + return true; + } + } + + return false; + } + + /** + */ + public function getLastUnfinishedRun () : ?TaskRun + { + foreach ($this->runs as $run) + { + if (!$run->isFinished()) + { + return $run; + } + } + + return null; + } + + /** + * + */ + public function startRun () : TaskRun + { + if ($this->isFinishedSuccessfully()) + { + throw new InvalidLogActionException("Can't start a run for a task #{$this->id} that is already finished."); + } + + $run = new TaskRun($this); + $this->runs->add($run); + return $run; + } +} diff --git a/src/Entity/TaskRun.php b/src/Entity/TaskRun.php new file mode 100644 index 0000000..f54be7f --- /dev/null +++ b/src/Entity/TaskRun.php @@ -0,0 +1,153 @@ +taskLog = $taskLog; + $this->timeStarted = now(); + } + + + //region Accessors + /** + */ + public function getTaskLog () : TaskLog + { + return $this->taskLog; + } + + /** + */ + public function getTimeStarted () : \DateTimeImmutable + { + return $this->timeStarted; + } + + /** + */ + public function getTimeFinished () : ?\DateTimeImmutable + { + return $this->timeFinished; + } + + /** + * Whether the task was finished successfully. + */ + public function isFinishedSuccessfully () : bool + { + return true === $this->successful; + } + + /** + */ + public function getOutput () : ?string + { + return $this->output; + } + + /** + */ + public function isFinished () : bool + { + return null !== $this->timeFinished; + } + + /** + * Whether the task was finished properly or was automatically finished. + */ + public function hasFinishedProperly () : bool + { + return true === $this->finishedProperly; + } + //endregion + + + /** + */ + public function finish (bool $successful, ?string $output) : void + { + if ($this->isFinished()) + { + throw new InvalidLogActionException("Can't finish task run #{$this->id} as it is already finished."); + } + + $this->finishedProperly = true; + $this->successful = $successful; + $this->output = $output; + $this->timeFinished = now(); + } + + + /** + * Aborts the task, without finishing it properly + */ + public function abort (bool $successful, ?string $output = null) : void + { + if ($this->isFinished()) + { + throw new InvalidLogActionException("Can't abort task run #{$this->id} as it is already finished."); + } + + $this->finishedProperly = false; + $this->successful = $successful; + $this->output = $output; + $this->timeFinished = now(); + } +} diff --git a/src/Exception/Log/InvalidLogActionException.php b/src/Exception/Log/InvalidLogActionException.php new file mode 100644 index 0000000..6b9c8f4 --- /dev/null +++ b/src/Exception/Log/InvalidLogActionException.php @@ -0,0 +1,9 @@ + */ + private EntityRepository $repository; + + /** + */ + public function __construct ( + private readonly EntityManagerInterface $entityManager, + ) + { + $repository = $this->entityManager->getRepository(TaskLog::class); + \assert($repository instanceof EntityRepository); + $this->repository = $repository; + } + + + /** + * Gets or creates the log entry for the given task + */ + public function getLogForTask (Task $task) : TaskLog + { + $log = $this->repository->findOneBy([ + "taskId" => $task->ulid, + ]); + + if (null !== $log) + { + return $log; + } + + // if it isn't created yet, create a new one + $log = new TaskLog($task->ulid); + $this->entityManager->persist($log); + + return $log; + } + + + /** + * Creates a new run for the given task (lok) and marks it as persisted. + */ + public function createRunForTask (TaskLog $log) : TaskRun + { + $run = new TaskRun($log); + $this->entityManager->persist($run); + + return $run; + } + + + /** + */ + public function flush () : void + { + $this->entityManager->flush(); + } +}