status/lib/runtime.js
Romein van Buren 09a7df98e6
Define test interval separately for each service
Signed-off-by: Romein van Buren <romein@vburen.nl>
2022-07-11 12:52:42 +02:00

57 lines
1.6 KiB
JavaScript

'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 = [];
for (const service of message.services) {
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;
console.log(`service ${service.id}`, lastChecked, service.autotestEnabled, timePassed, interval, needsCheck);
if (service.autotestEnabled && needsCheck) {
ids.push(service.id);
promises.push(testEndpoints(service.autotest));
console.log(`testing ${service.id}`);
}
}
const result = await Promise.all(promises);
const mapped = {};
for (const [ i, id ] of ids.entries()) {
mapped[id] = result[i];
}
process.send({ outage: mapped });
}
break;
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 });
}
break;
default:
console.error('Unknown command:', message.command);
break;
}
});