generated from spiral-packages/package-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from spiral/feature/info-command
Adds temporal:info console command
- Loading branch information
Showing
19 changed files
with
496 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\TemporalBridge\Commands; | ||
|
||
use Spiral\Boot\DirectoriesInterface; | ||
use Spiral\Console\Attribute\AsCommand; | ||
use Spiral\Console\Attribute\Option; | ||
use Spiral\Console\Command; | ||
use Spiral\TemporalBridge\DeclarationLocatorInterface; | ||
use Spiral\TemporalBridge\DeclarationWorkerResolver; | ||
use Symfony\Component\Console\Helper\TableSeparator; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Temporal\Internal\Declaration\Reader\ActivityReader; | ||
use Temporal\Internal\Declaration\Reader\WorkflowReader; | ||
use Temporal\Workflow\WorkflowInterface; | ||
|
||
#[AsCommand( | ||
name: 'temporal:info', | ||
description: 'Show information about registered temporal workflows and activities.', | ||
)] | ||
final class InfoCommand extends Command | ||
{ | ||
#[Option(name: 'show-activities', shortcut: 'a', description: 'Show activities.')] | ||
private bool $showActivities = false; | ||
|
||
public function perform( | ||
DeclarationLocatorInterface $locator, | ||
DeclarationWorkerResolver $workerResolver, | ||
WorkflowReader $workflowReader, | ||
ActivityReader $activityReader, | ||
DirectoriesInterface $dir, | ||
): int { | ||
$workflows = []; | ||
$activities = []; | ||
|
||
foreach ($locator->getDeclarations() as $type => $declaration) { | ||
$taskQueue = $workerResolver->resolve($declaration); | ||
|
||
if ($type === WorkflowInterface::class) { | ||
$prototype = $workflowReader->fromClass($declaration->getName()); | ||
$workflows[$prototype->getID()] = [ | ||
'class' => $declaration->getName(), | ||
'file' => $declaration->getFileName(), | ||
'name' => $prototype->getID(), | ||
'task_queue' => \implode(', ', $taskQueue), | ||
]; | ||
} else { | ||
$taskQueueShown = false; | ||
|
||
foreach ($activityReader->fromClass($declaration->getName()) as $prototype) { | ||
$activities[$declaration->getName()][$prototype->getID()] = [ | ||
'file' => $declaration->getFileName(), | ||
'name' => $prototype->getID(), | ||
'handler' => $declaration->getShortName() . '::' . $prototype->getHandler()->getName(), | ||
'task_queue' => !$taskQueueShown ? \implode(', ', $taskQueue) : '', | ||
]; | ||
|
||
$taskQueueShown = true; | ||
} | ||
} | ||
} | ||
|
||
$rootDir = \realpath($dir->get('root')) . '/'; | ||
|
||
\assert($this->output instanceof OutputInterface); | ||
|
||
$this->output->title('Workflows'); | ||
|
||
$table = $this->table(['Name', 'Class', 'Task Queue']); | ||
foreach ($workflows as $workflow) { | ||
$table->addRow([ | ||
\sprintf('<fg=green>%s</>', $workflow['name']), | ||
$workflow['class'] . "\n" . \sprintf('<fg=blue>%s</>', \str_replace($rootDir, '', $workflow['file'])), | ||
$workflow['task_queue'], | ||
]); | ||
} | ||
$table->render(); | ||
|
||
if (!$this->showActivities) { | ||
return self::SUCCESS; | ||
} | ||
|
||
$this->output->title('Activities'); | ||
$table = $this->table(['Name', 'Class', 'Task Queue']); | ||
foreach ($activities as $class => $prototypes) { | ||
foreach ($prototypes as $prototype) { | ||
$table->addRow([ | ||
$prototype['name'], | ||
$prototype['handler'], | ||
$prototype['task_queue'], | ||
]); | ||
} | ||
if (\end($activities) !== $prototypes) { | ||
$table->addRow(new TableSeparator()); | ||
} | ||
} | ||
$table->render(); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\TemporalBridge; | ||
|
||
use Spiral\Attributes\ReaderInterface; | ||
use Spiral\TemporalBridge\Attribute\AssignWorker; | ||
use Spiral\TemporalBridge\Config\TemporalConfig; | ||
|
||
final class DeclarationWorkerResolver | ||
{ | ||
public function __construct( | ||
private readonly ReaderInterface $reader, | ||
private readonly TemporalConfig $config, | ||
) { | ||
} | ||
|
||
/** | ||
* Find the worker name for the given workflow or class declaration. If no worker is assigned, the default task | ||
* queue name is returned. | ||
*/ | ||
public function resolve(\ReflectionClass $declaration): array | ||
{ | ||
$queue = $this->resolveTaskQueues($declaration); | ||
|
||
if ($queue !== []) { | ||
return $queue; | ||
} | ||
|
||
return [$this->config->getDefaultWorker()]; | ||
} | ||
|
||
private function resolveTaskQueues(\ReflectionClass $declaration): array | ||
{ | ||
$assignWorker = $this->reader->getClassMetadata($declaration, AssignWorker::class); | ||
|
||
$workers = []; | ||
|
||
foreach ($assignWorker as $worker) { | ||
$workers[] = $worker->taskQueue; | ||
} | ||
|
||
if ($workers !== []) { | ||
return $workers; | ||
} | ||
|
||
$parents = $declaration->getInterfaceNames(); | ||
foreach ($parents as $parent) { | ||
$queueName = $this->resolveTaskQueues(new \ReflectionClass($parent)); | ||
if ($queueName !== []) { | ||
return $queueName; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\TemporalBridge\Tests\App; | ||
|
||
class SomeActivityWithDefaultWorker | ||
{ | ||
} |
Oops, something went wrong.