Skip to content

Commit

Permalink
Merge pull request #10 from 21TORR/task-log
Browse files Browse the repository at this point in the history
  • Loading branch information
keichinger authored May 16, 2024
2 parents eafd2c4 + becfccc commit a79fcad
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 68 deletions.
19 changes: 11 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@
],
"homepage": "https://github.com/21TORR/TaskManagerBundle",
"require": {
"php": ">= 8.1",
"php": ">= 8.3",
"21torr/bundle-helpers": "^2.1",
"21torr/cli": "^1.0",
"symfony/config": "^6.4 || ^7.0",
"symfony/console": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"doctrine/orm": "^2.19",
"symfony/clock": "^7.0",
"symfony/config": "^7.0",
"symfony/console": "^7.0",
"symfony/dependency-injection": "^7.0",
"symfony/event-dispatcher-contracts": "^3.4",
"symfony/http-kernel": "^6.4 || ^7.0",
"symfony/messenger": "^6.4 || ^7.0",
"symfony/string": "^6.4 || ^7.0"
"symfony/http-kernel": "^7.0",
"symfony/messenger": "^7.0",
"symfony/string": "^7.0",
"symfony/uid": "^7.0"
},
"require-dev": {
"21torr/janus": "^1.2",
"bamarni/composer-bin-plugin": "^1.8",
"roave/security-advisories": "dev-latest",
"symfony/phpunit-bridge": "^6.4 || ^7.0"
"symfony/phpunit-bridge": "^7.0"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 4 additions & 3 deletions src/Command/ListTasksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected function execute (InputInterface $input, OutputInterface $output) : in
foreach ($tasks as $task)
{
$row = [];
$metaData = $task->getMetaData();

if ($first)
{
Expand All @@ -69,10 +70,10 @@ protected function execute (InputInterface $input, OutputInterface $output) : in

$row[] = \sprintf(
"<fg=yellow>%s</>",
$task->key,
$metaData->getKey(),
);
$row[] = $task->label;
$row[] = \get_class($task->task);
$row[] = $metaData->label;
$row[] = \get_class($task);
$rows[] = $row;
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/Command/QueueTasksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Torr\TaskManager\Exception\Registry\UnknownTaskKeyException;
use Torr\TaskManager\Manager\TaskManager;
use Torr\TaskManager\Receiver\ReceiverHelper;
use Torr\TaskManager\Registry\Data\Task;
use Torr\TaskManager\Registry\TaskRegistry;
use Torr\TaskManager\Task\Task;

#[AsCommand("task-manager:queue")]
final class QueueTasksCommand extends Command
Expand Down Expand Up @@ -74,7 +74,7 @@ protected function execute (InputInterface $input, OutputInterface $output) : in
"• Queuing task %s",
$this->formatTaskLabel($task),
));
$this->taskManager->enqueue($task->task);
$this->taskManager->enqueue($task);
}

$io->success("All done.");
Expand Down Expand Up @@ -154,15 +154,17 @@ private function fetchTasksByKey (array $keys) : array
*/
private function formatTaskLabel (Task $task) : string
{
if (null !== $task->group)
$metaData = $task->getMetaData();

if (null !== $metaData->group)
{
return \sprintf(
"<fg=blue>%s</>: %s",
$task->group,
$task->label,
$metaData->group,
$metaData->label,
);
}

return $task->label;
return $metaData->label;
}
}
27 changes: 6 additions & 21 deletions src/Event/RegisterTasksEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Torr\TaskManager\Event;

use Symfony\Component\String\Slugger\AsciiSlugger;
use function Symfony\Component\String\u;
use Torr\TaskManager\Exception\Registry\DuplicateTaskRegisteredException;
use Torr\TaskManager\Registry\Data\Task;
use Torr\TaskManager\Task\Task;

/**
* This event lets you register your tasks, so that the UI can make them selectable
Expand All @@ -18,29 +16,17 @@ final class RegisterTasksEvent
{
/** @var array<string, Task> */
public array $tasks = [];
private readonly AsciiSlugger $slugger;

/**
*/
public function __construct ()
{
$this->slugger = new AsciiSlugger("en");
}


/**
* Registers a task
*
* @throws DuplicateTaskRegisteredException
*/
public function registerTask (
string $label,
object $message,
?string $group = null,
) : self
public function registerTask (Task $task) : self
{
$key = u($label)->lower()->toString();
$key = $this->slugger->slug($key)->toString();
$definition = $task->getMetaData();
$key = $definition->getKey();

if (\array_key_exists($key, $this->tasks))
{
Expand All @@ -50,8 +36,7 @@ public function registerTask (
));
}

$this->tasks[$key] = new Task($key, $label, $message, $group);

$this->tasks[$key] = $task;
return $this;
}

Expand All @@ -65,7 +50,7 @@ public function getTasks () : array

\usort(
$entries,
static fn (Task $left, Task $right) => \strnatcasecmp($left->label, $right->label),
static fn (Task $left, Task $right) => \strnatcasecmp($left->getMetaData()->label, $right->getMetaData()->label),
);

return $entries;
Expand Down
19 changes: 0 additions & 19 deletions src/Registry/Data/Task.php

This file was deleted.

12 changes: 7 additions & 5 deletions src/Registry/TaskRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Psr\EventDispatcher\EventDispatcherInterface;
use Torr\TaskManager\Event\RegisterTasksEvent;
use Torr\TaskManager\Registry\Data\Task;
use Torr\TaskManager\Task\Task;

/**
* Contains all tasks that are automatically configured to be runnable.
Expand Down Expand Up @@ -59,16 +59,18 @@ private function fetchGroupedTasks () : array

foreach ($tasks as $task)
{
if (null !== $task->group)
$definition = $task->getMetaData();

if (null !== $definition->group)
{
$grouped[$task->group][] = $task;
$grouped[$definition->group][] = $task;
}
else
{
$ungrouped[] = $task;
}

$this->keyMap[$task->key] = $task;
$this->keyMap[$definition->getKey()] = $task;
}

// sort groups by group label
Expand All @@ -85,7 +87,7 @@ private function fetchGroupedTasks () : array
{
\usort(
$entries,
static fn (Task $left, Task $right) => \strnatcasecmp($left->label, $right->label),
static fn (Task $left, Task $right) => \strnatcasecmp($left->getMetaData()->label, $right->getMetaData()->label),
);
}

Expand Down
23 changes: 17 additions & 6 deletions src/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@

namespace Torr\TaskManager\Task;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Uid\Ulid;

final class Task
/**
* A runnable task
*/
abstract class Task
{
public readonly string $ulid;

/**
*/
public function __construct ()
{
$this->ulid = (new Ulid())->toBase58();
}


/**
* Defines the metadata for this task
*/
public function __construct (
public readonly string $queueName,
public readonly Envelope $envelope,
) {}
abstract public function getMetaData () : TaskMetaData;
}
31 changes: 31 additions & 0 deletions src/Task/TaskMetaData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Torr\TaskManager\Task;

use Symfony\Component\String\Slugger\AsciiSlugger;
use function Symfony\Component\String\u;

/**
* A VO that describes a task
*/
final class TaskMetaData
{
/**
*/
public function __construct (
public readonly string $label,
public readonly ?string $group = null,
public readonly ?string $uniqueTaskId = null,
) {}


/**
* Returns a unique key for this task
*/
public function getKey () : string
{
$slugger = new AsciiSlugger("en");
$key = u($this->label)->lower()->toString();
return $slugger->slug($key)->toString();
}
}

0 comments on commit a79fcad

Please sign in to comment.