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

Open
wants to merge 7 commits into
base: 3.x
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
],
"require": {
"php": "^8.1",
"spiral/boot": "^3.0",
"spiral/boot": "^3.12",
"spiral/attributes": "^2.8 || ^3.0",
"spiral/tokenizer": "^3.0",
"spiral/scaffolder": "^3.0",
"spiral/tokenizer": "^3.12",
"spiral/scaffolder": "^3.12",
"spiral/roadrunner-bridge": "^2.0 || ^3.0",
"temporal/sdk": "^2.7"
},
"require-dev": {
"spiral/framework": "^3.0",
"spiral/framework": "^3.12",
"spiral/testing": "^2.6",
"vimeo/psalm": "^5.17"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Bootloader/TemporalBridgeBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function init(
$this->initConfig($env);

$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 Temporal\Activity\ActivityInterface;
use Temporal\Worker\WorkerFactoryInterface;
use Temporal\Workflow\WorkflowInterface;

#[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 ($type === ActivityInterface::class) {
// Workflows are stateful. So you need a type to create instances.
$worker->registerActivity(
$declaration->getName(),
fn(ReflectionClass $class): object => $this->container->make($class->getName()),
);
$worker->registerActivity($declaration->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 @@ -13,6 +13,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(DeclarationLocatorInterface::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