2019-07-05 12:01:38 +02:00
|
|
|
import env from "./env";
|
2019-06-28 03:46:48 +02:00
|
|
|
|
|
|
|
export default class Consumer {
|
2019-06-29 03:35:53 +02:00
|
|
|
|
2019-07-03 01:41:05 +02:00
|
|
|
constructor(){
|
2019-07-05 12:01:38 +02:00
|
|
|
this.connectors = {};
|
|
|
|
for (let connector of env.config.connectors) {
|
|
|
|
this.connectors[connector.name] = connector.class
|
|
|
|
}
|
|
|
|
|
2019-07-05 17:06:57 +02:00
|
|
|
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-07-05 12:01:38 +02:00
|
|
|
|
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 {
|
2019-07-05 12:01:38 +02:00
|
|
|
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-07-05 12:01:38 +02:00
|
|
|
|
2019-06-30 01:42:58 +02:00
|
|
|
} catch (error) {
|
2019-07-05 12:01:38 +02:00
|
|
|
env.logger.log({
|
2019-07-03 01:41:05 +02:00
|
|
|
level: 'error',
|
|
|
|
message: error
|
|
|
|
});
|
2019-06-28 03:46:48 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|