@@ -50,6 +50,8 @@ export class Controller {
50
50
private pubSubIntegrationEventsClient : PubSubClient < PubSubIntegrationsEventMessage > | null =
51
51
null ;
52
52
private integrationName : string = 'UNKNOWN' ;
53
+
54
+ // used for garbage collection reasons, to prevent long running promises from getting canceled
53
55
private streamingPromises = new Map < string , Promise < void > > ( ) ;
54
56
55
57
constructor ( adapter : Adapter , contactCache : ContactCache | null ) {
@@ -288,10 +290,11 @@ export class Controller {
288
290
}
289
291
290
292
const timestamp = Date . now ( ) ;
293
+ const orderingKey = `${ userId } :${ timestamp } ` ;
291
294
292
295
infoLogger (
293
296
'streamContacts' ,
294
- `Starting contact streaming ${ timestamp } ` ,
297
+ `Starting contact streaming ${ timestamp } - orderingKey ${ orderingKey } ` ,
295
298
providerConfig . apiKey ,
296
299
) ;
297
300
@@ -303,7 +306,6 @@ export class Controller {
303
306
const iterator = this . adapter . streamContacts ( providerConfig ) ;
304
307
305
308
let result = await iterator . next ( ) ;
306
-
307
309
while ( ! result . done ) {
308
310
const { value : contacts } = result ;
309
311
@@ -312,18 +314,19 @@ export class Controller {
312
314
throw new Error ( 'Invalid contacts received' ) ;
313
315
}
314
316
315
- const message : PubSubContactsMessage = {
316
- userId,
317
- timestamp,
318
- contacts : contacts . map ( ( contact ) =>
319
- sanitizeContact ( contact , providerConfig . locale ) ,
320
- ) ,
321
- state : PubSubContactsState . IN_PROGRESS ,
322
- integrationName : this . integrationName ,
323
- // traceparent: tracer.getTraceParent(),
324
- } ;
325
-
326
- await this . pubSubContactStreamingClient ?. publishMessage ( message ) ;
317
+ await this . pubSubContactStreamingClient ?. publishMessage (
318
+ {
319
+ userId,
320
+ timestamp,
321
+ contacts : contacts . map ( ( contact ) =>
322
+ sanitizeContact ( contact , providerConfig . locale ) ,
323
+ ) ,
324
+ state : PubSubContactsState . IN_PROGRESS ,
325
+ integrationName : this . integrationName ,
326
+ // traceparent: tracer.getTraceParent(),
327
+ } ,
328
+ orderingKey ,
329
+ ) ;
327
330
} catch ( error ) {
328
331
errorLogger (
329
332
'streamContacts' ,
@@ -339,14 +342,17 @@ export class Controller {
339
342
340
343
const streamingPromise = streamContacts ( )
341
344
. then ( ( ) => {
342
- return this . pubSubContactStreamingClient ?. publishMessage ( {
343
- userId : providerConfig . userId ,
344
- timestamp,
345
- contacts : [ ] ,
346
- state : PubSubContactsState . COMPLETE ,
347
- integrationName : this . integrationName ,
348
- // traceparent: tracer.getTraceParent(),
349
- } ) ;
345
+ return this . pubSubContactStreamingClient ?. publishMessage (
346
+ {
347
+ userId : providerConfig . userId ,
348
+ timestamp,
349
+ contacts : [ ] ,
350
+ state : PubSubContactsState . COMPLETE ,
351
+ integrationName : this . integrationName ,
352
+ // traceparent: tracer.getTraceParent(),
353
+ } ,
354
+ orderingKey ,
355
+ ) ;
350
356
} )
351
357
. catch ( async ( error ) => {
352
358
errorLogger (
@@ -355,14 +361,17 @@ export class Controller {
355
361
providerConfig . apiKey ,
356
362
error ,
357
363
) ;
358
- return this . pubSubContactStreamingClient ?. publishMessage ( {
359
- userId : providerConfig . userId ,
360
- timestamp,
361
- contacts : [ ] ,
362
- state : PubSubContactsState . FAILED ,
363
- integrationName : this . integrationName ,
364
- // traceparent: tracer.getTraceParent(),
365
- } ) ;
364
+ return this . pubSubContactStreamingClient ?. publishMessage (
365
+ {
366
+ userId : providerConfig . userId ,
367
+ timestamp,
368
+ contacts : [ ] ,
369
+ state : PubSubContactsState . FAILED ,
370
+ integrationName : this . integrationName ,
371
+ // traceparent: tracer.getTraceParent(),
372
+ } ,
373
+ orderingKey ,
374
+ ) ;
366
375
} )
367
376
. catch ( ( error ) => {
368
377
errorLogger (
0 commit comments