Skip to content

Commit 80d7acf

Browse files
author
Amoki
committed
download file in child process
1 parent 1c12b74 commit 80d7acf

File tree

3 files changed

+55
-48
lines changed

3 files changed

+55
-48
lines changed

lib/helpers/child-process.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
11
"use strict";
2+
3+
var crypto = require('crypto');
24
var async = require("async");
5+
var fs = require('fs');
6+
var request = require('supertest');
7+
var url = require('url');
8+
var rarity = require('rarity');
9+
310

411
process.on('message', function(task) {
512
var hydrate = require(task.functionPath);
13+
var path = '/tmp/AFH-' + crypto.randomBytes(20).toString('hex');
614
async.waterfall([
15+
/**
16+
* Download the file from task.file_path, store it in a temporary file if there is file_path
17+
*/
18+
function downloadFile(cb) {
19+
if(task.file_path) {
20+
// Download the file
21+
var stream = fs.createWriteStream(path);
22+
23+
// Store error if statusCode !== 200
24+
var err;
25+
stream.on("finish", function() {
26+
cb(err);
27+
});
28+
29+
var apiUrl = url.parse(task.file_path, false, true);
30+
var req = request(apiUrl.protocol + "//" + apiUrl.host)
31+
.get(apiUrl.path);
32+
33+
req.end().req.once('response', function(res) {
34+
if(res.statusCode !== 200) {
35+
err = 'Error when downloading file ' + task.file_path + ': ' + res.statusCode;
36+
stream.end();
37+
this.abort();
38+
}
39+
});
40+
req.pipe(stream);
41+
}
42+
else {
43+
cb(null);
44+
}
45+
},
746
function startHydration(cb) {
847
cb.urlCallback = task.options.urlCallback;
948
cb.apiUrl = task.options.apiUrl;
10-
hydrate(task.path, task.document, task.changes, cb);
49+
hydrate(path, task.document, task.changes, cb);
50+
},
51+
function cleanFile(changes, cb) {
52+
if(task.file_path) {
53+
fs.unlink(path, rarity.carry([changes], cb));
54+
}
1155
}
1256
],
1357
function(err, changes) {

lib/helpers/hydrater.js

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
var async = require('async');
1111
var shellFork = require('child_process').fork;
1212
var request = require('supertest');
13-
var crypto = require('crypto');
1413
var restify = require('restify');
15-
var fs = require('fs');
1614
var url = require('url');
1715
var rarity = require('rarity');
1816
var util = require('util');
@@ -39,39 +37,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
3937
return function(task, done) {
4038
logger("Starting task: " + ((task.file_path) ? task.file_path : task.document.id));
4139

42-
var path = '/tmp/AFH-' + crypto.randomBytes(20).toString('hex');
4340
async.waterfall([
44-
/**
45-
* Download the file from task.file_path, store it in a temporary file if there is file_path
46-
*/
47-
function downloadFile(cb) {
48-
if(task.file_path) {
49-
// Download the file
50-
var stream = fs.createWriteStream(path);
51-
52-
// Store error if statusCode !== 200
53-
var err;
54-
stream.on("finish", function() {
55-
cb(err);
56-
});
57-
58-
var apiUrl = url.parse(task.file_path, false, true);
59-
var req = request(apiUrl.protocol + "//" + apiUrl.host)
60-
.get(apiUrl.path);
61-
62-
req.end().req.once('response', function(res) {
63-
if(res.statusCode !== 200) {
64-
err = new restify.BadGatewayError('Error when downloading file ' + task.file_path + ': ' + res.statusCode);
65-
stream.end();
66-
this.abort();
67-
}
68-
});
69-
req.pipe(stream);
70-
}
71-
else {
72-
cb(null);
73-
}
74-
},
7541
function performHydration(cb) {
7642
var child = shellFork(__dirname + '/child-process.js', {silent: true});
7743
var stderr = "";
@@ -86,6 +52,12 @@ module.exports = function(hydraterFunction, logger, errLogger) {
8652
cleaner.called = true;
8753
cb(err, changes);
8854
}
55+
if(stdout !== "") {
56+
logger('stdout', stdout);
57+
}
58+
if(stderr !== "") {
59+
errLogger('stderr', stderr);
60+
}
8961
clearTimeout(timeout);
9062
};
9163
cleaner.called = false;
@@ -125,7 +97,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
12597

12698
child.send({
12799
functionPath: hydraterFunction,
128-
path: (task.file_path) ? path : null,
100+
file_path: task.file_path,
129101
document: task.document,
130102
changes: lib.defaultChanges(),
131103
options: options,
@@ -142,7 +114,6 @@ module.exports = function(hydraterFunction, logger, errLogger) {
142114
err = null;
143115
}
144116
cleaner(err, res.changes);
145-
146117
});
147118

148119
timeout = setTimeout(function() {
@@ -215,21 +186,13 @@ module.exports = function(hydraterFunction, logger, errLogger) {
215186
if(task.next) {
216187
task.next(new restify.InvalidContentError("ERR hydrating " + ((task.file_path) ? task.file_path : task.document.id) + err.toString()));
217188
}
218-
errLogger("ERR hydrating " + ((task.file_path) ? task.file_path : task.document.id), err);
189+
errLogger("ERR hydrating " + ((task.file_path) ? task.file_path : task.document.id), err.toString());
219190
}
220191

221192
if(res && res.statusCode && res.statusCode !== 204) {
222193
errLogger("ERR hydrating: server refused data! Code:" + res.statusCode);
223194
}
224-
225-
if(task.file_path) {
226-
fs.unlink(path, function(_err) {
227-
done(err || _err, changes);
228-
});
229-
}
230-
else {
231-
done(changes);
232-
}
195+
done(err, changes);
233196
});
234197
};
235198
};

test/helpers/hydrater.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe("hydrate()", function() {
3636

3737
hydrate(task, function(err, changes) {
3838
changes.should.have.keys(["metadata"]);
39-
done();
39+
done(err);
4040
});
4141
});
4242

0 commit comments

Comments
 (0)