-
Notifications
You must be signed in to change notification settings - Fork 53
/
push.js
39 lines (33 loc) · 901 Bytes
/
push.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
'use strict';
const request = require('request');
const async = require('async');
const Insight = require('.');
// Messaged on each debounced `track()`
// Gets the queue, merges is with the previous and tries to upload everything
// If it fails, it will save everything again
process.on('message', message => {
const insight = new Insight(message);
const {config} = insight;
const q = config.get('queue') || {};
Object.assign(q, message.queue);
config.delete('queue');
async.forEachSeries(Object.keys(q), (element, cb) => {
const parts = element.split(' ');
const id = parts[0];
const payload = q[element];
request(insight._getRequestObj(id, payload), error => {
if (error) {
cb(error);
return;
}
cb();
});
}, error => {
if (error) {
const q2 = config.get('queue') || {};
Object.assign(q2, q);
config.set('queue', q2);
}
process.exit();
});
});