Skip to content

Commit

Permalink
Process a case when workflow is started but update failed
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Dec 16, 2024
1 parent 57c2e0a commit a3c2ace
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/Client/WorkflowClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,19 @@ public function updateWithStart(
);
$workflowStub->hasExecution() and throw new InvalidArgumentException(self::ERROR_WORKFLOW_START_DUPLICATION);

$handle = $this->getStarter()->updateWithStart(
[$execution, $handle] = $this->getStarter()->updateWithStart(
$workflowType,
$workflowStub->getOptions() ?? WorkflowOptions::new(),
$update,
$updateArgs,
$startArgs,
);

// todo: set execution if UpdateWorkflow was failed but WF was started
$workflowStub->setExecution($handle->getExecution());
$workflowStub->setExecution($execution);

return $handle;
return $handle instanceof \Throwable
? throw $handle
: $handle;
}

public function newWorkflowStub(
Expand Down
3 changes: 3 additions & 0 deletions src/Client/WorkflowStubInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
*/
interface WorkflowStubInterface extends WorkflowRunInterface
{
/**
* @return non-empty-string|null
*/
public function getWorkflowType(): ?string;

/**
Expand Down
31 changes: 20 additions & 11 deletions src/Internal/Client/WorkflowStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,23 @@ function (SignalWithStartInput $input): WorkflowExecution {
);
}

/**
* @param non-empty-string $workflowType
*
* @return array{WorkflowExecution, UpdateHandle|\Throwable}
*/
public function updateWithStart(
string $workflowType,
WorkflowOptions $options,
UpdateOptions $update,
array $updateArgs = [],
array $startArgs = [],
): UpdateHandle {
): array {
$arguments = EncodedValues::fromValues($startArgs, $this->converter);
$updateArguments = EncodedValues::fromValues($updateArgs, $this->converter);

return $this->interceptors->with(
function (UpdateWithStartInput $input): UpdateHandle {
function (UpdateWithStartInput $input): array {
$startRequest = $this->configureExecutionRequest(
new StartWorkflowExecutionRequest(),
$input->workflowStartInput,
Expand Down Expand Up @@ -216,15 +221,19 @@ function (UpdateWithStartInput $input): UpdateHandle {
$updateResponse = $responses[1]->getUpdateWorkflow();
\assert($updateResponse !== null);

$updateResult = (new \Temporal\Internal\Client\ResponseToResultMapper($this->converter))
->mapUpdateWorkflowResponse(
$updateResponse,
updateName: $input->updateInput->updateName,
workflowType: $input->workflowStartInput->workflowType,
workflowExecution: $execution,
);
try {
$updateResult = (new \Temporal\Internal\Client\ResponseToResultMapper($this->converter))
->mapUpdateWorkflowResponse(
$updateResponse,
updateName: $input->updateInput->updateName,
workflowType: $input->workflowStartInput->workflowType,
workflowExecution: $execution,
);
} catch (\RuntimeException $e) {
return [$execution, $e];
}

return new UpdateHandle(
return [$execution, new UpdateHandle(
client: $this->serviceClient,
clientOptions: $this->clientOptions,
converter: $this->converter,
Expand All @@ -234,7 +243,7 @@ function (UpdateWithStartInput $input): UpdateHandle {
resultType: $input->updateInput->resultType,
updateId: $updateResult->getReference()->updateId,
result: $updateResult->getResult(),
);
)];
},
/** @see WorkflowClientCallsInterceptor::updateWithStart() */
'updateWithStart',
Expand Down

0 comments on commit a3c2ace

Please sign in to comment.