Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of scopes #80

Merged
merged 9 commits into from
Aug 30, 2024
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
2 changes: 1 addition & 1 deletion src/Bootloader/TemporalBridgeBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function init(
$this->initConfig($env);
$tokenizer->addListener($locator);
$console->addCommand(Commands\InfoCommand::class);
$kernel->addDispatcher($this->factory->make(Dispatcher::class));
$kernel->addDispatcher(Dispatcher::class);
}

public function addWorkerOptions(string $worker, WorkerOptions $options): void
Expand Down
29 changes: 20 additions & 9 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@

namespace Spiral\TemporalBridge;

use Psr\Container\ContainerInterface;
use ReflectionClass;
use Spiral\Attribute\DispatcherScope;
use Spiral\Boot\DispatcherInterface;
use Spiral\Core\Container;
use Spiral\Core\FactoryInterface;
use Spiral\Core\Scope;
use Spiral\Core\ScopeInterface;
use Spiral\RoadRunnerBridge\RoadRunnerMode;
use Spiral\TemporalBridge\Declaration\DeclarationDto;
use Spiral\TemporalBridge\Declaration\DeclarationType;
use Temporal\Worker\WorkerFactoryInterface;

#[DispatcherScope('temporal')]
final class Dispatcher implements DispatcherInterface
{
public function __construct(
private readonly RoadRunnerMode $mode,
private readonly Container $container,
private readonly ContainerInterface $container,
private readonly DeclarationWorkerResolver $workerResolver,
private readonly ScopeInterface $scope,
) {
}

public function canServe(): bool
public static function canServe(RoadRunnerMode $mode): bool
{
return \PHP_SAPI === 'cli' && $this->mode === RoadRunnerMode::Temporal;
return \PHP_SAPI === 'cli' && $mode === RoadRunnerMode::Temporal;
}

public function serve(): void
Expand Down Expand Up @@ -55,10 +60,7 @@ public function serve(): void

if ($declaration->type === DeclarationType::Activity) {
// Workflows are stateful. So you need a type to create instances.
$worker->registerActivity(
$declaration->class->getName(),
fn(ReflectionClass $class): object => $this->container->make($class->getName()),
);
$worker->registerActivity($declaration->class->getName(), $this->makeActivity(...));
}

$hasDeclarations = true;
Expand All @@ -72,4 +74,13 @@ public function serve(): void
// start primary loop
$factory->run();
}

private function makeActivity(ReflectionClass $class): object
{
/** @psalm-suppress InvalidArgument */
return $this->scope->runScope(
new Scope('temporal-activity'),
static fn (FactoryInterface $factory): object => $factory->make($class->getName()),
);
}
}
19 changes: 19 additions & 0 deletions tests/app/src/SomeActivityWithScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Spiral\TemporalBridge\Tests\App;

use Spiral\Core\Attribute\Scope;
use Spiral\TemporalBridge\Attribute\AssignWorker;

#[Scope('temporal-activity')]
#[AssignWorker(taskQueue: 'worker1')]
class SomeActivityWithScope
{
// Binding ArrayAccess $tasks available only in `temporal-activity` scope
public function __construct(
private readonly \ArrayAccess $tasks,
) {
}
}
29 changes: 28 additions & 1 deletion tests/src/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Spiral\TemporalBridge\Dispatcher;
use Spiral\TemporalBridge\Tests\App\SomeActivity;
use Spiral\TemporalBridge\Tests\App\SomeActivityWithDefaultWorker;
use Spiral\TemporalBridge\Tests\App\SomeActivityWithScope;
use Spiral\TemporalBridge\Tests\App\SomeWorkflow;
use Spiral\TemporalBridge\Tests\App\SomeWorkflowWithMultipleWorkers;
use Spiral\TemporalBridge\WorkersRegistryInterface;
Expand All @@ -30,15 +31,27 @@ protected function setUp(): void
parent::setUp();

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

public function testCanServe(): void
{
$this->assertTrue(Dispatcher::canServe(RoadRunnerMode::Temporal));

$this->assertFalse(Dispatcher::canServe(RoadRunnerMode::Http));
$this->assertFalse(Dispatcher::canServe(RoadRunnerMode::Jobs));
$this->assertFalse(Dispatcher::canServe(RoadRunnerMode::Grpc));
$this->assertFalse(Dispatcher::canServe(RoadRunnerMode::Tcp));
$this->assertFalse(Dispatcher::canServe(RoadRunnerMode::Centrifuge));
$this->assertFalse(Dispatcher::canServe(RoadRunnerMode::Unknown));
}

public function testServeWithoutDeclarations(): void
{
$locator = $this->mockContainer(DeclarationRegistryInterface::class);
Expand Down Expand Up @@ -101,4 +114,18 @@ public function testServeWithDeclarations(): void

$this->dispatcher->serve();
}

public function testScope(): void
{
$binder = $this->getContainer()->getBinder('temporal-activity');
$binder->bind(SomeActivityWithScope::class, SomeActivityWithScope::class);
$binder->bind(\ArrayAccess::class, $this->createMock(\ArrayAccess::class));

$ref = new \ReflectionMethod($this->dispatcher, 'makeActivity');

$this->assertInstanceOf(
SomeActivityWithScope::class,
$ref->invoke($this->dispatcher, new \ReflectionClass(SomeActivityWithScope::class))
);
}
}
Loading