forked from daniloc/airtable-api-proxy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbatch.js
97 lines (78 loc) · 3.89 KB
/
batch.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @file Batch Processing for ESOVDB Items using Redis to share batch state between instances of node running in a PM2 cluster
* @author Avana Vana <[email protected]>
* @version 4.0.0
* @module batch
*/
const { createClient } = require('redis');
const cronitor = require('cronitor')(process.env.CRONITOR_API_KEY);
const monitor = new cronitor.Monitor('api-server-telemetry');
const db = createClient();
db.on('error', (err) => { monitor.ping({ state: 'fail', message: 'Unable to connect to Redis.' }); console.log(`[Error] Couldn't connect to Redis.`, err); });
db.on('connect', () => { monitor.ping({ state: 'ok', message: 'Connected to Redis.' }); console.log('Connected to Redis'); });
/** @constant {number} batchInterval - The duration, in seconds after which the current [batch]{@link batch} data is considered stale (default: 10000ms = 10s) */
const batchInterval = 10 * 1000;
module.exports = {
/** @constant {RedisClient} db - Exports a RedisClient instance */
db,
/** @constant {CronitorMonitor} monitor - Exports a Cronitor Monitor instance for recording telemetry events */
monitor,
/**
* Appends an object or multiple objects to the current batch, as part of a Redis set used for batch processing
*
* @async
* @method append
* @param {string} kind - String representation of the type of resource being synced, sent via URL parameter (e.g. 'items' or 'collections')
* @param {('create'|'update')} op - String representation of the current batch operation
* @param {Object[]} items - An array of objects from Airtable (ESOVDB) to be appended to the current batch
* @sideEffects Adds new batch data as redis set item under the redis key for the current batch
* @returns {Object[]} An array of all items in the Redis set representing the current batch
*/
append: async (kind, op, items) => {
await db.sAdd(`batch:${kind}:${op}`, items.map((item) => JSON.stringify(item)));
const data = await db.sMembers(`batch:${kind}:${op}`);
return data.map((item) => JSON.parse(item));
},
/**
* Re-initializes the Redis key and set that will represent the next batch
*
* @async
* @method clear
* @param {string} kind - String representation of the type of resource being synced, sent via URL parameter (e.g. 'items' or 'collections')
* @param {('create'|'update')} op - String representation of the current batch operation
* @sideEffects Deletes Redis key for current batch
*/
clear: async (kind, op) => {
await db.del(`batch:${kind}:${op}`);
},
/**
* Retrieves and returns all members of the Redis set representing the current batch
*
* @async
* @method get
* @param {string} kind - String representation of the type of resource being synced, sent via URL parameter (e.g. 'items' or 'collections')
* @param {('create'|'update')} op - String representation of the current batch operation
* @returns {Object[]} An array of objects for batch processing
*/
get: async (kind, op) => {
const data = await db.sMembers(`batch:${kind}:${op}`);
return data.map((item) => JSON.parse(item));
},
/**
* Retrieves the value of the const {@link batchInterval}
*
* @method interval
* @returns {number} The value of {@link batchInterval}
*/
interval: () => batchInterval,
/**
* Retrieves and returns the size of the current batch, or the cardinality of the Redis set representing the current batch
*
* @async
* @method size
* @param {string} kind - String representation of the type of resource being synced, sent via URL parameter (e.g. 'items' or 'collections')
* @param {('create'|'update')} op - String representation of the current batch operation
* @returns {number} The length of the current batch, or cardinality of the Redis set representing the current batch
*/
size: async (kind, op) => await db.sCard(`batch:${kind}:${op}`)
}