forked from redpencilio/poc-diff-consumer-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (40 loc) · 1.36 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
import fetch from "node-fetch";
import { app, errorHandler } from 'mu';
import express from "express";
import { getNextSyncTask, getLatestSyncTask, insertNextSyncTask } from './lib/sync-task';
import { getUnconsumedFiles } from './lib/delta-file';
const INGEST_INTERVAL = process.env.INGEST_INTERVAL_MS || 5000;
async function triggerIngest() {
console.log(`Consuming diff files at ${new Date().toISOString()}`);
await fetch("http://localhost/ingest/", { method: "POST" });
setTimeout( triggerIngest, INGEST_INTERVAL );
}
triggerIngest();
app.use("/ingest", express.json());
app.post('/ingest', async function( req, res, next ) {
const task = await getNextSyncTask();
if (task) {
console.log(`Ingesting new delta files since ${task.since.toISOString()}`);
try {
const files = await getUnconsumedFiles(task.since);
task.files = files;
task.execute();
return res.status(202).end();
} catch(e) {
console.log(`Something went wrong while ingesting`);
console.trace(e);
// TODO write failure to store
return next(new Error(e));
}
} else {
console.log(`No sync task found`);
const latestTask = getLatestSyncTask();
if (latestTask) {
await insertNextSyncTask(latestTask.until);
} else {
await insertNextSyncTask();
}
return res.status(200).end();
}
});
app.use(errorHandler);