-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_record_from_ds.js
More file actions
69 lines (56 loc) · 1.66 KB
/
fetch_record_from_ds.js
File metadata and controls
69 lines (56 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
export default defineComponent({
name: "Fetch Record from Data Store",
description: "Fetch Record from Data Store (DEV or PROD, as appropriate)",
key: "fetch_datastore_record",
version: "0.1.0",
type: "action",
props: {
devDataQueue: {
label: "DEV Data Store queue",
type: "data_store",
},
prodDataQueue: {
label: "PROD Data Store queue",
type: "data_store",
},
isProd: {
label: "Is this a PROD run?",
type: "boolean",
optional: true,
default: false,
},
},
async run({ steps, $ }) {
const MAX_DURATION_MS = 100000
const start = Date.now()
let attempt = 0
const dataQueue = this.isProd ? this.prodDataQueue : this.devDataQueue
while (true){
try {
attempt++
// Access your Data Store via the prop
const keys = await dataQueue.keys();
if (keys.length == 0) {
console.log("Data store is empty. Ending Subworkflow")
$.flow.exit()
}
keys.sort();
const key = keys.pop();
// get record from data store
const record = await dataQueue.get(key);
// remove record from data store
await dataQueue.delete(key);
console.log('Fetched record:', JSON.stringify(record) );
return {key, record};
} catch (err) {
const elapsed = Date.now() - start
console.log(`Attempt ${i+1} failed:`, err.message);
if (elapsed > MAX_DURATION_MS){
throw new Error("Datastore unavailable after 10s of retrying")
}
const delay = Math.min(1000 * attempt, 3000)
await new Promise(r=>setTimeout(r, delay))
}
}
},
});