-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreporter.js
More file actions
75 lines (65 loc) · 1.5 KB
/
reporter.js
File metadata and controls
75 lines (65 loc) · 1.5 KB
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
"use strict";
const Log = require('./log');
const Amplitude = require('./amplitude');
const EVENT_QUEUE = 'event_queue';
class Reporter
{
/**
* Initialize bot.
*
* @param {string} mqUrl - The URL to use when connecting to the MQ.
*/
constructor(mqUrl)
{
this.mqUrl = mqUrl;
this.amqp = require('amqplib');
this.amplitude = new Amplitude(process.env.AMPLITUDE_API_TOKEN);
}
/**
* Connect to Slack and get things started.
*/
async connect()
{
Log.info('Connecting to MQ...');
this.mqConnection = await this.amqp.connect(this.mqUrl);
await this.listenToQueue(EVENT_QUEUE);
}
/**
* Listen to a specific queue.
*
* @param {string} name - The name of the queue to listen to.
*/
async listenToQueue(name)
{
const ch = await this.mqConnection.createChannel();
ch.assertQueue(name);
Log.info(`Connected to queue ${name}.`);
ch.consume(name, this.onConsume.bind(this), { noAck: true });
}
/**
* Consume a queue message.
*
* @param {object} message - The message to consume.
*/
async onConsume(message)
{
try {
const decoded = JSON.parse(message.content.toString());
await this.processQueueMessage(decoded);
} catch (e) {
Log.error(e);
Log.error(message.content.toString());
throw e;
}
}
/**
* Process a message from the queue, sending it to event tracking services.
*
* @param object message The message from the queue.
*/
async processQueueMessage(message)
{
await this.amplitude.track(message);
}
}
module.exports = Reporter;