2022-07-03 12:15:47 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { testEndpoints } = require('./testendpoints');
|
|
|
|
|
|
|
|
process.on('message', async message => {
|
|
|
|
switch (message.command) {
|
|
|
|
case 'testAll':
|
|
|
|
if (!message.services) {
|
|
|
|
process.send({ error: 'services is not defined' });
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const ids = [];
|
|
|
|
const promises = [];
|
2022-07-11 12:52:42 +02:00
|
|
|
|
2022-07-03 12:15:47 +02:00
|
|
|
for (const service of message.services) {
|
2022-07-11 12:52:42 +02:00
|
|
|
const lastChecked = new Date(service.lastChecked);
|
|
|
|
let timePassed = new Date().getTime() - lastChecked.getTime();
|
|
|
|
timePassed = timePassed / 1000 / 60;
|
|
|
|
const interval = service.autotestInterval;
|
|
|
|
const needsCheck = timePassed >= interval;
|
|
|
|
|
2022-07-13 13:42:37 +02:00
|
|
|
if (needsCheck) {
|
2022-07-03 12:15:47 +02:00
|
|
|
ids.push(service.id);
|
|
|
|
promises.push(testEndpoints(service.autotest));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await Promise.all(promises);
|
|
|
|
const mapped = {};
|
|
|
|
for (const [ i, id ] of ids.entries()) {
|
|
|
|
mapped[id] = result[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
process.send({ outage: mapped });
|
|
|
|
}
|
2022-07-13 14:13:35 +02:00
|
|
|
process.exit();
|
2022-07-03 12:15:47 +02:00
|
|
|
|
|
|
|
case 'testOne':
|
|
|
|
if (!message.service) {
|
|
|
|
process.send({ error: 'service is not defined' });
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const { service } = message;
|
|
|
|
const result = await testEndpoints(service.autotest);
|
|
|
|
process.send({ outage: result });
|
|
|
|
}
|
2022-07-13 14:13:35 +02:00
|
|
|
process.exit();
|
2022-07-03 12:15:47 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
console.error('Unknown command:', message.command);
|
2022-07-13 14:13:35 +02:00
|
|
|
process.exit();
|
2022-07-03 12:15:47 +02:00
|
|
|
}
|
|
|
|
});
|