11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4+ import * as grpc from "@grpc/grpc-js" ;
45import { StringValue } from "google-protobuf/google/protobuf/wrappers_pb" ;
56import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb" ;
67import * as pb from "../proto/orchestrator_service_pb" ;
@@ -16,13 +17,10 @@ import { OrchestrationStatus, toProtobuf } from "../orchestration/enum/orchestra
1617import { TimeoutError } from "../exception/timeout-error" ;
1718import { PurgeResult } from "../orchestration/orchestration-purge-result" ;
1819import { PurgeInstanceCriteria } from "../orchestration/orchestration-purge-criteria" ;
19- import * as grpc from "@grpc/ grpc-js " ;
20+ import { callWithMetadata , MetadataGenerator } from "../utils/ grpc-helper.util " ;
2021
21- /**
22- * A function that generates gRPC metadata for each call.
23- * This is used for adding authentication tokens, task hub names, and other per-call metadata.
24- */
25- export type MetadataGenerator = ( ) => grpc . Metadata | Promise < grpc . Metadata > ;
22+ // Re-export MetadataGenerator for backward compatibility
23+ export { MetadataGenerator } from "../utils/grpc-helper.util" ;
2624
2725export class TaskHubGrpcClient {
2826 private _stub : stubs . TaskHubSidecarServiceClient ;
@@ -48,39 +46,6 @@ export class TaskHubGrpcClient {
4846 this . _metadataGenerator = metadataGenerator ;
4947 }
5048
51- /**
52- * Helper to get metadata for gRPC calls.
53- */
54- private async _getMetadata ( ) : Promise < grpc . Metadata > {
55- if ( this . _metadataGenerator ) {
56- return await this . _metadataGenerator ( ) ;
57- }
58- return new grpc . Metadata ( ) ;
59- }
60-
61- /**
62- * Helper to promisify a gRPC call with metadata support.
63- */
64- private _callWithMetadata < TReq , TRes > (
65- method : (
66- req : TReq ,
67- metadata : grpc . Metadata ,
68- callback : ( error : grpc . ServiceError | null , response : TRes ) => void ,
69- ) => grpc . ClientUnaryCall ,
70- req : TReq ,
71- ) : Promise < TRes > {
72- return new Promise ( async ( resolve , reject ) => {
73- const metadata = await this . _getMetadata ( ) ;
74- method ( req , metadata , ( error , response ) => {
75- if ( error ) {
76- reject ( error ) ;
77- } else {
78- resolve ( response ) ;
79- }
80- } ) ;
81- } ) ;
82- }
83-
8449 async stop ( ) : Promise < void > {
8550 await this . _stub . close ( ) ;
8651
@@ -122,9 +87,10 @@ export class TaskHubGrpcClient {
12287
12388 console . log ( `Starting new ${ name } instance with ID = ${ req . getInstanceid ( ) } ` ) ;
12489
125- const res = await this . _callWithMetadata < pb . CreateInstanceRequest , pb . CreateInstanceResponse > (
90+ const res = await callWithMetadata < pb . CreateInstanceRequest , pb . CreateInstanceResponse > (
12691 this . _stub . startInstance . bind ( this . _stub ) ,
12792 req ,
93+ this . _metadataGenerator ,
12894 ) ;
12995
13096 return res . getInstanceid ( ) ;
@@ -148,9 +114,10 @@ export class TaskHubGrpcClient {
148114 req . setInstanceid ( instanceId ) ;
149115 req . setGetinputsandoutputs ( fetchPayloads ) ;
150116
151- const res = await this . _callWithMetadata < pb . GetInstanceRequest , pb . GetInstanceResponse > (
117+ const res = await callWithMetadata < pb . GetInstanceRequest , pb . GetInstanceResponse > (
152118 this . _stub . getInstance . bind ( this . _stub ) ,
153119 req ,
120+ this . _metadataGenerator ,
154121 ) ;
155122
156123 return newOrchestrationState ( req . getInstanceid ( ) , res ) ;
@@ -182,9 +149,10 @@ export class TaskHubGrpcClient {
182149 req . setGetinputsandoutputs ( fetchPayloads ) ;
183150
184151 try {
185- const callPromise = this . _callWithMetadata < pb . GetInstanceRequest , pb . GetInstanceResponse > (
152+ const callPromise = callWithMetadata < pb . GetInstanceRequest , pb . GetInstanceResponse > (
186153 this . _stub . waitForInstanceStart . bind ( this . _stub ) ,
187154 req ,
155+ this . _metadataGenerator ,
188156 ) ;
189157
190158 // Execute the request and wait for the first response or timeout
@@ -229,9 +197,10 @@ export class TaskHubGrpcClient {
229197 try {
230198 console . info ( `Waiting ${ timeout } seconds for instance ${ instanceId } to complete...` ) ;
231199
232- const callPromise = this . _callWithMetadata < pb . GetInstanceRequest , pb . GetInstanceResponse > (
200+ const callPromise = callWithMetadata < pb . GetInstanceRequest , pb . GetInstanceResponse > (
233201 this . _stub . waitForInstanceCompletion . bind ( this . _stub ) ,
234202 req ,
203+ this . _metadataGenerator ,
235204 ) ;
236205
237206 // Execute the request and wait for the first response or timeout
@@ -286,9 +255,10 @@ export class TaskHubGrpcClient {
286255
287256 console . log ( `Raising event '${ eventName } ' for instance '${ instanceId } '` ) ;
288257
289- await this . _callWithMetadata < pb . RaiseEventRequest , pb . RaiseEventResponse > (
258+ await callWithMetadata < pb . RaiseEventRequest , pb . RaiseEventResponse > (
290259 this . _stub . raiseEvent . bind ( this . _stub ) ,
291260 req ,
261+ this . _metadataGenerator ,
292262 ) ;
293263 }
294264
@@ -309,9 +279,10 @@ export class TaskHubGrpcClient {
309279
310280 console . log ( `Terminating '${ instanceId } '` ) ;
311281
312- await this . _callWithMetadata < pb . TerminateRequest , pb . TerminateResponse > (
282+ await callWithMetadata < pb . TerminateRequest , pb . TerminateResponse > (
313283 this . _stub . terminateInstance . bind ( this . _stub ) ,
314284 req ,
285+ this . _metadataGenerator ,
315286 ) ;
316287 }
317288
@@ -321,9 +292,10 @@ export class TaskHubGrpcClient {
321292
322293 console . log ( `Suspending '${ instanceId } '` ) ;
323294
324- await this . _callWithMetadata < pb . SuspendRequest , pb . SuspendResponse > (
295+ await callWithMetadata < pb . SuspendRequest , pb . SuspendResponse > (
325296 this . _stub . suspendInstance . bind ( this . _stub ) ,
326297 req ,
298+ this . _metadataGenerator ,
327299 ) ;
328300 }
329301
@@ -333,9 +305,10 @@ export class TaskHubGrpcClient {
333305
334306 console . log ( `Resuming '${ instanceId } '` ) ;
335307
336- await this . _callWithMetadata < pb . ResumeRequest , pb . ResumeResponse > (
308+ await callWithMetadata < pb . ResumeRequest , pb . ResumeResponse > (
337309 this . _stub . resumeInstance . bind ( this . _stub ) ,
338310 req ,
311+ this . _metadataGenerator ,
339312 ) ;
340313 }
341314
@@ -365,9 +338,10 @@ export class TaskHubGrpcClient {
365338
366339 console . log ( `Purging Instance '${ instanceId } '` ) ;
367340
368- res = await this . _callWithMetadata < pb . PurgeInstancesRequest , pb . PurgeInstancesResponse > (
341+ res = await callWithMetadata < pb . PurgeInstancesRequest , pb . PurgeInstancesResponse > (
369342 this . _stub . purgeInstances . bind ( this . _stub ) ,
370343 req ,
344+ this . _metadataGenerator ,
371345 ) ;
372346 } else {
373347 const purgeInstanceCriteria = value ;
@@ -394,9 +368,10 @@ export class TaskHubGrpcClient {
394368
395369 console . log ( "Purging Instance using purging criteria" ) ;
396370
397- const callPromise = this . _callWithMetadata < pb . PurgeInstancesRequest , pb . PurgeInstancesResponse > (
371+ const callPromise = callWithMetadata < pb . PurgeInstancesRequest , pb . PurgeInstancesResponse > (
398372 this . _stub . purgeInstances . bind ( this . _stub ) ,
399373 req ,
374+ this . _metadataGenerator ,
400375 ) ;
401376 // Execute the request and wait for the first response or timeout
402377 res = ( await Promise . race ( [
0 commit comments