-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscampi.js
executable file
·254 lines (200 loc) · 7.88 KB
/
scampi.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/node
var fs = require('fs');
var ini = require('inireader');
var commander = require('commander');
var stomp = require('stomp');
var spawn = require('child_process').spawn;
var fork = require('child_process').fork;
var _ = require('underscore');
var path = require('path');
var sleep = require('sleep');
var log4js = require('log4js');
var client_id;
var logger = null;
var message_count = 0;
var messaging_server;
var messaging_port;
var debug = false;
function setup(callback) {
commander.option('-c, --config <path>',
'Specify an alternate configuration file path.')
.option('-l, --listen <path>',
'Specify the path to the listeners configuration JSON file.')
.option('--logfile <path>',
'Specify the path to the logfile (overrides log configuration directive).')
.parse(process.argv);
var parser = new ini.IniReader();
// Load and parse the configuration file. Take into account whether an
// alternate (non-default) configuration file path was specified or not.
var config_path = commander.config;
if (config_path === null || typeof config_path === 'undefined') {
config_path = path.join(__dirname, "conf", "config.ini");
}
parser.load(config_path);
// Determine if we are in debug mode or not
var debug_param = parser.param("global.debug");
if (debug_param.trim().toLowerCase() === "true") {
debug = true;
}
// First honor the command line option. If not provided, then use the setting in the
// configuration file. If there is nothing there either, then we log locally.
var log_file = commander.logfile;
if (log_file === null || log_file.length === 0) {
log_file = parser.param("global.logfile");
if (log_file === null || log_file.length === 0) {
console.log("No logfile config item found under the 'global' " +
"section. Using logs/listener.log");
log_file = path.join(__dirname, "logs", "listener.log");
}
}
// Okay, we know where to log to, so set up the logger...
set_logging(log_file.trim());
logger.info("Log file used: " + log_file);
messaging_server = parser.param("messaging.messaging_server");
messaging_port = parser.param("messaging.messaging_port");
messaging_port = parser.param("messaging.messaging_port");
client_id = parser.param("messaging.client_id");
// Load and parse the configuration file. Take into account whether an
// alternate (non-default) configuration file path was specified or not.
var listener_json_path = commander.listen;
if (listener_json_path === null || typeof listener_json_path === 'undefined') {
listener_json_path = path.join(__dirname, "conf", "listen.json");
}
start_listening(listener_json_path, callback);
}
function start_listening(listener_path, callback) {
logger.debug("Reading in the listener configuration from " + listener_path);
fs.readFile(listener_path, function (err, data) {
if (err) {
logger.error("Problem reading the listern descriptor.", err);
throw err;
}
var endpoints = null;
try {
endpoints = JSON.parse(data);
} catch (parse_error) {
logger.error("Unable to parse JSON listener descriptor.", parse_error);
}
if (endpoints !== null) {
// login and passcode are optional (required by rabbitMQ)
var stomp_args = {
host: messaging_server,
port: messaging_port,
'client-id': client_id,
debug: debug
};
var client = new stomp().Client(stomp_args)
callback(client, endpoints);
} else {
logger.error("No endpoint data. Exiting.");
process.exit(1);
}
});
}
function setup_listeners(client, endpoints) {
logger.debug("In setup_listeners.");
client.on('connected', function() {
logger.info("Connected to " + messaging_server + ".");
var destination = null;
for (destination in endpoints) {
var headers = { destination: destination,
'activemq.subcriptionName': client_id,
'activemq.subscriptionName': client_id,
ack: "client"
};
logger.info("Subscribing to " + destination);
client.subscribe(headers);
}
});
client.on('message', function(message) {
var message_id = message['headers']['message-id'];
client.ack(message_id);
message_count++;
logger.debug("Got message #" + message_count + ". ID: " + message_id);
if ( endpoints.hasOwnProperty(message['headers']['destination']) ) {
var consumers = endpoints[ message['headers']['destination'] ];
_.each( consumers, function(consumer) {
var text = message['body'][0];
if (consumer.hasOwnProperty('user') && consumer.hasOwnProperty('script')) {
var script = consumer['script'];
var username = consumer['user'];
var job_spec = { text: text,
user: username,
script: script };
logger.info("Invoking " + script + " as user: " + username);
try {
var forked = fork("forked.js");
forked.send(job_spec);
} catch (e) {
logger.error("Error invoking " + script);
}
} else {
logger.error("Invalid spec encountered. Both 'user' and 'script' and required.");
}
});
}
});
client.on('disconnected', function(error_frame) {
logger.error("Detected disconnect. Attempting to reconnect.")
reconnect(client);
});
client.on('error', function(error_frame) {
logger.error("Error: " + error_frame['body']);
reconnect(client);
});
client.connect({'client-id': client_id });
}
function reconnect(client) {
logger.debug("In reconnect.");
if (client !== null && typeof(client) !== "undefined") {
logger.debug("Disconnecting.");
client.disconnect();
}
client = null;
// Have a 10 second pause before we re-establish ourselves.
var sleepSecs = 10;
logger.debug("Sleeping for " + sleepSecs + " seconds.");
sleep.sleep(sleepSecs);
setup(function(c, endpoints) {
client = c;
setup_listeners(client, endpoints);
});
}
function set_logging(log_file) {
log4js.clearAppenders();
log4js.loadAppender('file');
log4js.loadAppender('console');
log4js.addAppender(log4js.appenders.file(log_file), 'main');
log4js.addAppender(log4js.appenders.console(log4js.messagePassThroughLayout), 'console');
// Set the logger
logger = log4js.getLogger('main');
}
function main() {
var client;
setup(function(new_client, endpoints) {
client = new_client;
setup_listeners(client, endpoints);
});
process.on('SIGTERM', function() {
logger.info("Detected termination.");
if (client !== null && typeof(client) !== "undefined") {
logger.info("Disconnecting...");
client.disconnect();
client = null;
}
process.exit(1);
});
process.on('SIGINT', function() {
logger.info("Detected interruption.");
if (client !== null && typeof(client) !== "undefined") {
logger.info("Disconnecting...");
client.disconnect();
client = null;
}
process.exit(1);
});
process.on('uncaughtException', function(err) {
logger.error("Caught exception: ", err.stack);
});
}
main();