Skip to content

Commit 22a8f1d

Browse files
authored
Workflow Update (#1277)
// An Update is defined similarly to Query and Signal. The second type parameter (input) is optional const myUpdate = wf.defineUpdate<string, [string]>('myUpdate'); // A handler is a sync or async function; the optional validator is always sync. wf.setHandler(myUpdate, handler, { validator }); // `await wfHandle.executeUpdate` to start an update and block until it completes or fails. // The signature resembles `WorkflowClient.start` in that the arguments are supplied // in the optional options object // (as opposed to the variadic signatures of wfHandle.query and wfHandle.signal) // An error is thrown if the RPC times out (we do not poll for this call) // or if the validator rejects, or the handler throws ApplicationFailure. const updateResult = await handle.executeUpdate(myUpdate, { args: ['a'] }); // `startUpdate` to block until Accepted and obtain a handle to the Update. Signature the same as `executeUpdate`. const updateHandle = await handle.startUpdate(myUpdate, { args: ['a'] }); // Blocking call to fetch result, with polling. // Validation failures or ApplicationFailure in the handler would throw here. const updateResult = await updateHandle.result();
1 parent 28d4de3 commit 22a8f1d

27 files changed

+1312
-112
lines changed

docs/activation-in-debug-mode.mermaid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ sequenceDiagram
1212
end
1313
Core->>-MT: Respond with Activation
1414
MT->>MT: Decode Payloads
15-
loop patches, signals, completions, queries as jobs
15+
loop patches, signals, updates, completions, queries as jobs
1616
MT->>VM: Activate(jobs)
1717
VM->>VM: Run Microtasks
1818
MT->>VM: Try Unblock Conditions

docs/activation.mermaid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ sequenceDiagram
1414
Core->>-MT: Respond with Activation
1515
MT->>MT: Decode Payloads
1616
MT->>+WT: Run Workflow Activation
17-
loop patches, signals, completions, queries as jobs
17+
loop patches, signals, updates, completions, queries as jobs
1818
WT->>VM: Activate(jobs)
1919
VM->>VM: Run Microtasks
2020
WT->>VM: Try Unblock Conditions

packages/client/src/errors.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ export class WorkflowFailedError extends Error {
3535
}
3636
}
3737

38+
/**
39+
* Thrown by the client while waiting on Workflow Update result if Update
40+
* completes with failure.
41+
*/
42+
@SymbolBasedInstanceOfError('WorkflowUpdateFailedError')
43+
export class WorkflowUpdateFailedError extends Error {
44+
public constructor(message: string, public readonly cause: TemporalFailure | undefined) {
45+
super(message);
46+
}
47+
}
48+
3849
/**
3950
* Thrown the by client while waiting on Workflow execution result if Workflow
4051
* continues as new.

packages/client/src/interceptors.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
TerminateWorkflowExecutionResponse,
1414
WorkflowExecution,
1515
} from './types';
16-
import { CompiledWorkflowOptions } from './workflow-options';
16+
import { CompiledWorkflowOptions, WorkflowUpdateOptions } from './workflow-options';
1717

18-
export { Next, Headers };
18+
export { Headers, Next };
1919

2020
/** Input for WorkflowClientInterceptor.start */
2121
export interface WorkflowStartInput {
@@ -25,6 +25,23 @@ export interface WorkflowStartInput {
2525
readonly options: CompiledWorkflowOptions;
2626
}
2727

28+
/** Input for WorkflowClientInterceptor.update */
29+
export interface WorkflowStartUpdateInput {
30+
readonly updateName: string;
31+
readonly args: unknown[];
32+
readonly workflowExecution: WorkflowExecution;
33+
readonly firstExecutionRunId?: string;
34+
readonly headers: Headers;
35+
readonly options: WorkflowUpdateOptions;
36+
}
37+
38+
/** Output for WorkflowClientInterceptor.startUpdate */
39+
export interface WorkflowStartUpdateOutput {
40+
readonly updateId: string;
41+
readonly workflowRunId: string;
42+
readonly outcome?: temporal.api.update.v1.IOutcome;
43+
}
44+
2845
/** Input for WorkflowClientInterceptor.signal */
2946
export interface WorkflowSignalInput {
3047
readonly signalName: string;
@@ -81,6 +98,15 @@ export interface WorkflowClientInterceptor {
8198
* {@link signalWithStart} most likely needs to be implemented too
8299
*/
83100
start?: (input: WorkflowStartInput, next: Next<this, 'start'>) => Promise<string /* runId */>;
101+
/**
102+
* Intercept a service call to updateWorkflowExecution
103+
*
104+
* @experimental Update is an experimental feature.
105+
*/
106+
startUpdate?: (
107+
input: WorkflowStartUpdateInput,
108+
next: Next<this, 'startUpdate'>
109+
) => Promise<WorkflowStartUpdateOutput>;
84110
/**
85111
* Intercept a service call to signalWorkflowExecution
86112
*

0 commit comments

Comments
 (0)