Skip to content

Commit

Permalink
Creating activity in temporal.activity scope
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Feb 28, 2024
1 parent 1226969 commit bd329ae
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
}

Expand Down Expand Up @@ -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;
Expand All @@ -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()),
);
}
}
20 changes: 20 additions & 0 deletions tests/app/src/SomeActivityWithScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Spiral\TemporalBridge\Tests\App;

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

#[Scope(Spiral::TemporalActivity)]
#[AssignWorker(taskQueue: 'worker1')]
class SomeActivityWithScope
{
// Binding ArrayAccess $tasks available only in temporal.activity scope
public function __construct(
private readonly \ArrayAccess $tasks,
) {
}
}
18 changes: 18 additions & 0 deletions tests/src/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

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;
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 @@ -36,6 +38,7 @@ protected function setUp(): void
new AttributeReader(),
new TemporalConfig(['defaultWorker' => 'foo']),
),
$this->getContainer(),
);
}

Expand Down Expand Up @@ -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))
);
}
}

0 comments on commit bd329ae

Please sign in to comment.