-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
184 lines (168 loc) · 5.38 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const { bootstrap } = require('global-agent');
// eslint-disable-next-line object-curly-newline
const { cleanEnv, str, num } = require('envalid');
const Watcher = require('./lib/feedWatcher');
const logger = require('./src/logger')('app');
const { version } = require('./package.json');
// Initialize Proxy Server, if defined.
if (process.env.GLOBAL_AGENT_HTTP_PROXY) {
logger.debug('invoke global agent proxy');
bootstrap();
}
// Process ENV Parameters
const env = cleanEnv(process.env, {
RSS_INTERVAL: num({ default: 5 }),
INC_ROOM: str(),
MAINT_ROOM: str(),
ANNOUNCE_ROOM: str(),
API_ROOM: str({ default: false }),
});
const parserService = require('./src/parserService');
// Define RSS Feeds
const incidentFeed = 'https://status.webex.com/incidents.rss';
const maintenanceFeed = 'https://status.webex.com/maintenances.rss';
const announcementFeed = 'https://status.webex.com/updates-upgrades.rss';
const apiFeed = 'https://developer.webex.com/api/content/changelog/feed';
// Load RSS Watcher Instances
const interval = env.RSS_INTERVAL * 60;
const incidentWatcher = new Watcher(incidentFeed, interval);
const maintenanceWatcher = new Watcher(maintenanceFeed, interval);
const announcementWatcher = new Watcher(announcementFeed, interval);
const apiWatcher = new Watcher(apiFeed, interval);
// Process Incident Feed
incidentWatcher.on('new entries', (entries) => {
entries.forEach((item) => {
// Identify Item Type
logger.debug('new incident item');
const typeIndex = item.description.indexOf('<strong >');
if (typeIndex !== -1) {
const typeEnd = item.description.indexOf('</strong >', typeIndex);
const itemType = item.description.substring(typeIndex + 9, typeEnd);
logger.debug(`detected as ${itemType}`);
switch (itemType) {
case 'resolved':
case 'monitoring':
case 'identified':
case 'investigating':
parserService.parseIncident(item, itemType);
break;
default:
logger.debug('EVENT: UNKNOWN');
logger.debug(item);
}
}
});
});
// Process Incident Feed
maintenanceWatcher.on('new entries', (entries) => {
entries.forEach((item) => {
// Identify Item Type
logger.debug('new maintenance item');
const typeIndex = item.description.indexOf('<strong >');
if (typeIndex !== -1) {
const typeEnd = item.description.indexOf('</strong >', typeIndex);
const itemType = item.description.substring(typeIndex + 9, typeEnd);
logger.debug(`detected as ${itemType}`);
switch (itemType) {
case 'scheduled':
case 'in progress':
case 'completed':
parserService.parseMaintenance(item, itemType);
break;
default:
logger.debug('EVENT: UNKNOWN');
logger.debug(item);
}
}
});
});
// Process Announcement Feed
announcementWatcher.on('new entries', (entries) => {
entries.forEach((item) => {
logger.debug('new announce item');
parserService.parseAnnouncement(item);
});
});
// Process API Feed
apiWatcher.on('new entries', (entries) => {
entries.forEach((item) => {
logger.debug('new api item');
parserService.parseApi(item);
});
});
// Handle Incident Feed Errors
incidentWatcher.on('error', (error) => {
logger.error(`Inc Feed Error: ${error}`);
});
// Handle Maintenance Feed Errors
maintenanceWatcher.on('error', (error) => {
logger.error(`Maint Feed Error: ${error}`);
});
// Handle Announcement Feed Errors
announcementWatcher.on('error', (error) => {
logger.warn(`Ann Feed Error: ${error}`);
});
// Handle API Feed Errors
apiWatcher.on('error', (error) => {
logger.warn(`Api Feed Error: ${error}`);
});
// Init Function
async function init() {
logger.info(`Webex RSS Integration, v${version}`);
try {
const bot = await parserService.getBot();
logger.info(`Bot Loaded: ${bot.displayName} (${bot.emails[0]})`);
} catch (error) {
logger.error('ERROR: Unable to load Webex Bot, check Token.');
logger.debug(error.message);
process.exit(2);
}
try {
const incRoom = await parserService.getRoom(env.INC_ROOM);
logger.info(`Inc Room: ${incRoom.title}`);
} catch (error) {
logger.error('ERROR: Bot is not a member of the Incident Room!');
process.exit(2);
}
try {
const maintRoom = await parserService.getRoom(env.MAINT_ROOM);
logger.info(`Maint Room: ${maintRoom.title}`);
} catch (error) {
logger.error('ERROR: Bot is not a member of the Maintenance Room!');
process.exit(2);
}
try {
const announceRoom = await parserService.getRoom(env.ANNOUNCE_ROOM);
logger.info(`Announce Room: ${announceRoom.title}`);
} catch (error) {
logger.error('ERROR: Bot is not a member of the Announcement Room!');
process.exit(2);
}
let apiEnabled = false;
if (env.API_ROOM) {
try {
const apiRoom = await parserService.getRoom(env.API_ROOM);
logger.info(`API Room: ${apiRoom.title}`);
apiEnabled = true;
} catch (error) {
logger.warn('WARN: Bot is not a member of the API Room!');
}
}
incidentWatcher.start();
maintenanceWatcher.start();
announcementWatcher.start();
if (apiEnabled) {
apiWatcher.start();
}
logger.info('Startup Complete!');
}
// Initiate
init();
// Handle Graceful Shutdown (CTRL+C)
process.on('SIGINT', () => {
logger.debug('Stopping...');
incidentWatcher.stop();
announcementWatcher.stop();
logger.debug('Feeds Stopped.');
process.exit(0);
});