node fibers - Why does an insert() break wrapAsync'd child_process.spawn() handlers in a Meteor method? -


i'm trying use child_process.spawn() in meteor method. want capture pid, stdout, stderr, , exit code external process, , store in database.

everything worked until added first insert() call. insert(), 1 'dummy' document inserted database. no error messages in server console. if comment out first insert(), other insert() calls succeed.

// server/app.js var spawn = npm.require('child_process').spawn;  meteor.methods({   start: function() {     var child = spawn('ls', ['/tmp']);     var pid = child.pid;      var wrappedchildstdouton = meteor.wrapasync(child.stdout.on, child.stdout);     var wrappedchildstderron = meteor.wrapasync(child.stderr.on, child.stderr);     var wrappedchildon = meteor.wrapasync(child.on, child);      // insert() breaks upcoming insert() calls!     stuff.insert({pid: pid, date: new date(), type: 'dummy', data: 'dummy'});      wrappedchildstdouton('data',  function (data) {       stuff.insert({pid: pid, date: new date(), type: 'stdout', data: data.tostring()});     });      wrappedchildstderron('data', function (data) {       stuff.insert({pid: pid, date: new date(), type: 'stderr', data: data.tostring()});     });      wrappedchildon('exit', function (code) {       stuff.insert({pid: pid, date: new date(), type: 'exit', code: code});     });   } }); 

what's first insert() call?

here's meteor app demonstrating issue.

the insert takes bit of time, ls finishes output before insert completes. time you've put event handler in place, it's late.

you can fix issue moving first insert end, moving first insert before spawn call, or adding no-op function () {} callback insert it's called asynchronously.


Comments