This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaskQuery.js
369 lines (324 loc) · 7.29 KB
/
TaskQuery.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
var url = require("url");
var console = require("console");
var util = require("util");
var uuid = require("node-uuid");
var timers = require("timers");
var config = require("./config").config;
var whitespace_timer_interval = config.task_query.whitespace_timer_interval;
if (whitespace_timer_interval == undefined) {
whitespace_timer_interval = 10000;
}
var task_id_validation = config.task_query.task_id_validation;
if (task_id_validation == undefined) {
task_id_validation = /^[A-Za-z0-9\_]+$/;
}
var max_task_id_length = config.task_query.max_task_id_length;
if (max_task_id_length == undefined) {
max_task_id_length = 32;
}
var tq_ctr = 0;
function TaskQuery (task_id, request, response)
{
this.id = tq_ctr++;
this.task_id = task_id;
this.request = request;
this.response = response;
this.data_name = global.redisKeyName("D_" + task_id);
this.uuid = uuid.v4();
console.log(
"TaskQuery " + this.id + ": HTTP request from " +
request.connection.remoteAddress + ":" +
request.connection.remotePort + " (" +
request.url + ")"
);
}
TaskQuery.prototype.handleRequest = function ()
{
this.invoke();
}
TaskQuery.prototype.sendError = function (message)
{
global.sendError(this.response, message);
this.cleanup();
}
TaskQuery.prototype.sendJSON = function (obj)
{
global.sendJSON(this.response, obj);
this.cleanup();
}
TaskQuery.prototype.stopWhitespaceTimer = function ()
{
if (this.whitespace_timer_id) {
timers.clearInterval(this.whitespace_timer_id);
delete this.whitespace_timer_id;
}
}
TaskQuery.prototype.startWhitespaceTimer = function ()
{
if (this.whitespace_timer_id || whitespace_timer_interval == 0) {
return;
}
var tq = this;
this.whitespace_timer_id = timers.setInterval(
function () {
tq.response.write("\n");
},
whitespace_timer_interval
);
}
TaskQuery.prototype.cleanup = function ()
{
this.stopWhitespaceTimer();
this.request.connection.removeAllListeners("close");
this.request = null;
delete this.request;
this.response = null;
delete this.response;
}
function WatchTaskQuery (task_id, request, response)
{
TaskQuery.call(this, task_id, request, response);
this.channel_name = global.redisKeyName("SC_" + this.task_id);
}
util.inherits(WatchTaskQuery, TaskQuery);
WatchTaskQuery.prototype.channelName = function ()
{
return global.redisKeyName("SC_" + this.task_id);
}
WatchTaskQuery.prototype.invoke = function ()
{
var rl = global.redis_listener_pool.getListener();
this.request.connection.on("close", function () {
global.trace(
"TaskQuery " + tq.id + ": HTTP connection closed"
);
tq.cleanup();
return;
});
// Listen for changes to the data
var tq = this;
rl.addChan(this.channel_name, {
message: function (channel, message) {
tq.handleMessage(message);
},
subscribe: function (channel) {
tq.checkValue();
// Drop other requests waiting on this query
global.trace(
"TaskQuery " + tq.id + ": Killing other " +
"listeners"
);
global.redis_client.publish(
tq.channel_name,
JSON.stringify({
'status': 'kill',
'src': tq.uuid,
}
));
},
kill: function () {
global.trace(
"TaskQuery " + tq.id + ": killed due to " +
"internal detection of duplicate listener"
);
tq.killed();
}
});
// Set to object
this.rl = rl;
}
WatchTaskQuery.prototype.checkValue = function ()
{
// Start whitespace timer
this.startWhitespaceTimer();
// Get current value
var tq = this;
global.redis_client.get(this.data_name, function (err, value) {
// Make sure the get succeeded
if (err) {
cosole.warn(
"TaskQuery " + tq.id + ": Redis error: " +
err
);
tq.sendError("Redis error");
return;
}
// Make sure we got data
if (!value) {
global.trace(
"TaskQuery " + tq.id + ": No task in Redis"
);
tq.sendError("Not Found");
return;
}
// Parse data
var json;
try {
json = JSON.parse(value);
}
catch (err) {
console.warn(
"TaskQuery " + tq.id + ": JSON parse " +
"failure reading task data: " + err
);
tq.sendError(
"JSON parse failure reading task data: " +
err
);
return;
}
// Check status
var tstatus = json['status'];
if (tstatus == 'done') {
global.trace(
"TaskQuery " + tq.id + ": Sending task " +
"data from data key"
);
// We're already done! Send the data
tq.response.write(value);
tq.response.end();
tq.cleanup();
return;
}
});
}
WatchTaskQuery.prototype.killed = function ()
{
global.trace(
"TaskQuery " + this.id + " killed due to a concurrent request"
);
this.sendError(
"Killed due to another concurrent request for this task"
);
}
WatchTaskQuery.prototype.handleMessage = function (message)
{
// Parse message
var json;
try {
json = JSON.parse(message);
}
catch (err) {
console.warn(
"WatchTaskQuery " + tq.id + " ignoring SC " +
"message due to JSON parse error: " + err
);
return;
}
// Determine status
var tstatus = json['status'];
if (tstatus == 'kill') {
// Don't let a query kill itself
if (json['src'] == this.uuid) {
return;
}
// Dispatch kill command
this.killed();
return;
}
else if (tstatus == 'update' || tstatus == 'done') {
this.stopWhitespaceTimer();
if (json['data']) {
// Data included! Send it back
global.trace(
"TaskQuery " + this.id + ": Sending data " +
"from SC"
);
this.response.write(message);
this.response.end();
this.cleanup();
return;
}
else {
// Retrieve data
this.invokeReturnValue();
return;
}
}
else {
global.trace(
"WatchTaskQuery " + this.id + " ignoring unknown " +
"SC status update"
);
}
}
WatchTaskQuery.prototype.cleanup = function ()
{
// Only run cleanup once
if (this.cleaned_up) {
return;
}
this.cleaned_up = 1;
// Emit log
global.trace(
"TaskQuery " + this.id + ": Unsubscribing from channel: " +
this.channel_name
);
// Unsubscribe
this.rl.removeChan(this.channel_name);
// Destroy TaskQuery
TaskQuery.prototype.cleanup.call(this);
}
TaskQuery.prototype.invokeReturnValue = function ()
{
global.trace(
"TaskQuery " + this.id + ": Getting data for task " +
this.task_id
);
var tq = this;
global.redis_client.get(this.data_name, function (error, value) {
if (error) {
console.warn(
"TaskQuery " + tq.id + ": Redis error: " +
error
);
tq.sendError("Redis error");
return;
}
if (!value) {
console.warn(
"TaskQuery " + tq.id + ": No task in Redis"
);
tq.sendError("Not Found");
return;
}
tq.response.write(value);
tq.response.end();
tq.cleanup();
});
}
function PollTaskQuery (task_id, request, response)
{
TaskQuery.call(this, task_id, request, response);
}
util.inherits(PollTaskQuery, TaskQuery);
PollTaskQuery.prototype.invoke = function ()
{
this.invokeReturnValue();
}
function handleRequest (task_id, request, response)
{
// Validate task_id
if (!task_id || !task_id.match(task_id_validation)
|| task_id.length > max_task_id_length) {
sendError(response, "Not Found");
return;
}
// Determine operation mode
var qstr = url.parse(request.url).query;
if (qstr == "watch") {
var tq = new WatchTaskQuery(task_id, request, response);
tq.invoke();
return tq;
}
else if (qstr == "poll" || !qstr) {
var tq = new PollTaskQuery(task_id, request, response);
tq.invoke();
return tq;
}
else {
sendError(response, "Invalid mode");
return;
}
}
exports.handleRequest = handleRequest;