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 UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

* `TaskLog::getTaskObject()` was removed. Use `TaskDetailsNormalizer::deserializeTask($log)` instead.
* `TaskLog::$ulid` was removed.
* `Task` has no constructor anymore, so you need to remove your `parent::__construct()` calls.


1.x to 2.0
Expand Down
7 changes: 6 additions & 1 deletion src/Director/RunDirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RunDirector
*/
public function __construct (
private readonly TaskLogModel $logModel,
private readonly TaskRun $run,
private readonly ?TaskRun $run,
)
{
$this->output = new ChainOutput();
Expand All @@ -40,6 +40,11 @@ public function getIo () : TorrStyle
*/
public function finish (bool $success) : void
{
if (null === $this->run)
{
return;
}

$this->run->finish($success, $this->output->getBufferedOutput());
$this->logModel->flush();
}
Expand Down
18 changes: 17 additions & 1 deletion src/Director/TaskDirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Torr\TaskManager\Director;

use Psr\Log\LoggerInterface;
use Torr\TaskManager\Identification\TaskIdentifier;
use Torr\TaskManager\Model\TaskLogModel;
use Torr\TaskManager\Task\Task;

Expand All @@ -11,14 +13,28 @@
*/
public function __construct (
private TaskLogModel $logModel,
private LoggerInterface $logger,
private TaskIdentifier $taskIdentifier,
) {}

/**
*
*/
public function startRun (Task $task) : RunDirector
{
$log = $this->logModel->getLogForTask($task);
$uuid = $this->taskIdentifier->getUuid($task);

if (null === $uuid)
{
$this->logger->critical("Could not identify task of type {type}", [
"type" => get_debug_type($task),
"task" => $task,
]);

return new RunDirector($this->logModel, null);
}

$log = $this->logModel->getLogForUuid($task, $uuid);

// create run
$run = $this->logModel->createRunForTask($log);
Expand Down
6 changes: 3 additions & 3 deletions src/Entity/TaskLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ class TaskLog
/**
*/
public function __construct (
Task $task,
object $task,
string $uuid,
)
{
$this->taskClass = $task::class;
/** @phpstan-ignore-next-line property.deprecated (The uuid integration will be refactored in v4) */
$this->taskId = $task->ulid;
$this->taskId = $uuid;
$this->runs = new ArrayCollection();
$this->timeQueued = now();
}
Expand Down
21 changes: 21 additions & 0 deletions src/Identification/TaskIdStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Torr\TaskManager\Identification;

use Symfony\Component\Messenger\Stamp\StampInterface;
use Symfony\Component\Uid\UuidV7;

/**
* @final
*/
readonly class TaskIdStamp implements StampInterface
{
public string $taskId;

/**
*/
public function __construct ()
{
$this->taskId = new UuidV7()->toRfc4122();
}
}
122 changes: 122 additions & 0 deletions src/Identification/TaskIdentificationMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php declare(strict_types=1);

namespace Torr\TaskManager\Identification;

use Symfony\Component\Console\Messenger\RunCommandContext;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Torr\TaskManager\Entity\TaskLog;
use Torr\TaskManager\Entity\TaskRun;
use Torr\TaskManager\Model\TaskLogModel;
use Torr\TaskManager\Normalizer\TaskDetailsNormalizer;
use Torr\TaskManager\Task\Task;

/**
* @final
*/
readonly class TaskIdentificationMiddleware implements MiddlewareInterface
{
/**
*/
public function __construct (
private TaskIdentifier $taskIdentifier,
private TaskLogModel $logModel,
private TaskDetailsNormalizer $detailsNormalizer,
) {}

/**
*/
public function handle (Envelope $envelope, StackInterface $stack) : Envelope
{
$stamp = $envelope->last(TaskIdStamp::class);

if (null === $stamp)
{
$stamp = new TaskIdStamp();
$envelope = $envelope->with($stamp);
}

$this->taskIdentifier->setUuid($envelope->getMessage(), $stamp->taskId);

// create task run before
$logEntry = $this->logModel->getLogForUuid($envelope->getMessage(), $stamp->taskId);
$logEntry->setTaskDetails($this->detailsNormalizer->normalizeTaskDetails($envelope));
$this->logModel->flush();

try
{
// push message through the stack
$envelope = $stack->next()->handle($envelope, $stack);
}
catch (ExceptionInterface $exception)
{
$run = $this->getTaskRun($logEntry, $envelope->getMessage());

if (null !== $run)
{
$run->abort(false, $exception->getMessage());
$this->logModel->flush();
}

throw $exception;
}

$handledStamp = $envelope->last(HandledStamp::class);

if (null !== $handledStamp)
{
$run = $this->getTaskRun($logEntry, $envelope->getMessage());

if (null !== $run)
{
$run->abort(
success: true,
output: $this->extractResultContent($handledStamp),
);
}
}

// update log with most up-to-date details
$logEntry->setTaskDetails($this->detailsNormalizer->normalizeTaskDetails($envelope));
$this->logModel->flush();

return $envelope;
}

/**
*
*/
private function getTaskRun (TaskLog $log, object $message) : ?TaskRun
{
// if the message is not a task, no task director will be called.
// so we can create a run here
if (!$message instanceof Task)
{
return $this->logModel->createRunForTask($log);
}

return $log->getLastUnfinishedRun();
}

/**
*/
private function extractResultContent (HandledStamp $handledStamp) : ?string
{
$result = $handledStamp->getResult();

if (\is_string($result))
{
return $result;
}

if ($result instanceof RunCommandContext)
{
return $result->output;
}

return null;
}
}
46 changes: 46 additions & 0 deletions src/Identification/TaskIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);

namespace Torr\TaskManager\Identification;

use Symfony\Contracts\Service\ResetInterface;

/**
* @final
*/
class TaskIdentifier implements ResetInterface
{
/** @var \WeakMap<object, string> */
private \WeakMap $uuidMap;

/**
*/
public function __construct ()
{
$this->uuidMap = new \WeakMap();
}

/**
*
*/
public function setUuid (object $message, string $uuid) : void
{
$this->uuidMap[$message] = $uuid;
}

/**
*
*/
public function getUuid (object $message) : ?string
{
return $this->uuidMap[$message]
?? null;
}

/**
*
*/
public function reset () : void
{
$this->uuidMap = new \WeakMap();
}
}
102 changes: 0 additions & 102 deletions src/Listener/MessengerEventListener.php

This file was deleted.

Loading
Loading