From 122696942c6a86fa74544ce4d69c50a5c45aa413 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Tue, 27 Feb 2024 17:33:47 +0200 Subject: [PATCH 1/8] Add DispatcherScope attribute --- composer.json | 2 +- src/Dispatcher.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1104a89..a4008e9 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ "temporal/sdk": "^2.7" }, "require-dev": { - "spiral/framework": "^3.0", + "spiral/framework": "dev-feature/scopes as 3.12.0", "spiral/testing": "^2.6", "vimeo/psalm": "^5.17" }, diff --git a/src/Dispatcher.php b/src/Dispatcher.php index bc643b4..b30e145 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -5,13 +5,16 @@ namespace Spiral\TemporalBridge; use ReflectionClass; +use Spiral\Attribute\DispatcherScope; use Spiral\Boot\DispatcherInterface; use Spiral\Core\Container; +use Spiral\Framework\Spiral; use Spiral\RoadRunnerBridge\RoadRunnerMode; use Temporal\Activity\ActivityInterface; use Temporal\Worker\WorkerFactoryInterface; use Temporal\Workflow\WorkflowInterface; +#[DispatcherScope(Spiral::Temporal)] final class Dispatcher implements DispatcherInterface { public function __construct( From bd329ae819dbd7251cc5a78ab31cdf9f295a5602 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Wed, 28 Feb 2024 15:57:37 +0200 Subject: [PATCH 2/8] Creating activity in temporal.activity scope --- src/Dispatcher.php | 22 ++++++++++++++++------ tests/app/src/SomeActivityWithScope.php | 20 ++++++++++++++++++++ tests/src/DispatcherTest.php | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 tests/app/src/SomeActivityWithScope.php diff --git a/src/Dispatcher.php b/src/Dispatcher.php index b30e145..0fc989c 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -4,10 +4,13 @@ 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\Framework\Spiral; use Spiral\RoadRunnerBridge\RoadRunnerMode; use Temporal\Activity\ActivityInterface; @@ -19,8 +22,9 @@ 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, ) { } @@ -58,10 +62,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; @@ -75,4 +76,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(Spiral::TemporalActivity), + static fn(FactoryInterface $factory): object => $factory->make($class->getName()), + ); + } } diff --git a/tests/app/src/SomeActivityWithScope.php b/tests/app/src/SomeActivityWithScope.php new file mode 100644 index 0000000..bd06dff --- /dev/null +++ b/tests/app/src/SomeActivityWithScope.php @@ -0,0 +1,20 @@ + 'foo']), ), + $this->getContainer(), ); } @@ -101,4 +104,19 @@ public function testServeWithDeclarations(): void $this->dispatcher->serve(); } + + public function testScope(): void + { + $binder = $this->getContainer()->getBinder(Spiral::TemporalActivity); + $binder->bind(SomeActivityWithScope::class, SomeActivityWithScope::class); + $binder->bind(\ArrayAccess::class, $this->createMock(\ArrayAccess::class)); + + $ref = new \ReflectionMethod($this->dispatcher, 'makeActivity'); + $ref->invoke($this->dispatcher, new \ReflectionClass(SomeActivityWithScope::class)); + + $this->assertInstanceOf( + SomeActivityWithScope::class, + $ref->invoke($this->dispatcher, new \ReflectionClass(SomeActivityWithScope::class)) + ); + } } From 450de7331c8547617bb8d85d2af06563e61f1b92 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Wed, 28 Feb 2024 16:36:01 +0200 Subject: [PATCH 3/8] Remove unused code --- tests/src/DispatcherTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/src/DispatcherTest.php b/tests/src/DispatcherTest.php index 7f4bc36..4ff24bf 100644 --- a/tests/src/DispatcherTest.php +++ b/tests/src/DispatcherTest.php @@ -112,7 +112,6 @@ public function testScope(): void $binder->bind(\ArrayAccess::class, $this->createMock(\ArrayAccess::class)); $ref = new \ReflectionMethod($this->dispatcher, 'makeActivity'); - $ref->invoke($this->dispatcher, new \ReflectionClass(SomeActivityWithScope::class)); $this->assertInstanceOf( SomeActivityWithScope::class, From 8ea5fa49a27c1c565d1208b7c1c805ff7995a3b0 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Fri, 1 Mar 2024 11:36:48 +0200 Subject: [PATCH 4/8] Static method canServe, stable SF version --- composer.json | 2 +- src/Bootloader/TemporalBridgeBootloader.php | 2 +- src/Dispatcher.php | 10 ++++------ tests/src/DispatcherTest.php | 5 +---- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index a4008e9..8b2f52b 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ "temporal/sdk": "^2.7" }, "require-dev": { - "spiral/framework": "dev-feature/scopes as 3.12.0", + "spiral/framework": "^3.12", "spiral/testing": "^2.6", "vimeo/psalm": "^5.17" }, diff --git a/src/Bootloader/TemporalBridgeBootloader.php b/src/Bootloader/TemporalBridgeBootloader.php index deecddd..b0838d2 100644 --- a/src/Bootloader/TemporalBridgeBootloader.php +++ b/src/Bootloader/TemporalBridgeBootloader.php @@ -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 diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 0fc989c..0501f41 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -11,26 +11,24 @@ use Spiral\Core\FactoryInterface; use Spiral\Core\Scope; use Spiral\Core\ScopeInterface; -use Spiral\Framework\Spiral; use Spiral\RoadRunnerBridge\RoadRunnerMode; use Temporal\Activity\ActivityInterface; use Temporal\Worker\WorkerFactoryInterface; use Temporal\Workflow\WorkflowInterface; -#[DispatcherScope(Spiral::Temporal)] +#[DispatcherScope('temporal')] final class Dispatcher implements DispatcherInterface { public function __construct( - private readonly RoadRunnerMode $mode, 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 @@ -81,7 +79,7 @@ private function makeActivity(ReflectionClass $class): object { /** @psalm-suppress InvalidArgument */ return $this->scope->runScope( - new Scope(Spiral::TemporalActivity), + new Scope('temporal.activity'), static fn(FactoryInterface $factory): object => $factory->make($class->getName()), ); } diff --git a/tests/src/DispatcherTest.php b/tests/src/DispatcherTest.php index 4ff24bf..a3c9cd0 100644 --- a/tests/src/DispatcherTest.php +++ b/tests/src/DispatcherTest.php @@ -6,8 +6,6 @@ use Mockery as m; use Spiral\Attributes\AttributeReader; -use Spiral\Framework\Spiral; -use Spiral\RoadRunnerBridge\RoadRunnerMode; use Spiral\TemporalBridge\Config\TemporalConfig; use Spiral\TemporalBridge\DeclarationLocatorInterface; use Spiral\TemporalBridge\DeclarationWorkerResolver; @@ -32,7 +30,6 @@ protected function setUp(): void parent::setUp(); $this->dispatcher = new Dispatcher( - RoadRunnerMode::Temporal, $this->getContainer(), new DeclarationWorkerResolver( new AttributeReader(), @@ -107,7 +104,7 @@ public function testServeWithDeclarations(): void public function testScope(): void { - $binder = $this->getContainer()->getBinder(Spiral::TemporalActivity); + $binder = $this->getContainer()->getBinder('temporal.activity'); $binder->bind(SomeActivityWithScope::class, SomeActivityWithScope::class); $binder->bind(\ArrayAccess::class, $this->createMock(\ArrayAccess::class)); From 89af47da8762bb74313af8e375a426015e40527d Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Fri, 1 Mar 2024 11:42:36 +0200 Subject: [PATCH 5/8] Add tests for canServe method --- tests/src/DispatcherTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/src/DispatcherTest.php b/tests/src/DispatcherTest.php index a3c9cd0..2f0158f 100644 --- a/tests/src/DispatcherTest.php +++ b/tests/src/DispatcherTest.php @@ -6,6 +6,7 @@ use Mockery as m; use Spiral\Attributes\AttributeReader; +use Spiral\RoadRunnerBridge\RoadRunnerMode; use Spiral\TemporalBridge\Config\TemporalConfig; use Spiral\TemporalBridge\DeclarationLocatorInterface; use Spiral\TemporalBridge\DeclarationWorkerResolver; @@ -39,6 +40,18 @@ protected function setUp(): void ); } + 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); From 2dda706134e9d89297b75524d6cfec6a92f60386 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Fri, 1 Mar 2024 11:46:34 +0200 Subject: [PATCH 6/8] Up min version of SF components --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 8b2f52b..d612131 100644 --- a/composer.json +++ b/composer.json @@ -37,10 +37,10 @@ ], "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" }, From 4b81bbcd45a886512a12b3208ad9777e3900b80f Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Fri, 1 Mar 2024 11:48:56 +0200 Subject: [PATCH 7/8] Fix CS --- src/Dispatcher.php | 2 +- tests/app/src/SomeActivityWithScope.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 0501f41..35003d5 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -80,7 +80,7 @@ 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()), + static fn (FactoryInterface $factory): object => $factory->make($class->getName()), ); } } diff --git a/tests/app/src/SomeActivityWithScope.php b/tests/app/src/SomeActivityWithScope.php index bd06dff..e1134fd 100644 --- a/tests/app/src/SomeActivityWithScope.php +++ b/tests/app/src/SomeActivityWithScope.php @@ -5,10 +5,9 @@ namespace Spiral\TemporalBridge\Tests\App; use Spiral\Core\Attribute\Scope; -use Spiral\Framework\Spiral; use Spiral\TemporalBridge\Attribute\AssignWorker; -#[Scope(Spiral::TemporalActivity)] +#[Scope('temporal.activity')] #[AssignWorker(taskQueue: 'worker1')] class SomeActivityWithScope { From f36e848a161fe52b223dd90bedd8380dfe265252 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 30 Aug 2024 16:41:35 +0400 Subject: [PATCH 8/8] Rename activity context scope --- src/Dispatcher.php | 2 +- tests/app/src/SomeActivityWithScope.php | 4 ++-- tests/src/DispatcherTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 9501473..288fbd8 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -79,7 +79,7 @@ private function makeActivity(ReflectionClass $class): object { /** @psalm-suppress InvalidArgument */ return $this->scope->runScope( - new Scope('temporal.activity'), + new Scope('temporal-activity'), static fn (FactoryInterface $factory): object => $factory->make($class->getName()), ); } diff --git a/tests/app/src/SomeActivityWithScope.php b/tests/app/src/SomeActivityWithScope.php index e1134fd..2303b90 100644 --- a/tests/app/src/SomeActivityWithScope.php +++ b/tests/app/src/SomeActivityWithScope.php @@ -7,11 +7,11 @@ use Spiral\Core\Attribute\Scope; use Spiral\TemporalBridge\Attribute\AssignWorker; -#[Scope('temporal.activity')] +#[Scope('temporal-activity')] #[AssignWorker(taskQueue: 'worker1')] class SomeActivityWithScope { - // Binding ArrayAccess $tasks available only in temporal.activity scope + // Binding ArrayAccess $tasks available only in `temporal-activity` scope public function __construct( private readonly \ArrayAccess $tasks, ) { diff --git a/tests/src/DispatcherTest.php b/tests/src/DispatcherTest.php index dd9e64a..c2c1391 100644 --- a/tests/src/DispatcherTest.php +++ b/tests/src/DispatcherTest.php @@ -117,7 +117,7 @@ public function testServeWithDeclarations(): void public function testScope(): void { - $binder = $this->getContainer()->getBinder('temporal.activity'); + $binder = $this->getContainer()->getBinder('temporal-activity'); $binder->bind(SomeActivityWithScope::class, SomeActivityWithScope::class); $binder->bind(\ArrayAccess::class, $this->createMock(\ArrayAccess::class));