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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

* (improvement) Simplify task run duration calculation.
* (improvement) Transform exception to log entry, so that the worker doesn't constantly fail.
* (deprecation) Deprecate entity getters in favor of properties.


2.3.2
Expand Down
32 changes: 16 additions & 16 deletions src/Command/TaskLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,25 @@ private function showTaskDetails (TorrStyle $io, int $taskId) : int
}

$io->definitionList(
["Task ID" => $task->getId()],
["Task ID" => $task->id],
["Task" => $task->getTaskLabel()],
["Status" => $status],
["Task Class" => $task->getTaskClass() ?? "<fg=gray>—</>"],
["Runs" => \count($task->getRuns())],
["Task Class" => $task->getTaskClass()],
["Runs" => \count($task->runs)],
["Total Duration" => $this->formatDuration($task->getTotalDuration())],
["Handled by" => implode(" on ", $handled)],
["Registered" => $task->getTimeQueued()->format("c")],
["Registered" => $task->timeQueued->format("c")],
);

$index = \count($task->getRuns());
$index = \count($task->runs);

foreach ($task->getRuns() as $run)
foreach ($task->runs as $run)
{
$status = "<fg=yellow>running</>";

if ($run->isFinished())
if ($run->isFinished)
{
$status = $run->isSuccess()
$status = $run->success
? "<fg=green>succeeded</>"
: "<fg=red>failed</>";
}
Expand All @@ -165,19 +165,19 @@ private function showTaskDetails (TorrStyle $io, int $taskId) : int
));
$io->writeln(\sprintf(
"Started: %s",
$run->getTimeStarted()->format("c"),
$run->timeStarted->format("c"),
));

if ($run->isFinished())
if ($run->isFinished)
{
$io->writeln(\sprintf(
"Duration: %s",
$this->formatDuration((float) $run->getDuration()),
$this->formatDuration((float) $run->duration),
));
$io->writeln("Output:");
$io->newLine();
$io->writeln("------------------");
$io->writeln((string) $run->getOutput());
$io->writeln((string) $run->output);
$io->writeln("------------------");
}

Expand Down Expand Up @@ -211,17 +211,17 @@ private function showList (TorrStyle $io, int $limit) : void
}

$rows[] = [
$task->getId(),
$task->id,
\sprintf(
"<fg=%s>%s</>",
null !== $task->getTaskLabel() ? "yellow" : "gray",
$task->getTaskLabel() ?? "—",
),
$status,
$task->getTaskClass() ?? "<fg=gray>—</>",
\count($task->getRuns()),
$task->getTaskClass(),
\count($task->runs),
$this->formatDuration($task->getTotalDuration()),
$task->getTimeQueued()->format("c"),
$task->timeQueued->format("c"),
];
}
$rows[] = [
Expand Down
23 changes: 14 additions & 9 deletions src/Entity/TaskLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class TaskLog
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "AUTO")]
#[ORM\Column(name: "id", type: Types::INTEGER)]
private ?int $id = null;
public private(set) ?int $id = null;

/**
* ULIDs have only 22 characters, but just to be sure
*/
#[ORM\Column(type: Types::STRING, length: 50, unique: true)]
private string $taskId;
public private(set) string $taskId;

/**
* The encoded task details
Expand All @@ -52,12 +52,12 @@ class TaskLog
*
*/
#[ORM\Column(name: "time_queued", type: Types::DATETIMETZ_IMMUTABLE)]
private \DateTimeImmutable $timeQueued;
public private(set) \DateTimeImmutable $timeQueued;

/** @var Collection<int, TaskRun> */
#[ORM\OneToMany(mappedBy: "taskLog", targetEntity: TaskRun::class, cascade: ["remove"], orphanRemoval: true)]
#[ORM\OrderBy(["timeStarted" => "asc"])]
private Collection $runs;
public private(set) Collection $runs;

/**
*/
Expand All @@ -71,20 +71,23 @@ public function __construct (
}

/**
* @deprecated use the property directly instead
*/
public function getId () : ?int
{
return $this->id;
}

/**
* @deprecated use the property directly instead
*/
public function getTaskId () : string
{
return $this->taskId;
}

/**
* @deprecated use the property directly instead
*/
public function getTimeQueued () : \DateTimeImmutable
{
Expand All @@ -93,6 +96,8 @@ public function getTimeQueued () : \DateTimeImmutable

/**
* @return Collection<int, TaskRun>
*
* @deprecated use the property directly instead
*/
public function getRuns () : Collection
{
Expand All @@ -106,7 +111,7 @@ public function isSuccess () : bool
{
foreach ($this->runs as $run)
{
if ($run->isSuccess())
if ($run->success)
{
return true;
}
Expand All @@ -121,7 +126,7 @@ public function getLastUnfinishedRun () : ?TaskRun
{
foreach ($this->runs as $run)
{
if (!$run->isFinished())
if (!$run->isFinished)
{
return $run;
}
Expand Down Expand Up @@ -202,12 +207,12 @@ public function getStatus () : ?bool

foreach ($this->runs as $run)
{
if (!$run->isFinished())
if (!$run->isFinished)
{
continue;
}

if ($run->isSuccess())
if ($run->success)
{
return true;
}
Expand All @@ -228,7 +233,7 @@ public function getTotalDuration () : float

foreach ($this->runs as $run)
{
$duration += (float) $run->getDuration();
$duration += (float) $run->duration;
}

return $duration;
Expand Down
29 changes: 20 additions & 9 deletions src/Entity/TaskRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,43 @@ class TaskRun
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "AUTO")]
#[ORM\Column(name: "id", type: Types::INTEGER)]
private ?int $id = null;
public private(set) ?int $id = null;

/**
*/
#[ORM\ManyToOne(targetEntity: TaskLog::class, inversedBy: "runs")]
#[ORM\JoinColumn(name: "task_log_id", referencedColumnName: "id", nullable: false)]
private TaskLog $taskLog;
public private(set) TaskLog $taskLog;

/**
*
*/
#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE)]
private \DateTimeImmutable $timeStarted;
public private(set) \DateTimeImmutable $timeStarted;

/**
*
*/
#[ORM\Column(type: Types::FLOAT, nullable: true)]
private ?float $duration = null;
public private(set) ?float $duration = null;

/**
*/
#[ORM\Column(type: Types::BOOLEAN, nullable: true)]
private ?bool $success = null;
public private(set) ?bool $success = null;

/**
*/
#[ORM\Column(type: Types::BOOLEAN, nullable: true)]
private ?bool $finishedProperly = null;
public private(set) ?bool $finishedProperly = null;

/**
*/
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $output = null;
public private(set) ?string $output = null;
public bool $isFinished {
get => null !== $this->duration;
}
// endregion

/**
Expand All @@ -71,21 +74,25 @@ public function __construct (

// region Accessors
/**
* @deprecated use the property directly instead
*/
public function getTaskLog () : TaskLog
{
return $this->taskLog;
}

/**
* @deprecated use the property directly instead
*/
public function getTimeStarted () : \DateTimeImmutable
{
return $this->timeStarted;
}

/**
* Whether the task was finished successfully.
* @deprecated use the property directly instead
*
* Whether the task was finished successfully
*
* @return bool|null Whether the task was finished successfully. Will return null if not yet finished.
*/
Expand All @@ -95,13 +102,15 @@ public function isSuccess () : ?bool
}

/**
* @deprecated use the property directly instead
*/
public function getOutput () : ?string
{
return $this->output;
}

/**
* @deprecated use the property directly instead
*/
public function isFinished () : bool
{
Expand All @@ -110,6 +119,8 @@ public function isFinished () : bool

/**
* The duration of the run in nanoseconds (if the task is finished already)
*
* @deprecated use the property directly instead
*/
public function getDuration () : ?float
{
Expand Down Expand Up @@ -157,7 +168,7 @@ private function finalizeRun (
?string $output = null,
) : void
{
if ($this->isFinished())
if ($this->isFinished)
{
$this->logger?->error("Can't finalize task run {id} as it is already finished.", [
"id" => $this->id,
Expand Down
2 changes: 1 addition & 1 deletion src/Log/LogCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function cleanLogEntries () : array
$deleted[] = \sprintf(
"<fg=yellow>%s</> (%s)",
$logEntry->getTaskLabel(),
$logEntry->getTimeQueued()->format("c"),
$logEntry->timeQueued->format("c"),
);

$this->model->remove($logEntry);
Expand Down
4 changes: 2 additions & 2 deletions src/Model/TaskLogModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public function fetchOutdatedTasks (
// TTL. If so, then adjust the purge date, to fulfill both
$cutOffEntry = $this->getCutoffEntry($maxEntries);

if (null !== $cutOffEntry && $cutOffEntry->getTimeQueued() > $purgeBefore)
if (null !== $cutOffEntry && $cutOffEntry->timeQueued > $purgeBefore)
{
$purgeBefore = $cutOffEntry->getTimeQueued();
$purgeBefore = $cutOffEntry->timeQueued;
}

/** @var TaskLog[] $entries */
Expand Down