1
0
mirror of https://github.com/nttgin/BGPalerter.git synced 2024-05-19 06:50:08 +00:00
Files
nttgin-BGPalerter/consumer.js

57 lines
1.7 KiB
JavaScript
Raw Normal View History

import env from "./env";
2019-06-28 03:46:48 +02:00
export default class Consumer {
2019-06-29 03:35:53 +02:00
constructor(){
this.connectors = {};
for (let connector of env.config.connectors) {
this.connectors[connector.name] = connector.class
}
this.monitors = env.config.monitors
.map(monitor => new monitor.class(monitor.name, monitor.channel, monitor.params, env));
this.reports = env.config.reports
.map(report => new report.class(report.channels, report.params, env));
2019-06-28 03:46:48 +02:00
process.on('message', this.dispatch);
2019-07-09 02:46:08 +02:00
env.pubSub.subscribe('data', (type, data) => {
this.dispatch(data);
});
2019-06-28 03:46:48 +02:00
};
dispatch = (data) => {
try {
const connector = data.slice(0,3);
const messagesRaw = JSON.parse(data.slice(4));
const messages = this.connectors[connector].transform(messagesRaw);
for (let monitor of this.monitors) {
// Blocking filtering to reduce stack usage
for (const message of messages.filter(monitor.filter)) {
// Promise call to reduce waiting times
monitor
.monitor(message)
.catch(error => {
env.logger.log({
level: 'error',
message: error
});
});
}
2019-06-28 03:46:48 +02:00
}
2019-06-30 01:42:58 +02:00
} catch (error) {
env.logger.log({
level: 'error',
message: error
});
2019-06-28 03:46:48 +02:00
}
};
}