@@ -40,8 +40,6 @@ module.exports = function(hydraterFunction, logger, errLogger) {
4040 logger ( "Starting task: " + ( ( task . file_path ) ? task . file_path : task . document . id ) ) ;
4141
4242 var path = '/tmp/AFH-' + crypto . randomBytes ( 20 ) . toString ( 'hex' ) ;
43- var domain = nodeDomain . create ( ) ;
44-
4543 async . waterfall ( [
4644 /**
4745 * Download the file from task.file_path, store it in a temporary file if there is file_path
@@ -76,21 +74,24 @@ module.exports = function(hydraterFunction, logger, errLogger) {
7674 }
7775 } ,
7876 function performHydration ( cb ) {
77+ var domain = nodeDomain . create ( ) ;
7978
8079 var timeout ;
8180 /**
8281 * Function to call, either on domain error, on hydration error or successful hydration.
8382 * Will clean the fs and dispose the domain for better performance.
8483 */
85- var onlyOnce = function ( err , changes ) {
86- if ( ! onlyOnce . called ) {
87- onlyOnce . called = true ;
84+ var cleaner = function ( err , changes ) {
85+ if ( ! cleaner . called ) {
86+ cleaner . called = true ;
87+ domain . exit ( ) ;
88+ domain . dispose ( ) ;
8889 cb ( err , changes ) ;
8990 }
9091 } ;
91- onlyOnce . called = false ;
92+ cleaner . called = false ;
9293
93- domain . on ( 'error' , onlyOnce ) ;
94+ domain . on ( 'error' , cleaner ) ;
9495
9596 // Run in a domain to prevent memory leak on crash
9697 domain . run ( function ( ) {
@@ -120,17 +121,17 @@ module.exports = function(hydraterFunction, logger, errLogger) {
120121
121122 // Wait for nexttick, to end this function and be able to properly GC it on domain.dispose().
122123 process . nextTick ( function ( ) {
123- onlyOnce ( err , changes ) ;
124+ cleaner ( err , changes ) ;
124125 } ) ;
125126 } ) ;
126127 } ) ;
127128
128129 timeout = setTimeout ( function ( ) {
129- if ( ! onlyOnce . called ) {
130+ if ( ! cleaner . called ) {
130131 var changes = { } ;
131132 changes . hydration_errored = true ;
132133 changes . hydration_error = "Task took too long." ;
133- onlyOnce ( null , changes ) ;
134+ cleaner ( null , changes ) ;
134135 }
135136 } , process . env . TIMEOUT || 60 * 1000 ) ;
136137 } ,
@@ -188,9 +189,6 @@ module.exports = function(hydraterFunction, logger, errLogger) {
188189 . end ( rarity . carry ( [ changes ] , cb ) ) ;
189190 }
190191 ] , function handleErrors ( err , changes , res ) {
191- domain . exit ( ) ;
192- domain . dispose ( ) ;
193-
194192 if ( err ) {
195193 // When the user asked for long polling, send error
196194 if ( task . next ) {
0 commit comments