-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (40 loc) · 1.21 KB
/
app.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
import { app, errorHandler } from 'mu';
import bodyParser from 'body-parser';
import Yggdrasil from './repository/yggdrasil';
import DeltaCache from './repository/delta-cache';
import { LOG_INCOMING_DELTA, DELTA_INTERVAL_MS } from './config';
/* Accept application/json format from delta-notifier */
app.use(bodyParser.json({
type: function(req) { return /^application\/json/.test(req.get('content-type')); },
limit: '50mb'
}));
/* Initialize service */
const yggdrasil = new Yggdrasil();
yggdrasil.initialize();
const cache = new DeltaCache();
let hasTimeout = null;
/* Endpoints */
app.post('/delta', async function( req, res ) {
const delta = req.body;
if (LOG_INCOMING_DELTA)
console.log(`Receiving delta ${JSON.stringify(delta)}`);
cache.push(...delta);
if ( !hasTimeout ) {
scheduleDeltaProcessing();
}
res.status(202).send();
});
function scheduleDeltaProcessing() {
setTimeout( () => {
hasTimeout = false;
triggerDeltaProcessing(cache);
}, DELTA_INTERVAL_MS );
hasTimeout = true;
}
async function triggerDeltaProcessing(cache) {
await yggdrasil.processDeltas(cache);
if (!yggdrasil.isBusy && !cache.isEmpty) {
triggerDeltaProcessing(cache);
}
}
app.use(errorHandler);