generated from spiral-packages/package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from msmakouz/feature/workers
Adding an ability to register activities and workflows in specific workers
- Loading branch information
Showing
14 changed files
with
396 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\TemporalBridge\Attribute; | ||
|
||
use Spiral\Attributes\NamedArgumentConstructor; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor] | ||
final class AssignWorker | ||
{ | ||
public function __construct( | ||
public string $name | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\TemporalBridge; | ||
|
||
use Spiral\Attributes\ReaderInterface; | ||
use Spiral\Boot\FinalizerInterface; | ||
use Spiral\TemporalBridge\Attribute\AssignWorker; | ||
use Spiral\TemporalBridge\Config\TemporalConfig; | ||
use Temporal\Worker\WorkerFactoryInterface; | ||
use Temporal\Worker\WorkerInterface; | ||
use Temporal\Worker\WorkerOptions; | ||
|
||
final class WorkersRegistry implements WorkersRegistryInterface | ||
{ | ||
/** @psalm-var array<non-empty-string, WorkerInterface> */ | ||
private array $workers = []; | ||
|
||
/** @psalm-param array<non-empty-string, WorkerOptions> $options */ | ||
public function __construct( | ||
private WorkerFactoryInterface $workerFactory, | ||
private ReaderInterface $reader, | ||
private FinalizerInterface $finalizer, | ||
private TemporalConfig $config | ||
) { | ||
} | ||
|
||
public function get(\ReflectionClass $declaration): WorkerInterface | ||
{ | ||
$name = $this->resolveName($declaration); | ||
$options = $this->config->getWorkers(); | ||
|
||
if (!$this->hasWorker($name)) { | ||
$this->workers[$name] = $this->workerFactory->newWorker($name, $options[$name] ?? null); | ||
$this->workers[$name]->registerActivityFinalizer(fn() => $this->finalizer->finalize()); | ||
} | ||
|
||
return $this->workers[$name]; | ||
} | ||
|
||
private function hasWorker(string $name): bool | ||
{ | ||
return isset($this->workers[$name]); | ||
} | ||
|
||
private function resolveName(\ReflectionClass $declaration): string | ||
{ | ||
$assignWorker = $this->reader->firstClassMetadata($declaration, AssignWorker::class); | ||
|
||
if ($assignWorker === null) { | ||
return $this->config->getDefaultWorker(); | ||
} | ||
|
||
return $assignWorker->name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\TemporalBridge; | ||
|
||
use Temporal\Worker\WorkerInterface; | ||
|
||
interface WorkersRegistryInterface | ||
{ | ||
public function get(\ReflectionClass $declaration): WorkerInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\TemporalBridge\Tests\App; | ||
|
||
use Spiral\TemporalBridge\Attribute\AssignWorker; | ||
|
||
#[AssignWorker(name: 'worker1')] | ||
class SomeActivity | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\TemporalBridge\Tests\App; | ||
|
||
use Spiral\TemporalBridge\Attribute\AssignWorker; | ||
|
||
#[AssignWorker(name: 'worker2')] | ||
class SomeWorkflow | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\TemporalBridge\Tests\App; | ||
|
||
class WithoutAttribute | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\TemporalBridge\Tests\Attribute; | ||
|
||
use Spiral\Attributes\Factory; | ||
use Spiral\TemporalBridge\Attribute\AssignWorker; | ||
use Spiral\TemporalBridge\Tests\App\SomeActivity; | ||
use Spiral\TemporalBridge\Tests\App\SomeWorkflow; | ||
use Spiral\TemporalBridge\Tests\App\WithoutAttribute; | ||
use Spiral\TemporalBridge\Tests\TestCase; | ||
|
||
final class AssignWorkerTest extends TestCase | ||
{ | ||
/** @dataProvider assignWorkerDataProvider */ | ||
public function testAssignWorkerAttribute(\ReflectionClass $class, ?AssignWorker $expected = null): void | ||
{ | ||
$reader = (new Factory())->create(); | ||
|
||
$this->assertEquals($expected, $reader->firstClassMetadata($class, AssignWorker::class)); | ||
} | ||
|
||
public function assignWorkerDataProvider(): \Traversable | ||
{ | ||
yield [new \ReflectionClass(SomeActivity::class), new AssignWorker('worker1')]; | ||
yield [new \ReflectionClass(SomeWorkflow::class), new AssignWorker('worker2')]; | ||
yield [new \ReflectionClass(WithoutAttribute::class), null]; | ||
} | ||
} |
Oops, something went wrong.