-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (129 loc) · 4.16 KB
/
index.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
var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.listen(3001);
var long_running_req = {};
var id = 0;
function getTarget(req) {
return {
// host to forward to
host: 'localhost',
// port to forward to
port: 3000,
// path to forward to
path: '/wait/10000',
// request method
method: req.method,
// headers to send
//headers: req.headers
};
}
function getStatus(id) {
return {
status: long_running_req[id].status,
started: long_running_req[id].started,
finished: long_running_req[id].finished,
aborted: long_running_req[id].aborted,
statusCode: long_running_req[id].statusCode,
response_body: long_running_req[id].response_body
};
}
//extra route for debugging purposes
app.get('/jobs/:id/status', function(req, res) {
if (!long_running_req[req.params.id]) {
res.status(404).end();;
return;
}
res.send(getStatus(req.params.id));
});
app.get('/jobs/:id', function(req, res) {
if (!long_running_req[req.params.id]) {
res.status(404).end();;
return;
}
if (long_running_req[req.params.id].status == 'end') {
//redirect to the output if the job is complete
res.redirect('/jobs/' + req.params.id + '/output');
} else {
res.send(getStatus(req.params.id));
}
});
app.get('/jobs/:id/output', function(req, res) {
if (!long_running_req[req.params.id]) {
res.status(404).end();
return;
}
res.send(long_running_req[req.params.id].response_body);
});
app.delete('/jobs/:id/output', function(req, res) {
if (!long_running_req[req.params.id]) {
res.status(404).end();
return;
}
long_running_req[req.params.id].response_body = "";
res.status(200).end();
});
app.delete('/jobs/:id', function(req, res) {
if (!long_running_req[req.params.id]) {
res.status(404).end();
return;
}
if (long_running_req[req.params.id].status != 'end') {
long_running_req[req.params.id]._creq.abort();
};
delete long_running_req[req.params.id];
res.status(200).end();
});
app.all('/', function(req, res) {
var myid = id++;
console.log(myid + " pending");
long_running_req[myid] = { status: 'pending' };
res.status(202);
res.set('Location', '/jobs/' + myid);
res.end();
//alternatively to test from the browser
//res.redirect('/jobs/'+myid);
long_running_req[myid].response_body = "";
long_running_req[myid].started = new Date();
//configure URL parameters for the call which make take a long time
var options = getTarget(req);
console.log(options);
var creq = http.request(options, function(cres) {
// set encoding
cres.setEncoding('utf8');
cres.on('response', function(chunk) {
console.log(myid + ' response');
long_running_req[myid].status = 'response';
long_running_req[myid].statusCode = cres.statusCode;
});
// wait for data
cres.on('data', function(chunk) {
console.log(myid + ' data');
long_running_req[myid].status = 'data';
long_running_req[myid].response_body += chunk;
});
cres.on('aborted', function() {
console.log(myid + ' aborted');
long_running_req[myid].status = 'aborted';
long_running_req[myid].aborted = new Date();
});
cres.on('end', function() {
console.log(myid + ' end');
long_running_req[myid].statusCode = cres.statusCode;
long_running_req[myid].status = 'end';
long_running_req[myid].finished = new Date();
});
}).on('error', function(e) {
if (long_running_req[myid]) {
long_running_req[myid].status = 'error';
long_running_req[myid].error = e.message;
}
console.log(e.message);
});
creq.end();
console.log(myid + " sent");
long_running_req[myid].status = 'sent';
long_running_req[myid]._creq = creq;
});