为什么 insert() 会破坏 Meteor 方法中的 wrapAsync'd child_process.spawn() 处理程序?
Why does an insert() break wrapAsync'd child_process.spawn() handlers in a Meteor method?
我正在尝试在 Meteor 方法中使用 child_process.spawn()
。我想从外部进程捕获 PID、stdout、stderr 和退出代码,并将所有这些存储在数据库中。
一切正常,直到我添加第一个 insert()
调用。使用那个 insert()
,只有一个 'dummy' 文档被插入到数据库中。我在服务器控制台中没有收到任何错误消息。如果我注释掉第一个 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()
电话怎么了?
insert
需要一些时间,因此 ls
在 insert
完成之前完成其输出。当您将事件处理程序放置到位时,为时已晚。
您可以通过将第一个 insert
移到最后,将第一个 insert
移到 spawn
调用之前,或添加一个空操作 function () {}
回调到 insert
所以它被异步调用。
我正在尝试在 Meteor 方法中使用 child_process.spawn()
。我想从外部进程捕获 PID、stdout、stderr 和退出代码,并将所有这些存储在数据库中。
一切正常,直到我添加第一个 insert()
调用。使用那个 insert()
,只有一个 'dummy' 文档被插入到数据库中。我在服务器控制台中没有收到任何错误消息。如果我注释掉第一个 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()
电话怎么了?
insert
需要一些时间,因此 ls
在 insert
完成之前完成其输出。当您将事件处理程序放置到位时,为时已晚。
您可以通过将第一个 insert
移到最后,将第一个 insert
移到 spawn
调用之前,或添加一个空操作 function () {}
回调到 insert
所以它被异步调用。