Skip to content

Commit 0afa6ab

Browse files
author
Amoki
committed
remove long poll
1 parent 6273b99 commit 0afa6ab

File tree

4 files changed

+35
-100
lines changed

4 files changed

+35
-100
lines changed

lib/handlers/hydrater.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,20 @@ module.exports = function(req, res, server, logger, next) {
2121
return next(new restify.MissingParameterError('No specified callback'));
2222
}
2323

24+
res.send(202);
25+
next();
26+
2427
// Prepare the new task
2528
var task = {};
2629
task.file_path = (req.params.file_path) ? req.params.file_path : null;
2730
task.callback = req.params.callback;
2831

29-
if(req.params.long_poll) {
30-
task.res = res;
31-
task.next = next;
32-
task.long_poll = true;
33-
}
34-
else {
35-
res.send(202);
36-
next();
37-
}
3832

3933
task.document = req.params.document || {};
4034
task.document.metadata = task.document ? task.document.metadata || {} : {};
4135
task.document.data = task.document ? task.document.data || {} : {};
4236

43-
44-
if(req.params.long_poll) {
45-
task.priority = 100;
46-
}
47-
else {
48-
task.priority = (req.params.priority) ? parseInt(req.params.priority) : 0;
49-
}
37+
task.priority = (req.params.priority) ? parseInt(req.params.priority) : 0;
5038

5139
// Push it to the queue
5240
server.queue.push(task, -task.priority);

lib/helpers/hydrater.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
var async = require('async');
1111
var request = require('supertest');
12-
var restify = require('restify');
1312
var url = require('url');
1413
var rarity = require('rarity');
1514
var util = require('util');
@@ -184,18 +183,12 @@ module.exports = function(hydraterFunction, concurrency, logger, errLogger) {
184183

185184
logger("End of task: " + ((task.file_path) ? task.file_path : task.document.id));
186185

187-
// When long_polling, it is possible we don't have a callback
188-
if(task.callback) {
189-
var apiUrl = url.parse(task.callback, false, true);
186+
var apiUrl = url.parse(task.callback, false, true);
190187

191-
request(apiUrl.protocol + "//" + apiUrl.host)
192-
.patch(apiUrl.path)
193-
.send(changes)
194-
.end(rarity.carry([changes], cb));
195-
}
196-
else {
197-
cb(null, changes);
198-
}
188+
request(apiUrl.protocol + "//" + apiUrl.host)
189+
.patch(apiUrl.path)
190+
.send(changes)
191+
.end(rarity.carry([changes], cb));
199192
}
200193
], function handleErrors(err, changes, res) {
201194
async.waterfall([
@@ -211,20 +204,7 @@ module.exports = function(hydraterFunction, concurrency, logger, errLogger) {
211204
cb(null);
212205
},
213206
function forwardError(cb) {
214-
if(!err) {
215-
if(task.long_poll) {
216-
task.res.send(changes);
217-
task.next();
218-
}
219-
220-
return cb(null);
221-
}
222-
223-
if(task.long_poll) {
224-
task.next(new restify.InvalidContentError("ERR hydrating " + ((task.file_path) ? task.file_path : task.document.id) + err.toString()));
225-
cb(null);
226-
}
227-
else {
207+
if(err) {
228208
var apiUrl = url.parse(task.callback, false, true);
229209

230210
request(apiUrl.protocol + "//" + apiUrl.host)
@@ -234,6 +214,9 @@ module.exports = function(hydraterFunction, concurrency, logger, errLogger) {
234214
})
235215
.end(cb);
236216
}
217+
else {
218+
cb(null);
219+
}
237220
}
238221
], function(internalErr) {
239222
if(internalErr) {

test/errors.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,38 @@ describe('Errors', function() {
5151
};
5252
});
5353

54-
it('should be handled gracefully with long_poll option while hydrating', function(done) {
55-
request(hydrationServer).post('/hydrate')
56-
.send({
57-
file_path: 'http://127.0.0.1:4243/afile',
58-
callback: 'http://127.0.0.1:4243/result',
59-
document: {
60-
metadata: {
61-
"foo": "bar"
62-
},
63-
},
64-
'long_poll': true
65-
})
66-
.expect(400)
67-
.expect(/err/i)
68-
.expect(/buggy/i)
69-
.end(done);
70-
});
7154

7255
it('should be handled gracefully if file does not exists', function(done) {
7356
this.timeout(10000);
57+
58+
var fakeApi = require('./helpers/fake-api.js')();
59+
fakeApi.patch('/result', function(req, res, next) {
60+
res.send(204);
61+
next();
62+
if(req.params.hydration_error === 'Error when downloading file http://127.0.0.1:4244/notafile: 404') {
63+
done();
64+
}
65+
else {
66+
done(new Error("Should send an error"));
67+
}
68+
fakeApi.close();
69+
70+
});
71+
72+
fakeApi.listen(4244);
73+
7474
request(hydrationServer).post('/hydrate')
7575
.send({
76-
file_path: 'http://127.0.0.1:4243/notafile',
77-
callback: 'http://127.0.0.1:4243/result',
76+
file_path: 'http://127.0.0.1:4244/notafile',
77+
callback: 'http://127.0.0.1:4244/result',
7878
document: {
7979
metadata: {
8080
"foo": "bar"
8181
},
82-
},
83-
'long_poll': true
82+
}
8483
})
85-
.expect(400)
86-
.expect(/err/i)
87-
.expect(/downloading file/i)
88-
.end(done);
84+
.expect(202)
85+
.end(function() {});
8986
});
9087
});
9188

test/index.js

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,6 @@ describe('POST /hydrate', function() {
4444
.end(done);
4545
});
4646

47-
it('should accept request without callback when long_polling', function(done) {
48-
request(server).post('/hydrate')
49-
.send({
50-
'file_path': 'http://127.0.0.1:4243/afile',
51-
'long_poll': true,
52-
'document': {
53-
'metadata': {},
54-
}
55-
})
56-
.expect(200)
57-
.end(done);
58-
});
59-
6047
it('should immediately return 202', function(done) {
6148
this.timeout(300);
6249
request(server)
@@ -71,26 +58,6 @@ describe('POST /hydrate', function() {
7158
.expect(202)
7259
.end(done);
7360
});
74-
75-
it('should allow for long polling', function(done) {
76-
this.timeout(10000);
77-
78-
request(server)
79-
.post('/hydrate')
80-
.send({
81-
'file_path': 'http://127.0.0.1:4243/afile',
82-
'callback': 'http://127.0.0.1:4243/callback',
83-
'long_poll': true,
84-
'document': {
85-
'metadata': {},
86-
}
87-
})
88-
.expect(200)
89-
.expect(function(res) {
90-
res.body.should.have.property('metadata').and.have.property('hydrated').and.equal(true);
91-
})
92-
.end(done);
93-
});
9461
});
9562

9663

0 commit comments

Comments
 (0)