Skip to content

Commit 168e606

Browse files
committed
refactor(nexus): remove NexusEnvironment; update task handling and client integration
1 parent 44248d1 commit 168e606

22 files changed

Lines changed: 259 additions & 219 deletions

src/Internal/Nexus/NexusContext.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Temporal\Internal\Nexus;
1313

14+
use Temporal\Client\WorkflowClientInterface;
1415
use Temporal\Internal\Interceptor\Pipeline;
1516
use Temporal\Nexus\Handler\OperationCancelDetails;
1617
use Temporal\Nexus\Handler\OperationContext;
@@ -22,22 +23,23 @@
2223
*
2324
* {@see $current} (the handler-side {@see OperationContext} with links/headers/deadline) is the
2425
* one invariant of a dispatch — it is always present. The remaining fields are conditional:
25-
* {@see $operation}/{@see $environment} are null until the worker environment is bound;
26+
* {@see $operation} is null until the worker environment is bound; {@see $workflowClient} is null
27+
* unless a WorkflowClient was supplied to the WorkerFactory;
2628
* {@see $startDetails} and {@see $cancelDetails} are mutually exclusive (a dispatch is a start
2729
* XOR a cancel); {@see $outboundPipeline} is null when no interceptor pipeline applies. The
28-
* {@see $environment} (WorkflowClient) and {@see $outboundPipeline} are internal plumbing and
30+
* {@see $workflowClient} and {@see $outboundPipeline} are internal plumbing and
2931
* should not be consumed by user code.
3032
*/
3133
final class NexusContext
3234
{
3335
/**
3436
* @param null|Pipeline<\Temporal\Interceptor\NexusOperationOutboundCallsInterceptor, mixed> $outboundPipeline
35-
* @internal $environment and $outboundPipeline are framework plumbing.
37+
* @internal $workflowClient and $outboundPipeline are framework plumbing.
3638
*/
3739
public function __construct(
3840
public readonly OperationContext $current,
3941
public readonly ?NexusOperationContext $operation = null,
40-
public readonly ?NexusEnvironment $environment = null,
42+
public readonly ?WorkflowClientInterface $workflowClient = null,
4143
public readonly ?OperationStartDetails $startDetails = null,
4244
public readonly ?OperationCancelDetails $cancelDetails = null,
4345
public readonly ?Pipeline $outboundPipeline = null,

src/Internal/Nexus/NexusEnvironment.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/Internal/Nexus/NexusTaskHandler.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Temporal\Nexus\Handler\SyncOperationStartResult;
2323
use Temporal\Nexus\Header as NexusHeader;
2424
use Temporal\Nexus\LinkParser;
25+
use Temporal\Nexus\NexusOperationContext;
2526
use Temporal\Api\Common\V1\Payload;
2627
use Temporal\Api\Common\V1\Payloads;
2728
use Temporal\Api\Nexus\V1\CancelOperationRequest;
@@ -30,6 +31,7 @@
3031
use Temporal\Api\Nexus\V1\Response;
3132
use Temporal\Api\Nexus\V1\StartOperationRequest;
3233
use Temporal\Api\Nexus\V1\StartOperationResponse;
34+
use Temporal\Client\WorkflowClientInterface;
3335
use Temporal\DataConverter\DataConverterInterface;
3436
use Temporal\Interceptor\PipelineProvider;
3537
use Temporal\Interceptor\SimplePipelineProvider;
@@ -57,7 +59,7 @@ public function __construct(
5759
private readonly DataConverterInterface $dataConverter,
5860
private readonly bool $includeTracebackInFailure = true,
5961
private readonly PipelineProvider $interceptorProvider = new SimplePipelineProvider(),
60-
private readonly ?NexusEnvironment $environment = null,
62+
private readonly ?WorkflowClientInterface $workflowClient = null,
6163
) {}
6264

6365
/**
@@ -85,8 +87,11 @@ public static function deadlineFromHeaders(array $headers): ?\DateTimeImmutable
8587
}
8688
}
8789

88-
public function handleStartOperation(Request $request, ?MethodCanceller $methodCanceller = null): Response
89-
{
90+
public function handleStartOperation(
91+
Request $request,
92+
?MethodCanceller $methodCanceller = null,
93+
NexusOperationContext $operationContext = new NexusOperationContext(),
94+
): Response {
9095
$startRequest = $request->getStartOperation();
9196
\assert($startRequest instanceof StartOperationRequest);
9297

@@ -125,7 +130,8 @@ public function handleStartOperation(Request $request, ?MethodCanceller $methodC
125130
$context,
126131
$details,
127132
$input,
128-
$this->environment,
133+
$this->workflowClient,
134+
$operationContext,
129135
);
130136

131137
$startResponse = new StartOperationResponse();
@@ -174,8 +180,10 @@ public function handleStartOperation(Request $request, ?MethodCanceller $methodC
174180
}
175181
}
176182

177-
public function handleCancelOperation(Request $request): Response
178-
{
183+
public function handleCancelOperation(
184+
Request $request,
185+
NexusOperationContext $operationContext = new NexusOperationContext(),
186+
): Response {
179187
$cancelRequest = $request->getCancelOperation();
180188
\assert($cancelRequest instanceof CancelOperationRequest);
181189

@@ -202,7 +210,8 @@ public function handleCancelOperation(Request $request): Response
202210
$this->getServiceHandler()->cancelOperation(
203211
$context,
204212
$details,
205-
$this->environment,
213+
$this->workflowClient,
214+
$operationContext,
206215
);
207216

208217
$response = new Response();

src/Internal/ServiceContainer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Spiral\Attributes\ReaderInterface;
16+
use Temporal\Client\WorkflowClientInterface;
1617
use Temporal\DataConverter\DataConverterInterface;
1718
use Temporal\Exception\ExceptionInterceptorInterface;
1819
use Temporal\Interceptor\PipelineProvider;
@@ -23,7 +24,6 @@
2324
use Temporal\Internal\Declaration\Reader\ActivityReader;
2425
use Temporal\Internal\Declaration\Reader\NexusServiceReader;
2526
use Temporal\Internal\Declaration\Reader\WorkflowReader;
26-
use Temporal\Internal\Nexus\NexusEnvironment;
2727
use Temporal\Internal\Nexus\NexusInvocationRegistry;
2828
use Temporal\Internal\Nexus\NexusTaskHandler;
2929
use Temporal\Internal\Marshaller\MarshallerInterface;
@@ -63,7 +63,7 @@ public function __construct(
6363
public readonly ExceptionInterceptorInterface $exceptionInterceptor,
6464
public readonly PipelineProvider $interceptorProvider,
6565
public readonly LoggerInterface $logger,
66-
public readonly ?NexusEnvironment $nexusEnvironment = null,
66+
public readonly ?WorkflowClientInterface $workflowClient = null,
6767
) {
6868
$this->workflows = new WorkflowCollection();
6969
$this->activities = new ActivityCollection();
@@ -73,7 +73,7 @@ public function __construct(
7373
$this->nexusServices,
7474
$this->dataConverter,
7575
interceptorProvider: $this->interceptorProvider,
76-
environment: $nexusEnvironment,
76+
workflowClient: $workflowClient,
7777
);
7878
$this->running = new ProcessCollection();
7979
$this->workflowsReader = new WorkflowReader($this->reader);
@@ -86,7 +86,7 @@ public static function fromWorkerFactory(
8686
ExceptionInterceptorInterface $exceptionInterceptor,
8787
PipelineProvider $interceptorProvider,
8888
LoggerInterface $logger,
89-
?NexusEnvironment $nexusEnvironment = null,
89+
?WorkflowClientInterface $workflowClient = null,
9090
): self {
9191
return new self(
9292
$worker,
@@ -99,7 +99,7 @@ public static function fromWorkerFactory(
9999
$exceptionInterceptor,
100100
$interceptorProvider,
101101
$logger,
102-
$nexusEnvironment,
102+
$workflowClient,
103103
);
104104
}
105105
}

src/Internal/Transport/Router/CancelNexusOperation.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use Temporal\Api\Nexus\V1\CancelOperationRequest;
1616
use Temporal\Api\Nexus\V1\Request;
1717
use Temporal\DataConverter\EncodedValues;
18+
use Temporal\Internal\Marshaller\MarshallerInterface;
1819
use Temporal\Internal\Nexus\NexusHandlerErrorException;
1920
use Temporal\Internal\Nexus\NexusTaskHandler;
21+
use Temporal\Nexus\NexusOperationContext;
2022
use Temporal\Worker\Transport\Command\ServerRequestInterface;
2123

2224
/**
@@ -41,13 +43,19 @@ final class CancelNexusOperation extends Route
4143
{
4244
public function __construct(
4345
private readonly NexusTaskHandler $taskHandler,
46+
private readonly MarshallerInterface $marshaller,
4447
) {}
4548

4649
public function handle(ServerRequestInterface $request, array $headers, Deferred $resolver): void
4750
{
4851
try {
49-
$protoRequest = self::buildProtoRequest($request->getOptions());
50-
$this->taskHandler->handleCancelOperation($protoRequest);
52+
$options = $request->getOptions();
53+
$operationContext = $this->marshaller->unmarshal($options, new NexusOperationContext());
54+
$protoRequest = self::buildProtoRequest($options);
55+
$this->taskHandler->handleCancelOperation(
56+
$protoRequest,
57+
$operationContext,
58+
);
5159
$resolver->resolve(EncodedValues::fromValues([]));
5260
} catch (NexusHandlerErrorException $e) {
5361
// Unwrap the proto-shaped wrapper produced by NexusTaskHandler — the

src/Internal/Transport/Router/InvokeNexusOperation.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
use Temporal\DataConverter\DataConverterInterface;
2020
use Temporal\DataConverter\EncodedValues;
2121
use Temporal\DataConverter\ValuesInterface;
22+
use Temporal\Internal\Marshaller\MarshallerInterface;
2223
use Temporal\Internal\Nexus\NexusHandlerErrorException;
2324
use Temporal\Internal\Nexus\NexusInvocationRegistry;
2425
use Temporal\Internal\Nexus\NexusLinkConverter;
2526
use Temporal\Internal\Nexus\NexusTaskHandler;
2627
use Temporal\Nexus\Handler\MethodCanceller;
2728
use Temporal\Nexus\LinkParser;
29+
use Temporal\Nexus\NexusOperationContext;
2830
use Temporal\Worker\Transport\Command\Client\NexusOperationStarted;
2931
use Temporal\Worker\Transport\Command\ServerRequestInterface;
3032

@@ -34,12 +36,14 @@ public function __construct(
3436
private readonly NexusTaskHandler $taskHandler,
3537
private readonly NexusInvocationRegistry $invocations,
3638
private readonly DataConverterInterface $dataConverter,
39+
private readonly MarshallerInterface $marshaller,
3740
) {}
3841

3942
public function handle(ServerRequestInterface $request, array $headers, Deferred $resolver): void
4043
{
4144
$options = $request->getOptions();
4245
$invocationId = (int) ($options['invocationId'] ?? 0);
46+
$operationContext = $this->marshaller->unmarshal($options, new NexusOperationContext());
4347

4448
$canceller = null;
4549
if ($invocationId !== 0) {
@@ -49,7 +53,11 @@ public function handle(ServerRequestInterface $request, array $headers, Deferred
4953

5054
try {
5155
$protoRequest = self::buildProtoRequest($options, $request->getPayloads());
52-
$response = $this->taskHandler->handleStartOperation($protoRequest, $canceller);
56+
$response = $this->taskHandler->handleStartOperation(
57+
$protoRequest,
58+
$canceller,
59+
$operationContext,
60+
);
5361

5462
$startResponse = $response->getStartOperation();
5563
\assert($startResponse !== null);

src/Nexus/Handler/Internal/HandlerInterface.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
namespace Temporal\Nexus\Handler\Internal;
1313

14+
use Temporal\Client\WorkflowClientInterface;
1415
use Temporal\DataConverter\ValuesInterface;
15-
use Temporal\Internal\Nexus\NexusEnvironment;
1616
use Temporal\Nexus\Exception\HandlerException;
1717
use Temporal\Nexus\Exception\OperationException;
1818
use Temporal\Nexus\Handler\OperationCancelDetails;
1919
use Temporal\Nexus\Handler\OperationContext;
2020
use Temporal\Nexus\Handler\OperationStartDetails;
2121
use Temporal\Nexus\Handler\OperationStartResult;
22+
use Temporal\Nexus\NexusOperationContext;
2223

2324
/**
2425
* @internal
@@ -37,7 +38,8 @@ public function startOperation(
3738
OperationContext $context,
3839
OperationStartDetails $details,
3940
ValuesInterface $input,
40-
?NexusEnvironment $environment = null,
41+
?WorkflowClientInterface $workflowClient = null,
42+
NexusOperationContext $operationContext = new NexusOperationContext(),
4143
): OperationStartResult;
4244

4345
/**
@@ -55,6 +57,7 @@ public function startOperation(
5557
public function cancelOperation(
5658
OperationContext $context,
5759
OperationCancelDetails $details,
58-
?NexusEnvironment $environment = null,
60+
?WorkflowClientInterface $workflowClient = null,
61+
NexusOperationContext $operationContext = new NexusOperationContext(),
5962
): void;
6063
}

src/Nexus/Handler/Internal/ServiceHandler.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Temporal\Nexus\Handler\Internal;
1313

1414
use Temporal\Api\Common\V1\Payloads;
15+
use Temporal\Client\WorkflowClientInterface;
1516
use Temporal\DataConverter\DataConverterInterface;
1617
use Temporal\DataConverter\EncodedValues;
1718
use Temporal\DataConverter\ValuesInterface;
@@ -23,7 +24,6 @@
2324
use Temporal\Interceptor\SimplePipelineProvider;
2425
use Temporal\Internal\Declaration\NexusServiceInstance;
2526
use Temporal\Internal\Nexus\NexusContext;
26-
use Temporal\Internal\Nexus\NexusEnvironment;
2727
use Temporal\Nexus\Exception\ErrorType;
2828
use Temporal\Nexus\Exception\HandlerException;
2929
use Temporal\Nexus\Exception\InvalidArgumentException;
@@ -92,7 +92,8 @@ public function startOperation(
9292
OperationContext $context,
9393
OperationStartDetails $details,
9494
ValuesInterface $input,
95-
?NexusEnvironment $environment = null,
95+
?WorkflowClientInterface $workflowClient = null,
96+
NexusOperationContext $operationContext = new NexusOperationContext(),
9697
): OperationStartResult {
9798
[$instance, $handler] = $this->resolveHandler($context);
9899

@@ -119,8 +120,8 @@ public function startOperation(
119120
}
120121

121122
Nexus::setCurrentContext(new NexusContext(
122-
operation: self::buildPublicOperationContext($environment),
123-
environment: $environment,
123+
operation: self::publicOperationContext($operationContext),
124+
workflowClient: $workflowClient,
124125
current: $contextWithServiceDefinition,
125126
startDetails: $details,
126127
outboundPipeline: $this->interceptorProvider
@@ -160,7 +161,8 @@ public function startOperation(
160161
public function cancelOperation(
161162
OperationContext $context,
162163
OperationCancelDetails $details,
163-
?NexusEnvironment $environment = null,
164+
?WorkflowClientInterface $workflowClient = null,
165+
NexusOperationContext $operationContext = new NexusOperationContext(),
164166
): void {
165167
[$instance, $handler] = $this->resolveHandler($context);
166168

@@ -179,8 +181,8 @@ public function cancelOperation(
179181
$contextWithServiceDefinition = $context->withServiceDefinition($instance->prototype);
180182

181183
Nexus::setCurrentContext(new NexusContext(
182-
operation: self::buildPublicOperationContext($environment),
183-
environment: $environment,
184+
operation: self::publicOperationContext($operationContext),
185+
workflowClient: $workflowClient,
184186
current: $contextWithServiceDefinition,
185187
cancelDetails: $details,
186188
outboundPipeline: $this->interceptorProvider
@@ -201,12 +203,12 @@ static function (CancelOperationInput $input) use ($handler): void {
201203
}
202204
}
203205

204-
private static function buildPublicOperationContext(?NexusEnvironment $environment): ?NexusOperationContext
206+
private static function publicOperationContext(NexusOperationContext $operationContext): ?NexusOperationContext
205207
{
206-
if ($environment === null) {
208+
if ($operationContext->namespace === '' || $operationContext->taskQueue === '') {
207209
return null;
208210
}
209-
return new NexusOperationContext($environment->namespace, $environment->taskQueue);
211+
return $operationContext;
210212
}
211213

212214
/**

0 commit comments

Comments
 (0)