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,36 +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- var apiUrl = url . parse ( task . file_path , false , true ) ;
50- request ( apiUrl . protocol + "//" + apiUrl . host )
51- . get ( apiUrl . path )
52- . expect ( 200 )
53- . end ( function ( err , res ) {
54- if ( err ) {
55- err = new restify . BadGatewayError ( 'Error when downloading file ' + task . file_path + ': ' + err ) ;
56- }
57- cb ( err , res && res . text ) ;
58- } ) ;
59- }
60- else {
61- cb ( null ) ;
62- }
63- } ,
64- function saveFile ( res , cb ) {
65- if ( res ) {
66- fs . writeFile ( path , res , cb ) ;
67- }
68- else {
69- cb ( ) ;
70- }
71- } ,
7241 function performHydration ( cb ) {
7342 var child = shellFork ( __dirname + '/child-process.js' , { silent : true } ) ;
7443 var stderr = "" ;
@@ -83,6 +52,12 @@ module.exports = function(hydraterFunction, logger, errLogger) {
8352 cleaner . called = true ;
8453 cb ( err , changes ) ;
8554 }
55+ if ( stdout !== "" ) {
56+ logger ( stdout ) ;
57+ }
58+ if ( stderr !== "" ) {
59+ errLogger ( stderr ) ;
60+ }
8661 clearTimeout ( timeout ) ;
8762 } ;
8863 cleaner . called = false ;
@@ -122,7 +97,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
12297
12398 child . send ( {
12499 functionPath : hydraterFunction ,
125- path : ( task . file_path ) ? path : null ,
100+ file_path : task . file_path ,
126101 document : task . document ,
127102 changes : lib . defaultChanges ( ) ,
128103 options : options ,
@@ -139,7 +114,6 @@ module.exports = function(hydraterFunction, logger, errLogger) {
139114 err = null ;
140115 }
141116 cleaner ( err , res . changes ) ;
142-
143117 } ) ;
144118
145119 timeout = setTimeout ( function ( ) {
@@ -212,21 +186,13 @@ module.exports = function(hydraterFunction, logger, errLogger) {
212186 if ( task . next ) {
213187 task . next ( new restify . InvalidContentError ( "ERR hydrating " + ( ( task . file_path ) ? task . file_path : task . document . id ) + err . toString ( ) ) ) ;
214188 }
215- 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 ( ) ) ;
216190 }
217191
218192 if ( res && res . statusCode && res . statusCode !== 204 ) {
219193 errLogger ( "ERR hydrating: server refused data! Code:" + res . statusCode ) ;
220194 }
221-
222- if ( task . file_path ) {
223- fs . unlink ( path , function ( _err ) {
224- done ( err || _err , changes ) ;
225- } ) ;
226- }
227- else {
228- done ( changes ) ;
229- }
195+ done ( err , changes ) ;
230196 } ) ;
231197 } ;
232198} ;
0 commit comments