ag-common 0.0.719 → 0.0.721
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.
|
@@ -55,8 +55,8 @@ const batchWrite = (tableName, itemsIn) => __awaiter(void 0, void 0, void 0, fun
|
|
|
55
55
|
try {
|
|
56
56
|
yield (0, async_1.asyncForEach)(chunked, (items) => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
57
|
let retryCount = 0;
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
const retryMax = 3;
|
|
59
|
+
const params = new lib_dynamodb_1.BatchWriteCommand({
|
|
60
60
|
RequestItems: {
|
|
61
61
|
[`${tableName}`]: items.map((Item) => ({
|
|
62
62
|
PutRequest: { Item },
|
|
@@ -64,28 +64,32 @@ const batchWrite = (tableName, itemsIn) => __awaiter(void 0, void 0, void 0, fun
|
|
|
64
64
|
},
|
|
65
65
|
});
|
|
66
66
|
(0, log_1.debug)(`running dynamo batchWrite=${JSON.stringify(params, null, 2)}`);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
|
-
let es = e.toString();
|
|
74
|
-
let msg = es;
|
|
75
|
-
(0, log_1.warn)('dynamo write error', msg);
|
|
76
|
-
if (es.indexOf('429') !== -1 ||
|
|
77
|
-
es.indexOf('ProvisionedThroughputExceeded') !== -1) {
|
|
78
|
-
retryCount += 1;
|
|
79
|
-
msg = `batch write throttled. retry ${retryCount}/${retryMax}`;
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
throw e;
|
|
67
|
+
// eslint-disable-next-line
|
|
68
|
+
while (true) {
|
|
69
|
+
try {
|
|
70
|
+
yield exports.dynamoDb.send(params);
|
|
71
|
+
return {};
|
|
83
72
|
}
|
|
84
|
-
|
|
73
|
+
catch (e) {
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
75
|
+
const es = e.toString();
|
|
76
|
+
let msg = es;
|
|
77
|
+
(0, log_1.warn)('dynamo write error', msg);
|
|
78
|
+
if (es.indexOf('429') !== -1 ||
|
|
79
|
+
es.indexOf('ProvisionedThroughputExceeded') !== -1) {
|
|
80
|
+
retryCount += 1;
|
|
81
|
+
msg = `batch write throttled. retry ${retryCount}/${retryMax}`;
|
|
82
|
+
(0, log_1.warn)(msg);
|
|
83
|
+
if (retryCount >= retryMax) {
|
|
84
|
+
throw new Error(`Max retries (${retryMax}) exceeded: ${es}`);
|
|
85
|
+
}
|
|
86
|
+
yield (0, sleep_1.sleep)(2000);
|
|
87
|
+
// Continue the while loop to retry
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
// For non-throttling errors, throw immediately
|
|
85
91
|
throw e;
|
|
86
92
|
}
|
|
87
|
-
(0, log_1.warn)(`dynamo retry ${retryCount}/${retryMax}`);
|
|
88
|
-
yield (0, sleep_1.sleep)(2000);
|
|
89
93
|
}
|
|
90
94
|
}));
|
|
91
95
|
return {};
|
|
@@ -97,13 +101,13 @@ const batchWrite = (tableName, itemsIn) => __awaiter(void 0, void 0, void 0, fun
|
|
|
97
101
|
});
|
|
98
102
|
exports.batchWrite = batchWrite;
|
|
99
103
|
const batchDelete = (_a) => __awaiter(void 0, [_a], void 0, function* ({ tableName, keys, pkName, }) {
|
|
100
|
-
//batch up to 20, so we can retry
|
|
104
|
+
// batch up to 20, so we can retry
|
|
101
105
|
let chunked = (0, array_1.chunk)(keys, 20);
|
|
102
106
|
try {
|
|
103
107
|
yield (0, async_1.asyncForEach)(chunked, (items) => __awaiter(void 0, void 0, void 0, function* () {
|
|
104
108
|
let retryCount = 0;
|
|
105
|
-
|
|
106
|
-
|
|
109
|
+
const retryMax = 3;
|
|
110
|
+
const params = new lib_dynamodb_1.BatchWriteCommand({
|
|
107
111
|
RequestItems: {
|
|
108
112
|
[`${tableName}`]: items.map((key) => ({
|
|
109
113
|
DeleteRequest: { Key: { [`${pkName}`]: key } },
|
|
@@ -111,28 +115,36 @@ const batchDelete = (_a) => __awaiter(void 0, [_a], void 0, function* ({ tableNa
|
|
|
111
115
|
},
|
|
112
116
|
});
|
|
113
117
|
(0, log_1.debug)(`running dynamo batch delete=${JSON.stringify(params, null, 2)}`);
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
catch (e) {
|
|
119
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
120
|
-
let es = e.toString();
|
|
121
|
-
let msg = es;
|
|
122
|
-
(0, log_1.warn)('dynamo write error', msg);
|
|
123
|
-
if (es.indexOf('429') !== -1 ||
|
|
124
|
-
es.indexOf('ProvisionedThroughputExceeded') !== -1) {
|
|
125
|
-
retryCount += 1;
|
|
126
|
-
msg = `batch delete write throttled. retry ${retryCount}/${retryMax}`;
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
throw e;
|
|
118
|
+
while (retryCount < retryMax) {
|
|
119
|
+
try {
|
|
120
|
+
yield exports.dynamoDb.send(params);
|
|
121
|
+
return {};
|
|
130
122
|
}
|
|
131
|
-
|
|
132
|
-
|
|
123
|
+
catch (e) {
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
125
|
+
const es = e.toString();
|
|
126
|
+
let shouldRetry = false;
|
|
127
|
+
if (es.indexOf('429') !== -1 ||
|
|
128
|
+
es.indexOf('ProvisionedThroughputExceeded') !== -1) {
|
|
129
|
+
shouldRetry = true;
|
|
130
|
+
retryCount += 1;
|
|
131
|
+
const msg = `batch delete write throttled. retry ${retryCount}/${retryMax}`;
|
|
132
|
+
(0, log_1.warn)('dynamo write error', msg);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
// Non-retryable error
|
|
136
|
+
throw e;
|
|
137
|
+
}
|
|
138
|
+
if (shouldRetry) {
|
|
139
|
+
if (retryCount >= retryMax) {
|
|
140
|
+
(0, log_1.warn)(`Max retries (${retryMax}) reached, giving up`);
|
|
141
|
+
throw e;
|
|
142
|
+
}
|
|
143
|
+
(0, log_1.warn)(`dynamo retry ${retryCount}/${retryMax}`);
|
|
144
|
+
yield (0, sleep_1.sleep)(2000 * Math.pow(2, retryCount - 1)); // Exponential backoff
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
133
147
|
}
|
|
134
|
-
(0, log_1.warn)(`dynamo retry ${retryCount}/${retryMax}`);
|
|
135
|
-
yield (0, sleep_1.sleep)(2000);
|
|
136
148
|
}
|
|
137
149
|
}));
|
|
138
150
|
return {};
|
|
@@ -31,8 +31,12 @@ exports.arrayToObject = arrayToObject;
|
|
|
31
31
|
const flat = (arr) => [].concat(...arr);
|
|
32
32
|
exports.flat = flat;
|
|
33
33
|
const take = (array, num) => {
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
// Ensure num is within the bounds of the array
|
|
35
|
+
const safeNum = Math.max(0, Math.min(num, array.length));
|
|
36
|
+
// Use array.slice() to create shallow copies of the parts we need
|
|
37
|
+
const part = array.slice(0, safeNum);
|
|
38
|
+
const rest = array.slice(safeNum);
|
|
39
|
+
return { part, rest };
|
|
36
40
|
};
|
|
37
41
|
exports.take = take;
|
|
38
42
|
const chunk = (array, max) => {
|
|
@@ -24,6 +24,7 @@ function isJson(str) {
|
|
|
24
24
|
}
|
|
25
25
|
exports.isJson = isJson;
|
|
26
26
|
const objectKeysToLowerCase = (origObj) => {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
27
28
|
if (!origObj || Object.keys(origObj).length === 0) {
|
|
28
29
|
return {};
|
|
29
30
|
}
|
|
@@ -42,6 +43,7 @@ exports.objectKeysToLowerCase = objectKeysToLowerCase;
|
|
|
42
43
|
const getObjectKeysAsNumber = (o) => Object.keys(o).map((o2) => parseInt(o2, 10));
|
|
43
44
|
exports.getObjectKeysAsNumber = getObjectKeysAsNumber;
|
|
44
45
|
function objectToArray(obj) {
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
45
47
|
if (!obj) {
|
|
46
48
|
return [];
|
|
47
49
|
}
|
|
@@ -108,6 +110,7 @@ function objectToString(obj,
|
|
|
108
110
|
joinKeyValue,
|
|
109
111
|
/** eg '&' */
|
|
110
112
|
joinKeys) {
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
111
114
|
if (!obj || Object.keys(obj).length === 0) {
|
|
112
115
|
return '';
|
|
113
116
|
}
|
package/package.json
CHANGED