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

Adding support for Temporal Interceptors #65

Merged
merged 12 commits into from
Dec 25, 2023
27 changes: 15 additions & 12 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
on:
push:
branches:
- master
pull_request: null
push:
branches:
- master
- '*.*'

name: phpunit

jobs:
phpunit:
uses: spiral/gh-actions/.github/workflows/phpunit.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2']
stability: >-
['prefer-stable']
phpunit:
uses: spiral/gh-actions/.github/workflows/phpunit.yml@master
with:
extensions: sockets, grpc
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2']
stability: >-
['prefer-stable']
22 changes: 12 additions & 10 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
on:
push:
branches:
- master
pull_request: null
push:
branches:
- master
- '*.*'

name: static analysis

jobs:
psalm:
uses: spiral/gh-actions/.github/workflows/psalm.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.1']
psalm:
uses: spiral/gh-actions/.github/workflows/psalm.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.1']
54 changes: 0 additions & 54 deletions .github/workflows/run-tests.yml

This file was deleted.

51 changes: 0 additions & 51 deletions .github/workflows/static-analysis.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ docs
vendor
node_modules
.php-cs-fixer.cache
tests/runtime
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"spiral/tokenizer": "^3.0",
"spiral/roadrunner-bridge": "^2.0 || ^3.0",
"nette/php-generator": "^4.0",
"temporal/sdk": "^1.3 || ^2.0"
"temporal/sdk": "dev-interceptors"
},
"require-dev": {
"spiral/framework": "^3.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Bootloader/PrototypeBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Spiral\TemporalBridge\WorkflowManagerInterface;
use Temporal\Client\WorkflowClientInterface;

class PrototypeBootloader extends Bootloader
final class PrototypeBootloader extends Bootloader
{
protected const DEPENDENCIES = [
BasePrototypeBootloader::class,
Expand Down
65 changes: 41 additions & 24 deletions src/Bootloader/TemporalBridgeBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
use Spiral\Boot\AbstractKernel;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Boot\FinalizerInterface;
use Spiral\Config\ConfiguratorInterface;
use Spiral\Config\Patch\Append;
use Spiral\Console\Bootloader\ConsoleBootloader;
use Spiral\Core\Container\Autowire;
use Spiral\Core\FactoryInterface;
use Spiral\RoadRunnerBridge\Bootloader\RoadRunnerBootloader;
use Spiral\TemporalBridge\Commands;
use Spiral\TemporalBridge\Config\TemporalConfig;
use Spiral\TemporalBridge\DeclarationLocator;
use Spiral\TemporalBridge\DeclarationLocatorInterface;
use Spiral\TemporalBridge\Dispatcher;
use Spiral\TemporalBridge\Preset\PresetRegistry;
use Spiral\TemporalBridge\Preset\PresetRegistryInterface;
use Spiral\TemporalBridge\WorkerFactory;
use Spiral\TemporalBridge\WorkerFactoryInterface;
use Spiral\TemporalBridge\WorkersRegistry;
use Spiral\TemporalBridge\WorkersRegistryInterface;
use Spiral\TemporalBridge\Workflow\WorkflowManager;
Expand All @@ -33,22 +36,27 @@
use Temporal\Client\WorkflowClientInterface;
use Temporal\DataConverter\DataConverter;
use Temporal\DataConverter\DataConverterInterface;
use Temporal\Interceptor\PipelineProvider;
use Temporal\Interceptor\SimplePipelineProvider;
use Temporal\Internal\Interceptor\Interceptor;
use Temporal\Worker\Transport\Goridge;
use Temporal\Worker\WorkerFactoryInterface;
use Temporal\Worker\WorkerFactoryInterface as TemporalWorkerFactoryInterface;
use Temporal\Worker\WorkerOptions;
use Temporal\WorkerFactory;
use Temporal\WorkerFactory as TemporalWorkerFactory;

class TemporalBridgeBootloader extends Bootloader
{
protected const SINGLETONS = [
WorkflowPresetLocatorInterface::class => [self::class, 'initWorkflowPresetLocator'],
WorkflowManagerInterface::class => WorkflowManager::class,
WorkerFactoryInterface::class => [self::class, 'initWorkerFactory'],
TemporalWorkerFactoryInterface::class => [self::class, 'initWorkerFactory'],
DeclarationLocatorInterface::class => [self::class, 'initDeclarationLocator'],
WorkflowClientInterface::class => [self::class, 'initWorkflowClient'],
WorkersRegistryInterface::class => [self::class, 'initWorkersRegistry'],
WorkersRegistryInterface::class => WorkersRegistry::class,
PresetRegistryInterface::class => PresetRegistry::class,
DataConverterInterface::class => [self::class, 'initDataConverter'],
WorkerFactoryInterface::class => WorkerFactory::class,
PipelineProvider::class => [self::class, 'initPipelineProvider'],
];

protected const DEPENDENCIES = [
Expand All @@ -57,15 +65,15 @@ class TemporalBridgeBootloader extends Bootloader
];

public function __construct(
private readonly ConfiguratorInterface $config
private readonly ConfiguratorInterface $config,
) {
}

public function init(
AbstractKernel $kernel,
EnvironmentInterface $env,
ConsoleBootloader $console,
FactoryInterface $factory
FactoryInterface $factory,
): void {
$this->initConfig($env);

Expand All @@ -83,7 +91,7 @@ public function addWorkerOptions(string $worker, WorkerOptions $options): void

protected function initWorkflowPresetLocator(
FactoryInterface $factory,
ClassesInterface $classes
ClassesInterface $classes,
): WorkflowPresetLocatorInterface {
return new WorkflowPresetLocator(
factory: $factory,
Expand All @@ -99,20 +107,22 @@ protected function initConfig(EnvironmentInterface $env): void
[
'address' => $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233'),
'namespace' => 'App\\Workflow',
'defaultWorker' => (string)$env->get('TEMPORAL_TASK_QUEUE', WorkerFactoryInterface::DEFAULT_TASK_QUEUE),
'defaultWorker' => (string)$env->get('TEMPORAL_TASK_QUEUE', TemporalWorkerFactory::DEFAULT_TASK_QUEUE),
'workers' => [],
]
],
);
}

protected function initWorkflowClient(
TemporalConfig $config,
DataConverterInterface $dataConverter
DataConverterInterface $dataConverter,
PipelineProvider $pipelineProvider,
): WorkflowClientInterface {
return WorkflowClient::create(
return new WorkflowClient(
serviceClient: ServiceClient::create($config->getAddress()),
options: (new ClientOptions())->withNamespace($config->getTemporalNamespace()),
converter: $dataConverter
converter: $dataConverter,
interceptorProvider: $pipelineProvider,
);
}

Expand All @@ -122,28 +132,35 @@ protected function initDataConverter(): DataConverterInterface
}

protected function initWorkerFactory(
DataConverterInterface $dataConverter
): WorkerFactoryInterface {
return new WorkerFactory(
DataConverterInterface $dataConverter,
): TemporalWorkerFactoryInterface {
return new TemporalWorkerFactory(
dataConverter: $dataConverter,
rpc: Goridge::create()
);
}

protected function initDeclarationLocator(
ClassesInterface $classes
ClassesInterface $classes,
): DeclarationLocatorInterface {
return new \Spiral\TemporalBridge\DeclarationLocator(
return new DeclarationLocator(
classes: $classes,
reader: new AttributeReader()
);
}

protected function initWorkersRegistry(
WorkerFactoryInterface $workerFactory,
FinalizerInterface $finalizer,
TemporalConfig $config
): WorkersRegistryInterface {
return new WorkersRegistry($workerFactory, $finalizer, $config);
protected function initPipelineProvider(TemporalConfig $config, FactoryInterface $factory): PipelineProvider
{
/** @var Interceptor[] $interceptors */
$interceptors = \array_map(
static fn (mixed $interceptor) => match (true) {
\is_string($interceptor) => $factory->make($interceptor),
$interceptor instanceof Autowire => $interceptor->resolve($factory),
default => $interceptor
},
$config->getInterceptors(),
);

return new SimplePipelineProvider($interceptors);
}
}
7 changes: 6 additions & 1 deletion src/Commands/MakePresetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
use Spiral\TemporalBridge\Generator\Generator;
use Spiral\TemporalBridge\Preset\PresetRegistryInterface;
use Spiral\TemporalBridge\WorkflowPresetLocatorInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

final class MakePresetCommand extends Command
{
use WithContext;

protected const SIGNATURE = 'temporal:make-preset {preset : Workflow preset} {name : Workflow name}';
protected const NAME = 'temporal:make-preset';
protected const DESCRIPTION = 'Make a new Temporal workflow preset';
protected const ARGUMENTS = [
['name', InputArgument::REQUIRED, 'Workflow name'],
['preset', InputArgument::REQUIRED, 'Workflow preset'],
];

public function perform(
Generator $generator,
Expand Down
6 changes: 5 additions & 1 deletion src/Commands/MakeWorkflowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
use Spiral\TemporalBridge\Generator\HandlerInterfaceGenerator;
use Spiral\TemporalBridge\Generator\WorkflowGenerator;
use Spiral\TemporalBridge\Generator\WorkflowInterfaceGenerator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

final class MakeWorkflowCommand extends Command
{
use WithContext;

protected const SIGNATURE = 'temporal:make-workflow {name : Workflow name}';
protected const NAME = 'temporal:make-workflow';
protected const DESCRIPTION = 'Make a new Temporal workflow';
protected const ARGUMENTS = [
['name', InputArgument::REQUIRED, 'Workflow name'],
];

public function perform(Generator $generator): int
{
Expand Down
Loading