@@ -24,6 +24,9 @@ import { FailureDetails } from "../task/failure-details";
2424import { HistoryEvent } from "../orchestration/history-event" ;
2525import { convertProtoHistoryEvent } from "../utils/history-event-converter" ;
2626import { Logger , ConsoleLogger } from "../types/logger.type" ;
27+ import { StartOrchestrationOptions } from "../task/options" ;
28+ import { mapToRecord } from "../utils/tags.util" ;
29+ import { populateTagsMap } from "../utils/pb-helper.util" ;
2730
2831// Re-export MetadataGenerator for backward compatibility
2932export { MetadataGenerator } from "../utils/grpc-helper.util" ;
@@ -135,13 +138,46 @@ export class TaskHubGrpcClient {
135138 input ?: TInput ,
136139 instanceId ?: string ,
137140 startAt ?: Date ,
141+ ) : Promise < string > ;
142+ /**
143+ * Schedules a new orchestrator using the DurableTask client.
144+ *
145+ * @param {TOrchestrator | string } orchestrator - The orchestrator or the name of the orchestrator to be scheduled.
146+ * @param {TInput } input - Optional input for the orchestrator.
147+ * @param {StartOrchestrationOptions } options - Options for instance ID, start time, and tags.
148+ * @return {Promise<string> } A Promise resolving to the unique ID of the scheduled orchestrator instance.
149+ */
150+ async scheduleNewOrchestration (
151+ orchestrator : TOrchestrator | string ,
152+ input ?: TInput ,
153+ options ?: StartOrchestrationOptions ,
154+ ) : Promise < string > ;
155+ async scheduleNewOrchestration (
156+ orchestrator : TOrchestrator | string ,
157+ input ?: TInput ,
158+ instanceIdOrOptions ?: string | StartOrchestrationOptions ,
159+ startAt ?: Date ,
138160 ) : Promise < string > {
139161 let name ;
140162 if ( typeof orchestrator === "string" ) {
141163 name = orchestrator ;
142164 } else {
143165 name = getName ( orchestrator ) ;
144166 }
167+
168+ const instanceId =
169+ typeof instanceIdOrOptions === "string" || instanceIdOrOptions === undefined
170+ ? instanceIdOrOptions
171+ : instanceIdOrOptions . instanceId ;
172+ const scheduledStartAt =
173+ typeof instanceIdOrOptions === "string" || instanceIdOrOptions === undefined
174+ ? startAt
175+ : instanceIdOrOptions . startAt ;
176+ const tags =
177+ typeof instanceIdOrOptions === "string" || instanceIdOrOptions === undefined
178+ ? undefined
179+ : instanceIdOrOptions . tags ;
180+
145181 const req = new pb . CreateInstanceRequest ( ) ;
146182 req . setName ( name ) ;
147183 req . setInstanceid ( instanceId ?? randomUUID ( ) ) ;
@@ -150,11 +186,13 @@ export class TaskHubGrpcClient {
150186 i . setValue ( JSON . stringify ( input ) ) ;
151187
152188 const ts = new Timestamp ( ) ;
153- ts . fromDate ( new Date ( startAt ?. getTime ( ) ?? 0 ) ) ;
189+ ts . fromDate ( new Date ( scheduledStartAt ?. getTime ( ) ?? 0 ) ) ;
154190
155191 req . setInput ( i ) ;
156192 req . setScheduledstarttimestamp ( ts ) ;
157193
194+ populateTagsMap ( req . getTagsMap ( ) , tags ) ;
195+
158196 this . _logger . info ( `Starting new ${ name } instance with ID = ${ req . getInstanceid ( ) } ` ) ;
159197
160198 const res = await callWithMetadata < pb . CreateInstanceRequest , pb . CreateInstanceResponse > (
@@ -569,14 +607,15 @@ export class TaskHubGrpcClient {
569607 * @example
570608 * ```typescript
571609 * // Iterate over all matching instances
610+ * const logger = new ConsoleLogger();
572611 * const pageable = client.getAllInstances({ statuses: [OrchestrationStatus.COMPLETED] });
573612 * for await (const instance of pageable) {
574- * console.log (instance.instanceId);
613+ * logger.info (instance.instanceId);
575614 * }
576615 *
577616 * // Iterate over pages
578617 * for await (const page of pageable.asPages()) {
579- * console.log (`Page has ${page.values.length} items`);
618+ * logger.info (`Page has ${page.values.length} items`);
580619 * }
581620 * ```
582621 *
@@ -852,6 +891,8 @@ export class TaskHubGrpcClient {
852891 ) ;
853892 }
854893
894+ const tags = mapToRecord ( protoState . getTagsMap ( ) ) ;
895+
855896 return new OrchestrationState (
856897 instanceId ,
857898 name ?? "" ,
@@ -862,6 +903,7 @@ export class TaskHubGrpcClient {
862903 serializedOutput ,
863904 serializedCustomStatus ,
864905 failureDetails ,
906+ tags ,
865907 ) ;
866908 }
867909}
0 commit comments