@@ -22,10 +22,26 @@ import { OrchestrationQuery, ListInstanceIdsOptions, DEFAULT_PAGE_SIZE } from ".
2222import { Page , AsyncPageable , createAsyncPageable } from "../orchestration/page" ;
2323import { FailureDetails } from "../task/failure-details" ;
2424import { Logger , ConsoleLogger } from "../types/logger.type" ;
25+ import { StartOrchestrationOptions } from "../task/options" ;
2526
2627// Re-export MetadataGenerator for backward compatibility
2728export { MetadataGenerator } from "../utils/grpc-helper.util" ;
2829
30+ function mapToRecord ( tagsMap ?: { forEach : ( cb : ( value : string , key : string ) => void ) => void } ) :
31+ | Record < string , string >
32+ | undefined {
33+ if ( ! tagsMap ) {
34+ return ;
35+ }
36+
37+ const tags : Record < string , string > = { } ;
38+ tagsMap . forEach ( ( value , key ) => {
39+ tags [ key ] = value ;
40+ } ) ;
41+
42+ return Object . keys ( tags ) . length > 0 ? tags : undefined ;
43+ }
44+
2945/**
3046 * Options for creating a TaskHubGrpcClient.
3147 */
@@ -133,13 +149,46 @@ export class TaskHubGrpcClient {
133149 input ?: TInput ,
134150 instanceId ?: string ,
135151 startAt ?: Date ,
152+ ) : Promise < string > ;
153+ /**
154+ * Schedules a new orchestrator using the DurableTask client.
155+ *
156+ * @param {TOrchestrator | string } orchestrator - The orchestrator or the name of the orchestrator to be scheduled.
157+ * @param {TInput } input - Optional input for the orchestrator.
158+ * @param {StartOrchestrationOptions } options - Options for instance ID, start time, and tags.
159+ * @return {Promise<string> } A Promise resolving to the unique ID of the scheduled orchestrator instance.
160+ */
161+ async scheduleNewOrchestration (
162+ orchestrator : TOrchestrator | string ,
163+ input ?: TInput ,
164+ options ?: StartOrchestrationOptions ,
165+ ) : Promise < string > ;
166+ async scheduleNewOrchestration (
167+ orchestrator : TOrchestrator | string ,
168+ input ?: TInput ,
169+ instanceIdOrOptions ?: string | StartOrchestrationOptions ,
170+ startAt ?: Date ,
136171 ) : Promise < string > {
137172 let name ;
138173 if ( typeof orchestrator === "string" ) {
139174 name = orchestrator ;
140175 } else {
141176 name = getName ( orchestrator ) ;
142177 }
178+
179+ const instanceId =
180+ typeof instanceIdOrOptions === "string" || instanceIdOrOptions === undefined
181+ ? instanceIdOrOptions
182+ : instanceIdOrOptions . instanceId ;
183+ const scheduledStartAt =
184+ typeof instanceIdOrOptions === "string" || instanceIdOrOptions === undefined
185+ ? startAt
186+ : instanceIdOrOptions . startAt ;
187+ const tags =
188+ typeof instanceIdOrOptions === "string" || instanceIdOrOptions === undefined
189+ ? undefined
190+ : instanceIdOrOptions . tags ;
191+
143192 const req = new pb . CreateInstanceRequest ( ) ;
144193 req . setName ( name ) ;
145194 req . setInstanceid ( instanceId ?? randomUUID ( ) ) ;
@@ -148,11 +197,18 @@ export class TaskHubGrpcClient {
148197 i . setValue ( JSON . stringify ( input ) ) ;
149198
150199 const ts = new Timestamp ( ) ;
151- ts . fromDate ( new Date ( startAt ?. getTime ( ) ?? 0 ) ) ;
200+ ts . fromDate ( new Date ( scheduledStartAt ?. getTime ( ) ?? 0 ) ) ;
152201
153202 req . setInput ( i ) ;
154203 req . setScheduledstarttimestamp ( ts ) ;
155204
205+ if ( tags ) {
206+ const tagsMap = req . getTagsMap ( ) ;
207+ for ( const [ key , value ] of Object . entries ( tags ) ) {
208+ tagsMap . set ( key , value ) ;
209+ }
210+ }
211+
156212 this . _logger . info ( `Starting new ${ name } instance with ID = ${ req . getInstanceid ( ) } ` ) ;
157213
158214 const res = await callWithMetadata < pb . CreateInstanceRequest , pb . CreateInstanceResponse > (
@@ -779,6 +835,8 @@ export class TaskHubGrpcClient {
779835 ) ;
780836 }
781837
838+ const tags = mapToRecord ( protoState . getTagsMap ( ) ) ;
839+
782840 return new OrchestrationState (
783841 instanceId ,
784842 name ?? "" ,
@@ -789,6 +847,7 @@ export class TaskHubGrpcClient {
789847 serializedOutput ,
790848 serializedCustomStatus ,
791849 failureDetails ,
850+ tags ,
792851 ) ;
793852 }
794853}
0 commit comments