Skip to content

Commit cf1645e

Browse files
committed
refactor(nexus): extract dispatch logic; simplify operation context handling
1 parent 168e606 commit cf1645e

2 files changed

Lines changed: 47 additions & 48 deletions

File tree

src/Nexus/Handler/Internal/ServiceHandler.php

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -119,29 +119,23 @@ public function startOperation(
119119
);
120120
}
121121

122-
Nexus::setCurrentContext(new NexusContext(
123-
operation: self::publicOperationContext($operationContext),
124-
workflowClient: $workflowClient,
125-
current: $contextWithServiceDefinition,
126-
startDetails: $details,
127-
outboundPipeline: $this->interceptorProvider
128-
->getPipeline(NexusOperationOutboundCallsInterceptor::class),
129-
));
130-
try {
131-
$result = $this->interceptorProvider
132-
->getPipeline(NexusOperationInboundCallsInterceptor::class)
133-
->with(
134-
static fn(StartOperationInput $input): OperationStartResult => $handler->start(
135-
$input->operationContext,
136-
$input->startDetails,
137-
$input->input,
138-
),
139-
/** @see NexusOperationInboundCallsInterceptor::startOperation() */
140-
'startOperation',
141-
)(new StartOperationInput($contextWithServiceDefinition, $details, $inputObject));
142-
} finally {
143-
Nexus::setCurrentContext(null);
144-
}
122+
$result = $this->dispatch(
123+
new NexusContext(
124+
operation: self::publicOperationContext($operationContext),
125+
workflowClient: $workflowClient,
126+
current: $contextWithServiceDefinition,
127+
startDetails: $details,
128+
outboundPipeline: $this->interceptorProvider
129+
->getPipeline(NexusOperationOutboundCallsInterceptor::class),
130+
),
131+
static fn(StartOperationInput $input): OperationStartResult => $handler->start(
132+
$input->operationContext,
133+
$input->startDetails,
134+
$input->input,
135+
),
136+
'startOperation',
137+
new StartOperationInput($contextWithServiceDefinition, $details, $inputObject),
138+
);
145139

146140
\assert($result instanceof OperationStartResult);
147141

@@ -180,27 +174,21 @@ public function cancelOperation(
180174

181175
$contextWithServiceDefinition = $context->withServiceDefinition($instance->prototype);
182176

183-
Nexus::setCurrentContext(new NexusContext(
184-
operation: self::publicOperationContext($operationContext),
185-
workflowClient: $workflowClient,
186-
current: $contextWithServiceDefinition,
187-
cancelDetails: $details,
188-
outboundPipeline: $this->interceptorProvider
189-
->getPipeline(NexusOperationOutboundCallsInterceptor::class),
190-
));
191-
try {
192-
$this->interceptorProvider
193-
->getPipeline(NexusOperationInboundCallsInterceptor::class)
194-
->with(
195-
static function (CancelOperationInput $input) use ($handler): void {
196-
$handler->cancel($input->operationContext, $input->cancelDetails);
197-
},
198-
/** @see NexusOperationInboundCallsInterceptor::cancelOperation() */
199-
'cancelOperation',
200-
)(new CancelOperationInput($contextWithServiceDefinition, $details));
201-
} finally {
202-
Nexus::setCurrentContext(null);
203-
}
177+
$this->dispatch(
178+
new NexusContext(
179+
operation: self::publicOperationContext($operationContext),
180+
workflowClient: $workflowClient,
181+
current: $contextWithServiceDefinition,
182+
cancelDetails: $details,
183+
outboundPipeline: $this->interceptorProvider
184+
->getPipeline(NexusOperationOutboundCallsInterceptor::class),
185+
),
186+
static function (CancelOperationInput $input) use ($handler): void {
187+
$handler->cancel($input->operationContext, $input->cancelDetails);
188+
},
189+
'cancelOperation',
190+
new CancelOperationInput($contextWithServiceDefinition, $details),
191+
);
204192
}
205193

206194
private static function publicOperationContext(NexusOperationContext $operationContext): ?NexusOperationContext
@@ -211,6 +199,21 @@ private static function publicOperationContext(NexusOperationContext $operationC
211199
return $operationContext;
212200
}
213201

202+
/**
203+
* @param non-empty-string $method
204+
*/
205+
private function dispatch(NexusContext $dispatchContext, \Closure $terminal, string $method, object $input): mixed
206+
{
207+
Nexus::setCurrentContext($dispatchContext);
208+
try {
209+
return $this->interceptorProvider
210+
->getPipeline(NexusOperationInboundCallsInterceptor::class)
211+
->with($terminal, $method)($input);
212+
} finally {
213+
Nexus::setCurrentContext(null);
214+
}
215+
}
216+
214217
/**
215218
* @return array{NexusServiceInstance, OperationHandlerInterface}
216219
*/

src/Nexus/WorkflowRunOperation.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public static function start(WorkflowHandle $handle, OperationStartDetails $deta
5959
$options = $options->withTaskQueue($info->taskQueue);
6060
}
6161

62-
// Token = ns+workflowId (stable across retries).
6362
$token = WorkflowRunOperationToken::generate(
6463
$info->namespace,
6564
$options->workflowId,
@@ -90,9 +89,6 @@ public static function start(WorkflowHandle $handle, OperationStartDetails $deta
9089
$stub = $client->newWorkflowStub($handle->workflowClass, $options);
9190
$run = $client->start($stub, ...$handle->args);
9291

93-
// Self-link to WORKFLOW_EXECUTION_STARTED event of the run we just started.
94-
// Caller server attaches it to NEXUS_OPERATION_STARTED so UI shows the
95-
// caller↔handler chain. Mirror of Java/TS/Python/Go SDK behaviour.
9692
Nexus::getCurrentOperationContext()->links->add(
9793
self::buildStartedEventSelfLink($info->namespace, $run->getExecution()),
9894
);

0 commit comments

Comments
 (0)