@@ -81,6 +81,8 @@ import {
81
81
WithDefaults ,
82
82
} from './base-client' ;
83
83
import { mapAsyncIterable } from './iterators-utils' ;
84
+ import { WorkflowUpdateStage } from './workflow-update-stage' ;
85
+ import * as workflowUpdateStage from './workflow-update-stage' ;
84
86
85
87
/**
86
88
* A client side handle to a single Workflow instance.
@@ -140,8 +142,8 @@ export interface WorkflowHandle<T extends Workflow = Workflow> extends BaseWorkf
140
142
) : Promise < Ret > ;
141
143
142
144
/**
143
- * Start an Update and receive a handle to the Update.
144
- * The Update validator (if present) is run before the handle is returned.
145
+ * Start an Update and receive a handle to the Update. The Update validator (if present) is run
146
+ * before the handle is returned.
145
147
*
146
148
* @experimental Update is an experimental feature.
147
149
*
@@ -150,22 +152,41 @@ export interface WorkflowHandle<T extends Workflow = Workflow> extends BaseWorkf
150
152
* mean the update itself was timed out or cancelled.
151
153
*
152
154
* @param def an Update definition as returned from {@link defineUpdate}
153
- * @param options Update arguments
155
+ * @param options update arguments, and update lifecycle stage to wait for
156
+ *
157
+ * Currently, startUpdate always waits until a worker is accepting tasks for the workflow and the
158
+ * update is accepted or rejected, and the options object must be at least
159
+ * ```ts
160
+ * {
161
+ * waitForStage: WorkflowUpdateStage.ACCEPTED
162
+ * }
163
+ * ```
164
+ * If the update takes arguments, then the options object must additionally contain an `args`
165
+ * property with an array of argument values.
154
166
*
155
167
* @example
156
168
* ```ts
157
- * const updateHandle = await handle.startUpdate(incrementAndGetValueUpdate, { args: [2] });
169
+ * const updateHandle = await handle.startUpdate(incrementAndGetValueUpdate, {
170
+ * args: [2],
171
+ * waitForStage: WorkflowUpdateStage.ACCEPTED,
172
+ * });
158
173
* const updateResult = await updateHandle.result();
159
174
* ```
160
175
*/
161
176
startUpdate < Ret , Args extends [ any , ...any [ ] ] , Name extends string = string > (
162
177
def : UpdateDefinition < Ret , Args , Name > | string ,
163
- options : WorkflowUpdateOptions & { args : Args }
178
+ options : WorkflowUpdateOptions & {
179
+ args : Args ;
180
+ waitForStage : WorkflowUpdateStage . ACCEPTED ;
181
+ }
164
182
) : Promise < WorkflowUpdateHandle < Ret > > ;
165
183
166
184
startUpdate < Ret , Args extends [ ] , Name extends string = string > (
167
185
def : UpdateDefinition < Ret , Args , Name > | string ,
168
- options ?: WorkflowUpdateOptions & { args ?: Args }
186
+ options : WorkflowUpdateOptions & {
187
+ args ?: Args ;
188
+ waitForStage : WorkflowUpdateStage . ACCEPTED ;
189
+ }
169
190
) : Promise < WorkflowUpdateHandle < Ret > > ;
170
191
171
192
/**
@@ -782,15 +803,17 @@ export class WorkflowClient extends BaseClient {
782
803
* Used as the final function of the interceptor chain during startUpdate and executeUpdate.
783
804
*/
784
805
protected async _startUpdateHandler (
785
- waitForStage : temporal . api . enums . v1 . UpdateWorkflowExecutionLifecycleStage ,
806
+ waitForStage : WorkflowUpdateStage ,
786
807
input : WorkflowStartUpdateInput
787
808
) : Promise < WorkflowStartUpdateOutput > {
788
809
const updateId = input . options ?. updateId ?? uuid4 ( ) ;
789
810
const req : temporal . api . workflowservice . v1 . IUpdateWorkflowExecutionRequest = {
790
811
namespace : this . options . namespace ,
791
812
workflowExecution : input . workflowExecution ,
792
813
firstExecutionRunId : input . firstExecutionRunId ,
793
- waitPolicy : { lifecycleStage : waitForStage } ,
814
+ waitPolicy : {
815
+ lifecycleStage : workflowUpdateStage . toProtoEnum ( waitForStage ) ,
816
+ } ,
794
817
request : {
795
818
meta : {
796
819
updateId,
@@ -811,11 +834,7 @@ export class WorkflowClient extends BaseClient {
811
834
try {
812
835
do {
813
836
response = await this . workflowService . updateWorkflowExecution ( req ) ;
814
- } while (
815
- response . stage < waitForStage &&
816
- response . stage <
817
- temporal . api . enums . v1 . UpdateWorkflowExecutionLifecycleStage . UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_ACCEPTED
818
- ) ;
837
+ } while ( response . stage < waitForStage && response . stage < WorkflowUpdateStage . ACCEPTED ) ;
819
838
} catch ( err ) {
820
839
this . rethrowUpdateGrpcError ( err , 'Workflow Update failed' , input . workflowExecution ) ;
821
840
}
@@ -865,9 +884,7 @@ export class WorkflowClient extends BaseClient {
865
884
updateRef : { workflowExecution, updateId } ,
866
885
identity : this . options . identity ,
867
886
waitPolicy : {
868
- lifecycleStage :
869
- temporal . api . enums . v1 . UpdateWorkflowExecutionLifecycleStage
870
- . UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_COMPLETED ,
887
+ lifecycleStage : workflowUpdateStage . toProtoEnum ( WorkflowUpdateStage . COMPLETED ) ,
871
888
} ,
872
889
} ;
873
890
for ( ; ; ) {
@@ -1076,7 +1093,7 @@ export class WorkflowClient extends BaseClient {
1076
1093
} : WorkflowHandleOptions ) : WorkflowHandle < T > {
1077
1094
const _startUpdate = async < Ret , Args extends unknown [ ] > (
1078
1095
def : UpdateDefinition < Ret , Args > | string ,
1079
- waitForStage : temporal . api . enums . v1 . UpdateWorkflowExecutionLifecycleStage ,
1096
+ waitForStage : WorkflowUpdateStage ,
1080
1097
options ?: WorkflowUpdateOptions & { args ?: Args }
1081
1098
) : Promise < WorkflowUpdateHandle < Ret > > => {
1082
1099
const next = this . _startUpdateHandler . bind ( this , waitForStage ) ;
@@ -1098,12 +1115,7 @@ export class WorkflowClient extends BaseClient {
1098
1115
output . workflowRunId ,
1099
1116
output . outcome
1100
1117
) ;
1101
- if (
1102
- ! output . outcome &&
1103
- waitForStage ===
1104
- temporal . api . enums . v1 . UpdateWorkflowExecutionLifecycleStage
1105
- . UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_COMPLETED
1106
- ) {
1118
+ if ( ! output . outcome && waitForStage === WorkflowUpdateStage . COMPLETED ) {
1107
1119
await this . _pollForUpdateOutcome ( handle . updateId , input . workflowExecution ) ;
1108
1120
}
1109
1121
return handle ;
@@ -1162,25 +1174,18 @@ export class WorkflowClient extends BaseClient {
1162
1174
} ,
1163
1175
async startUpdate < Ret , Args extends any [ ] > (
1164
1176
def : UpdateDefinition < Ret , Args > | string ,
1165
- options ?: WorkflowUpdateOptions & { args ?: Args }
1177
+ options : WorkflowUpdateOptions & {
1178
+ args ?: Args ;
1179
+ waitForStage : WorkflowUpdateStage . ACCEPTED ;
1180
+ }
1166
1181
) : Promise < WorkflowUpdateHandle < Ret > > {
1167
- return await _startUpdate (
1168
- def ,
1169
- temporal . api . enums . v1 . UpdateWorkflowExecutionLifecycleStage
1170
- . UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_ACCEPTED ,
1171
- options
1172
- ) ;
1182
+ return await _startUpdate ( def , options . waitForStage , options ) ;
1173
1183
} ,
1174
1184
async executeUpdate < Ret , Args extends any [ ] > (
1175
1185
def : UpdateDefinition < Ret , Args > | string ,
1176
1186
options ?: WorkflowUpdateOptions & { args ?: Args }
1177
1187
) : Promise < Ret > {
1178
- const handle = await _startUpdate (
1179
- def ,
1180
- temporal . api . enums . v1 . UpdateWorkflowExecutionLifecycleStage
1181
- . UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_COMPLETED ,
1182
- options
1183
- ) ;
1188
+ const handle = await _startUpdate ( def , WorkflowUpdateStage . COMPLETED , options ) ;
1184
1189
return await handle . result ( ) ;
1185
1190
} ,
1186
1191
getUpdateHandle < Ret > ( updateId : string ) : WorkflowUpdateHandle < Ret > {
0 commit comments