Skip to content
Open
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
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
3.3.0
=====

* (feature) Properly serialize task objects in task log.
* (deprecation) Deprecate `TaskLog::getTaskObject()`. Use `TaskDetailsNormalizer::deserializeTask()` instead.
* (improvement) Avoid redundant `COUNT` queries in `LogCleaner` by using the count of fetched IDs directly.
* (internal) Add tests for `TaskDetailsNormalizer`, `LogCleaner`, `TaskRegistry`, `RegisterTasksEvent`, `TaskManager`, `TaskLog`, and `TaskRun`.


3.2.4
=====

* (bug) Fix stamps passed to `TaskManager::enqueue()` being silently dropped due to `Envelope::with()` being immutable.
* (improvement) Add database index on `time_queued` column of `task_manager_tasks` for faster log queries and cleanup.
* (internal) Remove unused `Paginator` wrapper in `TaskLogModel::getMostRecentEntries()`.
* (security) Replace `serialize()`/`unserialize()` with Symfony Serializer in `TaskDetailsNormalizer`. Add `TaskDetailsNormalizer::deserializeTask()` as replacement for the deprecated `TaskLog::getTaskObject()`.


3.2.3
=====

Expand Down
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.x to 4.0
==========

* `TaskLog::getTaskObject()` was removed. Use `TaskDetailsNormalizer::deserializeTask($log)` instead.


1.x to 2.0
==========

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"symfony/lock": "^7.4 || ^8.0",
"symfony/messenger": "^7.4 || ^8.0",
"symfony/scheduler": "^7.4 || ^8.0",
"symfony/serializer": "^7.4 || ^8.0",
"symfony/string": "^7.4 || ^8.0",
"symfony/uid": "^7.4 || ^8.0"
},
Expand Down Expand Up @@ -63,8 +64,8 @@
"forward-command": true
},
"branch-alias": {
"2.x-dev": "2.99.x-dev",
"dev-next": "2.99.x-dev"
"3.x-dev": "3.99.x-dev",
"dev-next": "3.99.x-dev"
}
},
"scripts": {
Expand Down
25 changes: 17 additions & 8 deletions src/Console/ChainOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final readonly class ChainOutput implements OutputInterface
{
private ConsoleOutput $consoleOutput;
private OutputInterface $mainOutput;
private BufferedOutput $bufferedOutput;

/**
Expand All @@ -18,10 +18,19 @@
int $verbosity = self::VERBOSITY_NORMAL,
bool $decorated = true,
?OutputFormatterInterface $formatter = null,
?OutputInterface $mainOutput = null,
)
{
$this->bufferedOutput = new BufferedOutput($verbosity, $decorated, $formatter);
$this->consoleOutput = new ConsoleOutput($verbosity, $decorated, $formatter);

$this->mainOutput = $mainOutput ?? new ConsoleOutput();
$this->mainOutput->setDecorated($decorated);
$this->mainOutput->setVerbosity($verbosity);

Check failure on line 28 in src/Console/ChainOutput.php

View workflow job for this annotation

GitHub Actions / build-test (8.5)

Parameter #1 $level of method Symfony\Component\Console\Output\OutputInterface::setVerbosity() expects 8|16|32|64|128|256, int given.

if (null !== $formatter)
{
$this->mainOutput->setFormatter($formatter);
}
}

/**
Expand All @@ -31,7 +40,7 @@
public function write (iterable|string $messages, bool $newline = false, int $options = 0) : void
{
$this->bufferedOutput->write($messages, $newline, $options);
$this->consoleOutput->write($messages, $newline, $options);
$this->mainOutput->write($messages, $newline, $options);
}

/**
Expand All @@ -41,7 +50,7 @@
public function writeln (iterable|string $messages, int $options = 0) : void
{
$this->bufferedOutput->writeln($messages, $options);
$this->consoleOutput->writeln($messages, $options);
$this->mainOutput->writeln($messages, $options);
}

/**
Expand All @@ -51,7 +60,7 @@
public function setVerbosity (int $level) : void
{
$this->bufferedOutput->setVerbosity($level);
$this->consoleOutput->setVerbosity($level);
$this->mainOutput->setVerbosity($level);
}

/**
Expand Down Expand Up @@ -113,8 +122,8 @@
#[\Override]
public function setDecorated (bool $decorated) : void
{
$this->bufferedOutput->setDecorated(true);
$this->consoleOutput->setDecorated(true);
$this->bufferedOutput->setDecorated($decorated);
$this->mainOutput->setDecorated($decorated);
}

/**
Expand All @@ -132,7 +141,7 @@
public function setFormatter (OutputFormatterInterface $formatter) : void
{
$this->bufferedOutput->setFormatter($formatter);
$this->consoleOutput->setFormatter($formatter);
$this->mainOutput->setFormatter($formatter);
}

/**
Expand Down
18 changes: 8 additions & 10 deletions src/Entity/TaskLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
#[ORM\Entity]
#[ORM\Table(name: "task_manager_tasks")]
#[ORM\Index(columns: ["time_queued"], name: "idx_task_manager_tasks_time_queued")]
class TaskLog
{
/**
Expand Down Expand Up @@ -194,18 +195,15 @@ public function getTaskLabel () : ?string
}

/**
* Returns the unserialized cached task object
* @deprecated Use TaskDetailsNormalizer::deserializeTask() instead. Will be removed in 4.0.
*
* @todo Remove in 4.0.
*/
public function getTaskObject () : ?object
public function getTaskObject () : null
{
$task = $this->getTaskDetails()["task"] ?? null;
$unserialized = \is_string($task)
? unserialize($task)
: null;

return \is_object($unserialized)
? $unserialized
: null;
trigger_deprecation("21torr/task-manager", "3.2.5", "TaskLog::getTaskObject() is deprecated, use TaskDetailsNormalizer::deserializeTask() instead.");

return null;
}

/**
Expand Down
35 changes: 8 additions & 27 deletions src/Log/LogCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
use Symfony\Component\Clock\ClockInterface;
use Torr\TaskManager\Entity\TaskLog;
use Torr\TaskManager\Entity\TaskRun;
use Torr\TaskManager\Model\TaskLogModel;

final readonly class LogCleaner
{
public function __construct (
private int $logTtlInDays,
private int $logMaxEntries,
private TaskLogModel $model,
private EntityManagerInterface $entityManager,
private ClockInterface $clock,
) {}
Expand All @@ -23,49 +21,32 @@ public function __construct (
*/
public function cleanLogEntries () : int
{
$tasksBefore = $this->model->getTaskCount();

$taskIdToDelete = $this->fetchIdsToDelete();

if (empty($taskIdToDelete))
{
return 0;
}

// first delete runs, as they are a foreign key on the task logs
$this->deleteRuns($taskIdToDelete);

// then delete tasks
$this->deleteTasks($taskIdToDelete);

$tasksAfter = $this->model->getTaskCount();

return $tasksBefore > $tasksAfter
? ($tasksBefore - $tasksAfter)
: 0;
return \count($taskIdToDelete);
}

/**
*
*/
private function deleteRuns (array $taskIdsToDelete) : void
{
$runIdsToDelete = $this->entityManager->createQueryBuilder()
->select("run.id")
->from(TaskRun::class, "run")
->leftJoin("run.taskLog", "task")
->andWhere("task.id IN (:taskIds)")
->setParameter("taskIds", $taskIdsToDelete)
->getQuery()
->getArrayResult();

if (empty($runIdsToDelete))
{
return;
}

$runIdsToDelete = array_column($runIdsToDelete, "id");

$this->entityManager->createQueryBuilder()
->delete()
->from(TaskRun::class, "run")
->andWhere("run.id IN (:runIds)")
->setParameter("runIds", $runIdsToDelete)
->andWhere("IDENTITY(run.taskLog) IN (:taskIds)")
->setParameter("taskIds", $taskIdsToDelete)
->getQuery()
->execute();
}
Expand Down
3 changes: 1 addition & 2 deletions src/Manager/TaskManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public function enqueue (Task $task, array $stamps = []) : bool
return false;
}

$envelope = new Envelope($task);
$envelope->with(...$stamps);
$envelope = new Envelope($task, $stamps);

$this->messageBus->dispatch($envelope);

Expand Down
13 changes: 1 addition & 12 deletions src/Model/TaskLogModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Psr\Log\LoggerInterface;
use Torr\TaskManager\Entity\TaskLog;
use Torr\TaskManager\Entity\TaskRun;
Expand Down Expand Up @@ -58,14 +57,6 @@ public function getLogForTask (Task $task) : TaskLog
return $log;
}

/**
*
*/
public function getTaskCount () : int
{
return $this->repository->count();
}

/**
* Returns the latest task log entries
*
Expand All @@ -92,9 +83,7 @@ public function getMostRecentEntries (
}

/** @var TaskLog[] */
return (new Paginator($builder->getQuery()))
->getQuery()
->getResult();
return $builder->getQuery()->getResult();
}

/**
Expand Down
42 changes: 41 additions & 1 deletion src/Normalizer/TaskDetailsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace Torr\TaskManager\Normalizer;

use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerException;
use Symfony\Component\Serializer\SerializerInterface;
use Torr\TaskManager\Entity\TaskLog;
use Torr\TaskManager\Task\Task;

Expand All @@ -13,6 +17,11 @@
*/
final readonly class TaskDetailsNormalizer
{
public function __construct (
private SerializerInterface $serializer,
private LoggerInterface $logger,
) {}

/**
* @return TaskDetails
*/
Expand All @@ -29,9 +38,40 @@ public function normalizeTaskDetails (Envelope $envelope) : array
if ($task instanceof Task)
{
$details["label"] = $task->getMetaData()->label;
$details["task"] = serialize($task);
$details["task"] = $this->serializer->serialize($task, JsonEncoder::FORMAT);
}

return $details;
}

/**
* Deserializes the task object stored in the given log entry.
*/
public function deserializeTask (TaskLog $log) : ?Task
{
$serialized = $log->getTaskDetails()["task"] ?? null;

if (!\is_string($serialized))
{
return null;
}

try
{
$task = $this->serializer->deserialize($serialized, $log->taskClass, JsonEncoder::FORMAT);

return $task instanceof Task ? $task : null;
}
catch (SerializerException $exception)
{
$this->logger->error("Failed to deserialize task of class '{taskClass}' from log entry #{logId}: {message}", [
"taskClass" => $log->taskClass,
"logId" => $log->id,
"message" => $exception->getMessage(),
"exception" => $exception,
]);

return null;
}
}
}
Loading
Loading