【问题标题】:Why does an insert() break wrapAsync'd child_process.spawn() handlers in a Meteor method?为什么 insert() 会在 Meteor 方法中破坏 wrapAsync 的 child_process.spawn() 处理程序?
【发布时间】:2015-07-19 00:23:37
【问题描述】:

我正在尝试在 Meteor 方法中使用 child_process.spawn()。我想从外部进程捕获 PID、stdout、stderr 和退出代码,并将所有这些存储在数据库中。

在我添加第一个 insert() 调用之前,一切正常。使用insert(),只会将一个“虚拟”文档插入到数据库中。我在服务器控制台中没有收到错误消息。如果我注释掉第一个insert(),其他insert() 调用成功。

// 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);

    // this 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});
    });
  }
});

第一个insert() 电话是怎么回事?

Here's a Meteor app demonstrating the issue.

【问题讨论】:

    标签: meteor node-fibers


    【解决方案1】:

    insert 需要一点时间,所以lsinsert 完成之前完成其输出。当您将事件处理程序放置到位时,为时已晚。

    您可以通过将第一个 insert 移动到末尾,将第一个 insert 移动到 spawn 调用之前,或将无操作 function () {} 回调添加到 insert 来解决此问题,因此它被调用异步。

    【讨论】:

    • 啊,当然! Meteor 确保在insert() 完成之前不会执行以下行。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多