1010var async = require ( 'async' ) ;
1111var shellFork = require ( 'child_process' ) . fork ;
1212var request = require ( 'supertest' ) ;
13- var crypto = require ( 'crypto' ) ;
1413var restify = require ( 'restify' ) ;
15- var fs = require ( 'fs' ) ;
1614var url = require ( 'url' ) ;
1715var rarity = require ( 'rarity' ) ;
1816var 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} ;
0 commit comments