-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
81 lines (69 loc) · 2.17 KB
/
app.js
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
76
77
78
79
80
81
const nodemailer = require('nodemailer');
const openapiInclude = require('./openapiIncludeAsync');
const host = 'server.name';
const credentials = { username: 'username', token: 'secret', type: 'password' };
const apiFileName = 'irisapi.nocache.js';
start();
async function start() {
try {
await openapiInclude(`https://${host}/irisapi/${apiFileName}`, apiFileName);
const wsUrl = `wss://${host}/socket`;
let client = new iris.Client(wsUrl);
client.addEventListener('connect', async () => {
await client.login(credentials);
console.log('Authenticated');
let transporter = await getNotificationConfig();
new PQAlertMonitor(client, transporter);
});
} catch(err) {
console.error(err);
}
}
async function getNotificationConfig() {
// Gets a random test account from ethereal
const testAccount = await nodemailer.createTestAccount();
const transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false,
auth: {
user: testAccount.user,
pass: testAccount.pass
}
});
return transporter;
}
class PQAlertMonitor {
constructor(client, transporter) {
this.transporter = transporter;
this.client = client;
this.client.addEventListener(iris.Client.EVENT_CONFIG_UPDATED, e => this.onConfigUpdate(e));
console.log('Monitoring...');
}
/**
* Checks if the update is a failure and sends a message if it is
*/
async onConfigUpdate(event) {
console.log('Got config updated event', event.detail.name, event.detail.status);
switch (event.detail.status) {
case 'Disconnected':
case 'Error':
case 'Failed': {
this.sendMessage(event);
break;
}
}
}
/**
* Sends a message for a given event
*/
async sendMessage(event) {
let info = await this.transporter.sendMail({
from: 'Test Failure <test@failure.com>',
to: 'failure@notifications.com',
subject: `[Deephaven] PQ ${event.detail.name} ${event.detail.status}`,
text: `Name: ${event.detail.name}\nSerial: ${event.detail.serial}\nHost: ${host}`
});
console.log(`Preview URL: ${nodemailer.getTestMessageUrl(info)}`);
}
}