-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
62 lines (53 loc) · 2.11 KB
/
update.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
'use strict';
const moment = require('moment');
const { getPlanetTimestamp } = require('./util/get-states');
const { getLastProcessedState, setProcessedState, storePendingReplications, getReplicationToProcess } = require('./util/redis-client');
const { range } = require('./util/range');
const run = require('./index');
const { NUM_WORKERS } = require('./lib/constants');
// Check the queue and pick a file to process
const processReplication = async () => {
let toProcess = await getReplicationToProcess();
while (toProcess) {
// avoid decreasing the lastProcessedState value
let lastProcessedState = await getLastProcessedState();
if (lastProcessedState < toProcess) {
await setProcessedState(toProcess);
}
console.log(`Processing replication file ${toProcess}`);
await run(toProcess);
console.log(`Finished processing replication file ${toProcess}`);
toProcess = await getReplicationToProcess();
}
};
const queueAndProcess = async () => {
// check if there are any files to process and queue them
const planetState = await getPlanetTimestamp();
let lastProcessedState = await getLastProcessedState();
if (!lastProcessedState) lastProcessedState = planetState - 2;
const files = range(lastProcessedState, planetState + 1);
console.log(`Queueing replication files from ${lastProcessedState} to ${planetState} to process.`);
await Promise.all(files.map(async (f) => {
await storePendingReplications(f);
}));
await Promise.all(
Array(Number(NUM_WORKERS)).fill().map(
async () => await processReplication()
));
};
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const process = async () => {
let lastProcessStartTime, timeDiff;
while (true) {
lastProcessStartTime = moment.utc();
console.log(`-- STARTING PROCESS AT ${lastProcessStartTime.toLocaleString()}`);
await queueAndProcess();
// If the process ends in less than 1 minute,
// make sure it will not run again in less than 60 seconds
timeDiff = moment.utc() - lastProcessStartTime;
if (timeDiff < 60000) {
await sleep(60000 - timeDiff);
}
}
}
process();