【发布时间】:2016-01-06 20:49:06
【问题描述】:
我正在编写一个 Meteor 应用程序,它在服务器上执行多个 shell 命令。我想要从服务器到客户端的实时输出,但我无法弄清楚实时部分。我不想等待命令完成 - 因为它可能需要很长时间。
现在我已经创建了一个 Logs Mongo 集合来存储输出。但是我收到了这样的错误:“错误:Meteor 代码必须始终在 Fiber 中运行。尝试包装你传递给非 Meteor 库的回调Meteor.bindEnvironment。”
听起来我 Meteor 想让我等到所有输出都写完。我不想 wrapAsync 因为我希望在客户端逐行打印异步输出。 Spawn 在服务器上返回一个流,以便覆盖这一侧,我只是在流向客户端时遇到问题。这是一个例子:
Logs = new Mongo.Collection("logs");
if (Meteor.isClient) {
Template.body.helpers({
log: function(branchName) {
return Logs.find({});
}
});
Template.branch.events({
'click button#start': function(event, template) {
Meteor.call('startStack', template.data, function(error, result) {
if(error){
console.log(error);
} else {
console.log('response: ', result);
}
});
}
});
}
if (Meteor.isServer) {
spawn = Npm.require('child_process').spawn;
Meteor.methods({
startStack: function(branch) {
command = spawn('sh', ['-c', "ls && sleep 2 && ls -l && sleep 3 && ls -la"]);
Logs.update({ branch: branch['name'] }, { branch: branch['name'], text:''}, { upsert : true });
command.stdout.on('data', function (data) {
// TODO: concat to existing text
Logs.update({ branch: branch['name'] }, { branch: branch['name'], text: ''+data});
});
}
});
}
【问题讨论】: