user1995872
user1995872

Reputation: 31

child_process.spawn in meteor

Has anyone had any success at all using node's childprocess.spawn() on meteor on any platform? I've tried it on both OS X and Windows as follows and the app crashes immediately:

if (Meteor.isServer) {
  Meteor.startup(function() {
    cmd = __meteor_bootstrap__.require('child_process').spawn('irb', [], {detached: true, stdio:'pipe'});
    cmd.stdout.on('data', function(data){
      Fiber(function(){
        Replies.remove({});
        Replies.insert({message: data});
      }).run();
    });

 });
}

In the console, I get the following message on OS X and a similar one on Windows:

Assertion failed: (handle->InternalFieldCount() > 0), function Unwrap, file ../src/node_object_wrap.h, line 61.
Exited from signal: SIGABRT

Does anyone have any thoughts?

Thanks!
-Greg

Upvotes: 3

Views: 1065

Answers (1)

Andrew Wilcox
Andrew Wilcox

Reputation: 1021

data is a node Buffer which can't be inserted into a collection; convert it to a string first.

Also note your data event callback will be called multiple times as data is streamed from the child process (unless the output is so small that you happen to get it all in one buffer). You'll want to accumulate the data in a buffer and then insert it in your collection when you get the stream end event.

If there is any chance that your child process will be outputing utf-8 (anything other than pure ASCII), make sure to accumulate the data in a node Buffer first, and then convert the entire Buffer to a string, rather than converting each chunk of data to a string and accumulating the data as a string. (utf-8 characters can span multiple bytes, so you can't chop a byte stream into arbitrary pieces and parse each piece as utf-8 separately).

Upvotes: 1

Related Questions