Skip to content

Commit

Permalink
Merge branch '2.0' into bugfix/issue-69
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Dec 15, 2023
2 parents 24a9f10 + ab48701 commit 9f62558
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Attribute/AssignWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
final class AssignWorker
{
public function __construct(
public readonly string $name
public readonly string $name,
) {
}
}
24 changes: 17 additions & 7 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly RoadRunnerMode $mode,
private readonly ReaderInterface $reader,
private readonly TemporalConfig $config,
private readonly Container $container
private readonly Container $container,
) {
}

Expand All @@ -45,7 +45,9 @@ public function serve(): void
$hasDeclarations = false;
foreach ($declarations as $type => $declaration) {
// Worker that listens on a task queue and hosts both workflow and activity implementations.
$worker = $registry->get($this->resolveQueueName($declaration));
$queueName = $this->resolveQueueName($declaration) ?? $this->config->getDefaultWorker();

$worker = $registry->get($queueName);

if ($type === WorkflowInterface::class) {
// Workflows are stateful. So you need a type to create instances.
Expand All @@ -56,7 +58,7 @@ public function serve(): void
// Workflows are stateful. So you need a type to create instances.
$worker->registerActivity(
$declaration->getName(),
fn(ReflectionClass $class): object => $this->container->make($class->getName())
fn(ReflectionClass $class): object => $this->container->make($class->getName()),
);
}
$hasDeclarations = true;
Expand All @@ -70,14 +72,22 @@ public function serve(): void
$factory->run();
}

private function resolveQueueName(\ReflectionClass $declaration): string
private function resolveQueueName(\ReflectionClass $declaration): ?string
{
$assignWorker = $this->reader->firstClassMetadata($declaration, AssignWorker::class);

if ($assignWorker === null) {
return $this->config->getDefaultWorker();
if ($assignWorker !== null) {
return $assignWorker->name;
}

$parents = $declaration->getInterfaceNames();
foreach ($parents as $parent) {
$queueName = $this->resolveQueueName(new \ReflectionClass($parent));
if ($queueName !== null) {
return $queueName;
}
}

return $assignWorker->name;
return null;
}
}
50 changes: 39 additions & 11 deletions tests/src/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,53 @@

final class DispatcherTest extends TestCase
{
public function testResolvingQueueName(): void
private \ReflectionMethod $method;
private Dispatcher $dispatcher;

protected function setUp(): void
{
$dispatcher = new Dispatcher(
parent::setUp();

$this->dispatcher = new Dispatcher(
RoadRunnerMode::Temporal,
new AttributeReader(),
new TemporalConfig(['defaultWorker' => 'foo']),
$this->getContainer(),
);

$ref = new \ReflectionClass($dispatcher);
$method = $ref->getMethod('resolveQueueName');
$ref = new \ReflectionClass($this->dispatcher);
$this->method = $ref->getMethod('resolveQueueName');
$this->method->setAccessible(true);
}

public function testResolvingQueueNameWithAttributeOnClass(): void
{
$queue = $this->method->invoke(
$this->dispatcher,
new \ReflectionClass(ActivityInterfaceWithAttribute::class),
);

$this->assertSame('worker1', $queue);
}

$queue = $method->invoke(
$dispatcher,
new \ReflectionClass(ActivityInterfaceWithAttribute::class)
public function testResolvingQueueNameWithAttributeOnParentClass(): void
{
$queue = $this->method->invoke(
$this->dispatcher,
new \ReflectionClass(ActivityClass::class),
);

$this->assertSame('worker1', $queue);
}

$queue = $method->invoke(
$dispatcher,
new \ReflectionClass(ActivityInterfaceWithoutAttribute::class)
public function testResolvingQueueNameWithoutAttribute(): void
{
$queue = $this->method->invoke(
$this->dispatcher,
new \ReflectionClass(ActivityInterfaceWithoutAttribute::class),
);
$this->assertSame('foo', $queue);

$this->assertNull($queue);
}

public function testServeWithoutDeclarations(): void
Expand Down Expand Up @@ -105,3 +129,7 @@ interface ActivityInterfaceWithAttribute
interface ActivityInterfaceWithoutAttribute
{
}

class ActivityClass implements ActivityInterfaceWithAttribute
{
}

0 comments on commit 9f62558

Please sign in to comment.