-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (58 loc) · 1.84 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const https = require('https');
const fetch = async (url, method) => {
method = method || 'GET';
const options = { method };
return new Promise((resolve, reject) => {
const req = https.request(url, options, function (res) {
res.setEncoding('utf8');
if (method === 'HEAD') {
return resolve(res.headers)
}
res.on('data', function (chunk) {
resolve(chunk);
});
});
req.on('error', function (e) {
reject(e);
});
req.end();
});
};
const splitByBatches = (array, size) => {
const chunked = [];
for (let i = 0, j = array.length; i < j; i += size) {
chunked.push(array.slice(i, i + size));
}
return chunked;
};
const initSingleHeater = (config) => {
const { name, interval, batch, urls } = config;
const handler = async () => {
const batches = splitByBatches(urls, batch);
for (let i = 0; i < batches.length; i++) {
const batch = batches[i];
console.log(`[${name}] start processing bath ${i + 1}`);
await Promise.all(batch.map(url => new Promise(resolve => {
const start = new Date();
fetch(`${url}?t=${Date.now()}`, 'HEAD').then(res => {
console.log(`[${name}][${url}] fetching took ${new Date() - start}ms`);
resolve(res);
});
})));
console.log(`[${name}] batch processed`);
}
console.log(`[${name}] wait for the next tick`)
};
console.log(`[${name}] set heating interval to ${interval}ms`);
handler();
setInterval(handler, interval)
};
(async () => {
const sourcesUrl = process.env.SOURCES_URL;
if (!sourcesUrl) throw new Error('Please set SOURCES_URL env variable with the link to json config');
const sources = JSON.parse(await fetch(sourcesUrl, 'GET'));
console.log('initialize heater');
for (const source of sources) {
initSingleHeater(source);
}
})();