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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.2.0
=====

* (feature) Add app validator to ensure, that all tasks are properly configured in the routing.


2.1.1
=====

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"php": ">= 8.4",
"21torr/bundle-helpers": "^2.3",
"21torr/cli": "^1.2.3",
"21torr/hosting": "^4.0",
"21torr/snail": "^1.0.0",
"doctrine/doctrine-bundle": "^2.14",
"doctrine/orm": "^3.3",
Expand All @@ -22,7 +23,7 @@
"symfony/config": "^7.2",
"symfony/console": "^7.2",
"symfony/dependency-injection": "^7.2",
"symfony/event-dispatcher-contracts": "^3.5",
"symfony/event-dispatcher": "^7.3",
"symfony/http-kernel": "^7.2",
"symfony/messenger": "^7.2",
"symfony/scheduler": "^7.2",
Expand Down
69 changes: 69 additions & 0 deletions src/App/AppValidator/TaskManagerAppValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types=1);

namespace Torr\TaskManager\App\AppValidator;

use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Sender\SendersLocatorInterface;
use Torr\Hosting\Event\ValidateAppEvent;
use Torr\TaskManager\Config\BundleConfig;

/**
* @final
*/
readonly class TaskManagerAppValidator
{
/**
*/
public function __construct (
private BundleConfig $bundleConfig,
#[Autowire(service: "messenger.senders_locator")]
private SendersLocatorInterface $sendersLocator,
) {}

/**
*/
#[AsEventListener]
public function onValidateApp (ValidateAppEvent $event) : void
{
$io = $event->io;
$io->section("Task Manager: Check that all Tasks have Transports");

$taskClasses = $this->bundleConfig->taskClasses;
$io->comment(\sprintf(
"Found <fg=blue>%d %s</>:",
\count($taskClasses),
1 !== \count($taskClasses) ? "tasks" : "task",
));

$allHaveSenders = true;

foreach ($taskClasses as $taskClass)
{
$task = new \ReflectionClass($taskClass)->newInstanceWithoutConstructor();
$envelope = Envelope::wrap($task);

$senders = iterator_to_array($this->sendersLocator->getSenders($envelope));
$taskHasSenders = [] !== $senders;

$io->writeln(\sprintf(
"• %s ... %s",
$taskClass,
$taskHasSenders ? "<fg=green>ok</>" : "<fg=red>not mapped</>",
));

$allHaveSenders = $allHaveSenders && $taskHasSenders;
}

if ($allHaveSenders)
{
$io->success("All tasks configured correctly.");

return;
}

$io->error("Not all tasks are configured correctly.");
$event->markAppAsInvalid("Task Manager: not all tasks have routing");
}
}
2 changes: 2 additions & 0 deletions src/Config/BundleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public function __construct (
public array $sortedQueues,
/** @var string[] */
public array $failureTransports = [],
/** @var class-string[] */
public array $taskClasses = [],
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Integrates into symfony/messenger, by automatically detecting the failure transports
*/
final class AutoDetectFailureTransportsCompilerInterface implements CompilerPassInterface
final class AutoDetectFailureTransportsCompilerPass implements CompilerPassInterface
{
/**
* @inheritDoc
Expand Down
44 changes: 44 additions & 0 deletions src/DependencyInjection/FindTaskClassesCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);

namespace Torr\TaskManager\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Torr\TaskManager\Config\BundleConfig;

/**
* Compiler pass, that collects all tasks from the container that are from the app
* itself (= in the App\ namespace).
*
* @final
*/
readonly class FindTaskClassesCompilerPass implements CompilerPassInterface
{
/**
*
*/
#[\Override]
public function process (ContainerBuilder $container) : void
{
$taskClasses = [];

foreach ($container->findTaggedServiceIds("task-manager.task") as $taskServiceId => $config)
{
$definition = $container->getDefinition($taskServiceId);
$taskFQCN = $definition->getClass();

if (null !== $taskFQCN && str_starts_with($taskFQCN, "App\\"))
{
$taskClasses[] = $taskFQCN;
}

// Remove task service definition, as these must never be a service
// in the actual runtime. We just use the mechanism to collect all
// tasks in the app.
$container->removeDefinition($taskServiceId);
}

$container->getDefinition(BundleConfig::class)
->setArgument('$taskClasses', $taskClasses);
}
}
11 changes: 9 additions & 2 deletions src/TaskManagerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Torr\BundleHelpers\Bundle\ConfigurableBundleExtension;
use Torr\TaskManager\Config\BundleConfig;
use Torr\TaskManager\DependencyInjection\AutoDetectFailureTransportsCompilerInterface;
use Torr\TaskManager\DependencyInjection\AutoDetectFailureTransportsCompilerPass;
use Torr\TaskManager\DependencyInjection\FindTaskClassesCompilerPass;
use Torr\TaskManager\DependencyInjection\TaskManagerBundleConfiguration;
use Torr\TaskManager\Log\LogCleaner;
use Torr\TaskManager\Task\Task;

final class TaskManagerBundle extends Bundle
{
Expand Down Expand Up @@ -45,7 +47,12 @@ static function (array $config, ContainerBuilder $container) : void
*/
public function build (ContainerBuilder $container) : void
{
$container->addCompilerPass(new AutoDetectFailureTransportsCompilerInterface());
$container
->addCompilerPass(new AutoDetectFailureTransportsCompilerPass())
->addCompilerPass(new FindTaskClassesCompilerPass());

$container->registerForAutoconfiguration(Task::class)
->addTag("task-manager.task");
}

/**
Expand Down