forked from unpkg/unpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingestLogsEveryMinute.js
43 lines (31 loc) · 1.27 KB
/
ingestLogsEveryMinute.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
const addMinutes = require('date-fns/add_minutes');
const startOfMinute = require('date-fns/start_of_minute');
const ingestLogs = require('./ingestLogs');
const oneSecond = 1000;
const oneMinute = oneSecond * 60;
let currentWorkload, timer;
function work() {
const now = Date.now();
// The log for a request is typically available within thirty (30) minutes
// of the request taking place under normal conditions. We deliver logs
// ordered by the time that the logs were created, i.e. the timestamp of
// the request when it was received by the edge. Given the order of
// delivery, we recommend waiting a full thirty minutes to ingest a full
// set of logs. This will help ensure that any congestion in the log
// pipeline has passed and a full set of logs can be ingested.
// https://support.cloudflare.com/hc/en-us/articles/216672448-Enterprise-Log-Share-REST-API
const start = startOfMinute(now - oneMinute * 31);
const end = addMinutes(start, 1);
currentWorkload = ingestLogs(start, end);
}
function shutdown() {
console.log('Shutting down...');
clearInterval(timer);
currentWorkload.then(() => {
console.log('Goodbye!');
process.exit();
});
}
work();
process.on('SIGINT', shutdown).on('SIGTERM', shutdown);
timer = setInterval(work, oneMinute);