@izara_project/izara-core-library-asynchronous-flow 1.0.6 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -0
- package/package.json +1 -1
- package/src/AsyncFlowSharedLib.js +137 -49
package/index.js
CHANGED
|
@@ -38,6 +38,7 @@ module.exports = {
|
|
|
38
38
|
createAwaitingStep: asyncFlowSharedLib.createAwaitingStep,
|
|
39
39
|
findPendingStepIdsAwaitingStep: asyncFlowSharedLib.findPendingStepIdsAwaitingStep,
|
|
40
40
|
findPendingStepsAwaitingStep: asyncFlowSharedLib.findPendingStepsAwaitingStep,
|
|
41
|
+
findPendingStepAwaitingStep: asyncFlowSharedLib.findPendingStepAwaitingStep,
|
|
41
42
|
|
|
42
43
|
// Checking and clearing steps
|
|
43
44
|
checkAllAwaitingStepsFinished: asyncFlowSharedLib.checkAllAwaitingStepsFinished,
|
package/package.json
CHANGED
|
@@ -28,46 +28,46 @@ const sqsSharedLib = require('@izara_project/izara-core-library-sqs');
|
|
|
28
28
|
// External service interactions
|
|
29
29
|
const { sqs } = require('@izara_project/izara-core-library-external-request');
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
function createFieldNameUniqueRequestId(
|
|
32
32
|
prefix,
|
|
33
33
|
uniqueRequestIdFieldName = 'UniqueRequestId'
|
|
34
|
-
)
|
|
34
|
+
) {
|
|
35
35
|
return prefix + uniqueRequestIdFieldName;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
function createConcatenatedAwaitingStepId(
|
|
39
39
|
partitionKey,
|
|
40
40
|
sortId,
|
|
41
41
|
prefix = ""
|
|
42
|
-
)
|
|
42
|
+
) {
|
|
43
43
|
return prefix + partitionKey + "_" + sortId
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
function createConcatenatedPendingStepId(
|
|
47
47
|
parentId,
|
|
48
48
|
childId
|
|
49
|
-
)
|
|
49
|
+
) {
|
|
50
50
|
return parentId + "_" + childId
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
function createAwaitingStepId(
|
|
54
54
|
partitionKey,
|
|
55
55
|
prefix = ""
|
|
56
|
-
)
|
|
56
|
+
) {
|
|
57
57
|
return prefix + partitionKey
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
function createPendingStepId(
|
|
61
61
|
identifierId,
|
|
62
62
|
prefix = ""
|
|
63
|
-
)
|
|
63
|
+
) {
|
|
64
64
|
return prefix + identifierId
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
function explodePendingStepId(
|
|
68
68
|
pendingStepId,
|
|
69
69
|
prefix = ""
|
|
70
|
-
)
|
|
70
|
+
) {
|
|
71
71
|
if (prefix === "") {
|
|
72
72
|
return pendingStepId;
|
|
73
73
|
} else {
|
|
@@ -84,10 +84,10 @@ module.exports.explodePendingStepId = (
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
// copy from izara-core-library-trigger-cache
|
|
87
|
-
|
|
87
|
+
function createFieldNameCacheComplete(prefix = 'cache') {
|
|
88
88
|
return prefix + 'CacheComplete';
|
|
89
89
|
}
|
|
90
|
-
|
|
90
|
+
function createFieldNameStatus(prefix = 'cache') {
|
|
91
91
|
return prefix + 'Status';
|
|
92
92
|
}
|
|
93
93
|
|
|
@@ -98,13 +98,13 @@ module.exports.createFieldNameStatus = (prefix = 'cache') => {
|
|
|
98
98
|
// logic can prepend any prefix to awaitingStepId if want to be share one table and differentiate different external step ids
|
|
99
99
|
|
|
100
100
|
//not yet used ?
|
|
101
|
-
|
|
101
|
+
async function createAwaitingMultipleStepsWithAdditionalAttributes(
|
|
102
102
|
_izContext,
|
|
103
103
|
records, // array of object including properties: awaitingStepId / (optional) additionalAttributes
|
|
104
104
|
pendingStepId,
|
|
105
105
|
tableName = "AwaitingMultipleSteps",
|
|
106
106
|
byPendingTableName = "AwaitingMultipleStepByPending"
|
|
107
|
-
)
|
|
107
|
+
) {
|
|
108
108
|
_izContext.logger.debug("Lib:createAwaitingMultipleStepsWithAdditionalAttributes", {
|
|
109
109
|
records: records,
|
|
110
110
|
pendingStepId: pendingStepId,
|
|
@@ -146,13 +146,13 @@ module.exports.createAwaitingMultipleStepsWithAdditionalAttributes = async (
|
|
|
146
146
|
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
async function createAwaitingMultipleSteps(
|
|
150
150
|
_izContext,
|
|
151
151
|
records, // array of awaitingStepIds
|
|
152
152
|
pendingStepId,
|
|
153
153
|
tableName = "AwaitingMultipleSteps",
|
|
154
154
|
byPendingTableName = "AwaitingMultipleStepByPending"
|
|
155
|
-
)
|
|
155
|
+
) {
|
|
156
156
|
_izContext.logger.debug("Lib:createAwaitingMultipleSteps", {
|
|
157
157
|
records: records,
|
|
158
158
|
pendingStepId: pendingStepId,
|
|
@@ -201,13 +201,13 @@ module.exports.createAwaitingMultipleSteps = async (
|
|
|
201
201
|
// One awaitingStepId can have multiple pendingStepIds that are awaiting it
|
|
202
202
|
// logic can prepend any prefix to awaitingStepId if want to be share one table and differentiate different external step ids
|
|
203
203
|
|
|
204
|
-
|
|
204
|
+
async function createAwaitingStep(
|
|
205
205
|
_izContext,
|
|
206
206
|
awaitingStepId,
|
|
207
207
|
pendingStepId,
|
|
208
208
|
tableName = "AwaitingStep",
|
|
209
209
|
additionalAttributes = {} // save in top level
|
|
210
|
-
)
|
|
210
|
+
) {
|
|
211
211
|
|
|
212
212
|
let attributesWhenCreate = {
|
|
213
213
|
awaitingStepId: awaitingStepId,
|
|
@@ -230,11 +230,11 @@ module.exports.createAwaitingStep = async (
|
|
|
230
230
|
|
|
231
231
|
|
|
232
232
|
|
|
233
|
-
|
|
233
|
+
async function findPendingStepIdsAwaitingStep(
|
|
234
234
|
_izContext,
|
|
235
235
|
awaitingStepId,
|
|
236
236
|
tableName = "AwaitingStep"
|
|
237
|
-
)
|
|
237
|
+
) {
|
|
238
238
|
|
|
239
239
|
let pendingStepIds = [];
|
|
240
240
|
|
|
@@ -253,11 +253,11 @@ module.exports.findPendingStepIdsAwaitingStep = async (
|
|
|
253
253
|
return pendingStepIds
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
-
|
|
256
|
+
async function findPendingStepsAwaitingStep(
|
|
257
257
|
_izContext,
|
|
258
258
|
awaitingStepId,
|
|
259
259
|
tableName = "AwaitingStep"
|
|
260
|
-
)
|
|
260
|
+
) {
|
|
261
261
|
|
|
262
262
|
let listPendingSteps = await dynamodbSharedLib.query(
|
|
263
263
|
_izContext,
|
|
@@ -269,18 +269,61 @@ module.exports.findPendingStepsAwaitingStep = async (
|
|
|
269
269
|
_izContext.logger.debug("Record of awaitingStepId", listPendingSteps)
|
|
270
270
|
return listPendingSteps.Items;
|
|
271
271
|
}
|
|
272
|
+
async function findPendingStepAwaitingStep(
|
|
273
|
+
_izContext,
|
|
274
|
+
awaitingStepId,
|
|
275
|
+
tableName = "AwaitingStep"
|
|
276
|
+
) {
|
|
277
|
+
|
|
278
|
+
let listPendingSteps = await dynamodbSharedLib.query(
|
|
279
|
+
_izContext,
|
|
280
|
+
await dynamodbSharedLib.tableName(_izContext, tableName),
|
|
281
|
+
{
|
|
282
|
+
awaitingStepId: awaitingStepId
|
|
283
|
+
}
|
|
284
|
+
);
|
|
285
|
+
_izContext.logger.debug("Record of awaitingStepId", listPendingSteps)
|
|
286
|
+
|
|
287
|
+
if (listPendingSteps.length !== 1) {
|
|
288
|
+
throw new NoRetryError("listPendingSteps not equals 1")
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return listPendingSteps.Items[0];
|
|
292
|
+
}
|
|
272
293
|
|
|
273
294
|
|
|
274
295
|
|
|
275
|
-
|
|
296
|
+
async function checkAllAwaitingStepsFinished(
|
|
276
297
|
_izContext,
|
|
277
298
|
pendingStepId,
|
|
278
299
|
currentAwaitingStepId = null,
|
|
300
|
+
errorsFound = [],
|
|
301
|
+
tableName = 'AwaitingMultipleSteps',
|
|
302
|
+
byPendingTableName = "AwaitingMultipleStepByPending"
|
|
303
|
+
// indexName = ''
|
|
304
|
+
) {
|
|
305
|
+
let [remaining, errorsFound] = await this.checkAllAwaitingStepsFinishedWithError(
|
|
306
|
+
_izContext,
|
|
307
|
+
pendingStepId,
|
|
308
|
+
currentAwaitingStepId,
|
|
309
|
+
errorsFound,
|
|
310
|
+
tableName,
|
|
311
|
+
byPendingTableName
|
|
312
|
+
);
|
|
313
|
+
return remaining;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
module.exports.checkAllAwaitingStepsFinishedWithError = async (
|
|
318
|
+
_izContext,
|
|
319
|
+
pendingStepId,
|
|
320
|
+
currentAwaitingStepId = null,
|
|
321
|
+
errorsFound = [],
|
|
279
322
|
tableName = 'AwaitingMultipleSteps',
|
|
280
323
|
byPendingTableName = "AwaitingMultipleStepByPending"
|
|
281
324
|
// indexName = ''
|
|
282
325
|
) => {
|
|
283
|
-
_izContext.logger.debug("Lib:
|
|
326
|
+
_izContext.logger.debug("Lib:checkAllAwaitingStepsFinishedWithError", {
|
|
284
327
|
pendingStepId: pendingStepId,
|
|
285
328
|
currentAwaitingStepId: currentAwaitingStepId,
|
|
286
329
|
tableName: tableName,
|
|
@@ -311,7 +354,8 @@ module.exports.checkAllAwaitingStepsFinished = async (
|
|
|
311
354
|
awaitingStepId: currentAwaitingStepId
|
|
312
355
|
},
|
|
313
356
|
{
|
|
314
|
-
complete: true
|
|
357
|
+
complete: true,
|
|
358
|
+
errorsFound: errorsFound
|
|
315
359
|
},
|
|
316
360
|
);
|
|
317
361
|
|
|
@@ -328,16 +372,19 @@ module.exports.checkAllAwaitingStepsFinished = async (
|
|
|
328
372
|
);
|
|
329
373
|
for (let idx = 0; idx < listPendingStepIds.Items.length; idx++) {
|
|
330
374
|
|
|
375
|
+
// if have error found return errorfound
|
|
376
|
+
// wrap function
|
|
377
|
+
|
|
331
378
|
if (currentAwaitingStepId && listPendingStepIds.Items[idx].awaitingStepId == currentAwaitingStepId) {
|
|
332
379
|
continue;
|
|
333
380
|
}
|
|
334
381
|
// if any record found not set to complete then there are steps not yet finished
|
|
335
382
|
if (!listPendingStepIds.Items[idx].complete) {
|
|
336
|
-
return false;
|
|
383
|
+
return [false, errorsFound];
|
|
337
384
|
}
|
|
338
385
|
}
|
|
339
386
|
|
|
340
|
-
return true;
|
|
387
|
+
return [true, errorsFound]; // all passed
|
|
341
388
|
|
|
342
389
|
|
|
343
390
|
// // mark currentAwaitingStepId as complete, this is done to protect against race condition in between checking all complete
|
|
@@ -384,12 +431,14 @@ module.exports.checkAllAwaitingStepsFinished = async (
|
|
|
384
431
|
// return true;
|
|
385
432
|
}
|
|
386
433
|
|
|
387
|
-
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
async function clearAllAwaitingSteps(
|
|
388
437
|
_izContext,
|
|
389
438
|
pendingStepId,
|
|
390
439
|
tableName = 'AwaitingMultipleSteps',
|
|
391
440
|
byPendingTableName = "AwaitingMultipleStepByPending"
|
|
392
|
-
)
|
|
441
|
+
) {
|
|
393
442
|
_izContext.logger.debug("Lib:clearAllAwaitingSteps", {
|
|
394
443
|
pendingStepId: pendingStepId,
|
|
395
444
|
tableName: tableName,
|
|
@@ -442,12 +491,12 @@ module.exports.clearAllAwaitingSteps = async (
|
|
|
442
491
|
}
|
|
443
492
|
|
|
444
493
|
|
|
445
|
-
|
|
494
|
+
async function removeAwaitingStep(
|
|
446
495
|
_izContext,
|
|
447
496
|
awaitingStepId,
|
|
448
497
|
pendingStepId,
|
|
449
498
|
tableName = "AwaitingStep"
|
|
450
|
-
)
|
|
499
|
+
) {
|
|
451
500
|
|
|
452
501
|
await dynamodbSharedLib.deleteItem(
|
|
453
502
|
_izContext,
|
|
@@ -459,13 +508,13 @@ module.exports.removeAwaitingStep = async (
|
|
|
459
508
|
);
|
|
460
509
|
}
|
|
461
510
|
|
|
462
|
-
|
|
511
|
+
async function removeAwaitingMultipleStep(
|
|
463
512
|
_izContext,
|
|
464
513
|
awaitingStepId,
|
|
465
514
|
pendingStepId,
|
|
466
515
|
tableName = "AwaitingMultipleSteps",
|
|
467
516
|
byPendingTableName = "AwaitingMultipleStepByPending"
|
|
468
|
-
)
|
|
517
|
+
) {
|
|
469
518
|
_izContext.logger.debug("Lib:removeAwaitingMultipleStep", {
|
|
470
519
|
awaitingStepId: awaitingStepId,
|
|
471
520
|
pendingStepId: pendingStepId,
|
|
@@ -502,14 +551,14 @@ module.exports.removeAwaitingMultipleStep = async (
|
|
|
502
551
|
}
|
|
503
552
|
|
|
504
553
|
// only works with AwaitingStep, does not work with AwaitingMultipleSteps, need to make new function that checks conditional pass? If pass delete from byPending table too
|
|
505
|
-
|
|
554
|
+
async function removeAwaitingStepWithCheckUniqueRequestId(
|
|
506
555
|
_izContext,
|
|
507
556
|
awaitingStepId,
|
|
508
557
|
pendingStepId,
|
|
509
558
|
checkUniqueRequestId,
|
|
510
559
|
prefix = "",
|
|
511
560
|
tableName = "AwaitingStep"
|
|
512
|
-
)
|
|
561
|
+
) {
|
|
513
562
|
|
|
514
563
|
// if logicalElements add queryElements.logicalElements
|
|
515
564
|
// set condition uniqueRequestId match with exists
|
|
@@ -561,14 +610,14 @@ module.exports.removeAwaitingStepWithCheckUniqueRequestId = async (
|
|
|
561
610
|
|
|
562
611
|
* @returns [string] [returnStatus, existingRecord]
|
|
563
612
|
*/
|
|
564
|
-
|
|
613
|
+
async function checkUniqueRequestProcessing(
|
|
565
614
|
_izContext,
|
|
566
615
|
tableName,
|
|
567
616
|
primaryKey,
|
|
568
617
|
prefix = '',
|
|
569
618
|
overwriteUniqueRequestId = null,
|
|
570
619
|
uniqueRequestIdFieldName = "UniqueRequestId" // if set will check/add this fieldname with _izContext.uniqueRequestId value
|
|
571
|
-
)
|
|
620
|
+
) {
|
|
572
621
|
try {
|
|
573
622
|
|
|
574
623
|
_izContext.logger.debug('current uniqueRequestId:', _izContext.uniqueRequestId)
|
|
@@ -666,13 +715,13 @@ module.exports.checkUniqueRequestProcessing = async (
|
|
|
666
715
|
};
|
|
667
716
|
|
|
668
717
|
// ----- Helper funtion, common use case in triggerFlow ---------
|
|
669
|
-
|
|
718
|
+
async function checkTimeCacheComplete(
|
|
670
719
|
_izContext,
|
|
671
720
|
fullMainTableName,
|
|
672
721
|
keyValues,
|
|
673
722
|
timeCacheComplete,
|
|
674
723
|
prefix
|
|
675
|
-
)
|
|
724
|
+
) {
|
|
676
725
|
let [returnValue, newTimeCacheComplete, uniqueRequestId] = await this.checkAndGetTimeCacheComplete(
|
|
677
726
|
_izContext,
|
|
678
727
|
fullMainTableName,
|
|
@@ -682,13 +731,13 @@ module.exports.checkTimeCacheComplete = async (
|
|
|
682
731
|
);
|
|
683
732
|
return [returnValue, uniqueRequestId];
|
|
684
733
|
}
|
|
685
|
-
|
|
734
|
+
async function checkAndGetTimeCacheComplete(
|
|
686
735
|
_izContext,
|
|
687
736
|
fullMainTableName,
|
|
688
737
|
keyValues,
|
|
689
738
|
timeCacheComplete,
|
|
690
739
|
prefix
|
|
691
|
-
)
|
|
740
|
+
) {
|
|
692
741
|
_izContext.logger.debug('event checkTimeCacheComplete', {
|
|
693
742
|
fullMainTableName: fullMainTableName,
|
|
694
743
|
keyValues: keyValues,
|
|
@@ -734,13 +783,13 @@ module.exports.checkAndGetTimeCacheComplete = async (
|
|
|
734
783
|
|
|
735
784
|
|
|
736
785
|
// ----- shared both stored cache and triggered cache, check uniqueRequestId changed ---------
|
|
737
|
-
|
|
786
|
+
async function checkCacheUniqueRequestId(
|
|
738
787
|
_izContext,
|
|
739
788
|
fullMainTableName,
|
|
740
789
|
keyValues,
|
|
741
790
|
checkUniqueRequestId,
|
|
742
791
|
uniqueRequestIdCompleteFieldName
|
|
743
|
-
)
|
|
792
|
+
) {
|
|
744
793
|
|
|
745
794
|
let cacheObject = await dynamodbSharedLib.getItem(
|
|
746
795
|
_izContext,
|
|
@@ -813,12 +862,12 @@ module.exports.checkCacheUniqueRequestId = async (
|
|
|
813
862
|
|
|
814
863
|
//====================================== end Multiple Lambda Invocations (Logic pagination of handling results)
|
|
815
864
|
|
|
816
|
-
|
|
865
|
+
function validateStartKeyParam(
|
|
817
866
|
_izContext,
|
|
818
867
|
startKey,
|
|
819
868
|
partitionKeyFieldName,
|
|
820
869
|
sortKeyFieldName
|
|
821
|
-
)
|
|
870
|
+
) {
|
|
822
871
|
const stringNotEmptyRegex = /^[A-Za-z0-9_-]+$/;
|
|
823
872
|
|
|
824
873
|
if (
|
|
@@ -841,13 +890,13 @@ module.exports.validateStartKeyParam = (
|
|
|
841
890
|
}
|
|
842
891
|
|
|
843
892
|
|
|
844
|
-
|
|
893
|
+
async function validateMultipleInvocations(
|
|
845
894
|
_izContext,
|
|
846
895
|
messageProperty,
|
|
847
896
|
passOnStartKey = {},
|
|
848
897
|
numberInvocation,
|
|
849
898
|
queueName
|
|
850
|
-
)
|
|
899
|
+
) {
|
|
851
900
|
_izContext.logger.debug("Lib:validateMultipleInvocations", {
|
|
852
901
|
messageProperty: messageProperty,
|
|
853
902
|
passOnStartKey: passOnStartKey,
|
|
@@ -880,3 +929,42 @@ module.exports.validateMultipleInvocations = async (
|
|
|
880
929
|
throw (err)
|
|
881
930
|
}
|
|
882
931
|
}
|
|
932
|
+
|
|
933
|
+
module.exports = {
|
|
934
|
+
// Helper functions
|
|
935
|
+
createFieldNameUniqueRequestId,
|
|
936
|
+
createConcatenatedAwaitingStepId,
|
|
937
|
+
createConcatenatedPendingStepId,
|
|
938
|
+
createAwaitingStepId,
|
|
939
|
+
createPendingStepId,
|
|
940
|
+
explodePendingStepId,
|
|
941
|
+
createFieldNameCacheComplete,
|
|
942
|
+
createFieldNameStatus,
|
|
943
|
+
|
|
944
|
+
// AwaitingMultipleSteps functions
|
|
945
|
+
createAwaitingMultipleStepsWithAdditionalAttributes,
|
|
946
|
+
createAwaitingMultipleSteps,
|
|
947
|
+
checkAllAwaitingStepsFinished,
|
|
948
|
+
clearAllAwaitingSteps,
|
|
949
|
+
removeAwaitingMultipleStep,
|
|
950
|
+
|
|
951
|
+
// AwaitingStep functions
|
|
952
|
+
createAwaitingStep,
|
|
953
|
+
findPendingStepIdsAwaitingStep,
|
|
954
|
+
findPendingStepAwaitingStep,
|
|
955
|
+
findPendingStepsAwaitingStep,
|
|
956
|
+
removeAwaitingStep,
|
|
957
|
+
removeAwaitingStepWithCheckUniqueRequestId,
|
|
958
|
+
|
|
959
|
+
// Unique Request Processing functions
|
|
960
|
+
checkUniqueRequestProcessing,
|
|
961
|
+
|
|
962
|
+
// Cache functions
|
|
963
|
+
checkTimeCacheComplete,
|
|
964
|
+
checkAndGetTimeCacheComplete,
|
|
965
|
+
checkCacheUniqueRequestId,
|
|
966
|
+
|
|
967
|
+
// Validation functions
|
|
968
|
+
validateStartKeyParam,
|
|
969
|
+
validateMultipleInvocations
|
|
970
|
+
};
|