短篇故事:
如果方法执行时间超过 120 秒,Meteor 方法就会循环。
这就是我测试的方式
服务器端有一个名为“build”的 Meteor 方法,如下所示:
'build': function(buildRequest) {
log.info('method build get called: buildRequest=%s', JSON.stringify(buildRequest));
shell.exec('echo "simulate long Android build task."');
shell.exec('sleep ' + buildRequest.sec);
var result = {'successful': true, 'output': "OK", 'fileName': 'test.apk', 'genFile': '/tmp/test.apk'}
log.info("method return result=%s", JSON.stringify(result));
return result;
}
我设置调用此方法的路由如下:
this.route('buildApp', {
where: 'server'
, action: function() {
var buildRequest = this.request.query;
log.info('buildApp: http parameters: ', buildRequest);
var result = methods.build(buildRequest);
var response = this.response;
if (result.successful) {
methods.download(response, result.genFile, result.fileName);
}
else {
response.writeHead(500, {});
response.end("server has error: " + result.output);
}
}
})
然后,我调用 url
http://localhost:3000/buildApp?app=test&server=dev&db=DB&appId=test&sec=120
然后,构建方法循环
=> App running at: http://localhost:3000/
I20150109-14:55:45.285(9)? info: buildApp: http parameters: app=test, server=dev, db=DB, appId=test, sec=120
I20150109-14:55:45.358(9)? info: method build get called: buildRequest={"app":"test","server":"dev","db":"DB","appId":"test","sec":"120"}
I20150109-14:55:45.358(9)? simulate long Android build task.
I20150109-14:57:45.359(9)? info: method return result={"successful":true,"output":"OK","fileName":"test.apk","genFile":"/tmp/test.apk"}
I20150109-14:57:45.387(9)? info: buildApp: http parameters: app=test, server=dev, db=DB, appId=test, sec=120
I20150109-14:57:45.387(9)? info: method build get called: buildRequest={"app":"test","server":"dev","db":"DB","appId":"test","sec":"120"}
I20150109-14:57:45.446(9)? simulate long Android build task.
我认为它与此代码相关:
长话短说:
我用Meteor
制作了一个简单的Android应用程序构建屏幕。
一切正常,但是如果我提交表单来构建应用程序,它会一遍又一遍地构建。即使我停止服务器并重新启动,服务器一重新启动它就会再次调用。
如果表单填写并提交,我将调用 Meteor
'build' 方法。
该方法将克隆 git 存储库并通过调用下面的 shell 脚本来构建应用程序。
var exec = shell.exec('./genApp.sh ' + buildRequest.appId + " " + buildRequest.server + " " + buildRequest.db);
//var exec = shell.exec('echo "simple task will not loop"');
如果我调用 ./genApp.sh
(这将需要几分钟。),那么 Meteor 的“build”方法会自行循环。但如果我做简单的任务,它不会循环而是执行一次。
我在构建 Meteor 方法的开头添加了以下代码以停止它进行调试。但我不知道是什么原因造成的。
if (a == 1) {
a = 0;
throw new Error("Why call again?!");
}
++ a;
服务器日志:
I20150108-19:48:08.220(9)? info: success
I20150108-19:48:08.221(9)? info: return result=[object Object]
I20150108-19:48:09.034(9)? Exception while invoking method 'build' Error: Why call again?!
I20150108-19:48:09.035(9)? at [object Object].methods.build (app/javis.js:92:25)
I20150108-19:48:09.035(9)? at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1599:1)
I20150108-19:48:09.035(9)? at packages/ddp/livedata_server.js:648:1
I20150108-19:48:09.035(9)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20150108-19:48:09.035(9)? at packages/ddp/livedata_server.js:647:1
I20150108-19:48:09.035(9)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20150108-19:48:09.036(9)? at [object Object]._.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646:1)
I20150108-19:48:09.036(9)? at packages/ddp/livedata_server.js:546:1
主要源码
var APP_01 = 'app01-andy';
var APP_02 = 'app02-andy';
if (Meteor.isClient) {
Router.configure({
layoutTemplate: 'layout'
})
Router.map(function() {
this.route('/', 'home');
});
buildRequest = {
appId: 0
, server: 'dev'
, db: 'DEFAULT'
, app: APP_01
};
Session.set('successful', false);
Session.set('output', '');
Session.set('downloadLink', null);
Template.home.helpers({
successful: function() {
return Session.get('successful');
}
, output: function() {
return Session.get('output');
}
, downloadLink: function() {
var successful = Session.get('successful');
var downloadLink = Session.get('downloadLink');
console.log(downloadLink);
if (successful) {
$('#downloadLink').show();
} else {
$('#downloadLink').hide();
}
return downloadLink;
}
});
Template.home.events({
'submit .app-build-form': function() {
event.preventDefault();
buildRequest.appId = event.target.appId.value;
buildRequest.server = event.target.server.value;
buildRequest.db = event.target.db.value;
buildRequest.app = event.target.app.value;
$("#submit").prop('disabled', true);
Meteor.call('build', buildRequest, function(error, result) {
console.log(JSON.stringify(result));
Session.set('successful', result.successful);
Session.set('output', result.output);
Session.set('downloadLink', '/downloadApp?fullPathFile='+result.genFile+'&fileName='+result.fileName);
$("#submit").prop('disabled', false);
console.log("meteor call end");
});
console.log("submit finished");
// prevent default form submit.
return false;
},
'click #sel_app': function() {
console.log(event.target.value);
var app = event.target.value;
var selDb = $("#sel_db");
if (app === APP_02) {
selDb.val('APP_02_DB'); selDb.prop('disabled', true);
}
else {
selDb.prop('disabled', false);
}
}
});
}
if (Meteor.isServer) {
var shell = Meteor.npmRequire('shelljs');
var log = Meteor.npmRequire('winston');
var a = 0;
var methods = {
'build': function(buildRequest) {
if (a == 1) {
a = 0;
throw new Error("Why call again?!");
}
++ a;
log.info(JSON.stringify(buildRequest));
var dir = shell.pwd();
log.info("work dir: %s", dir);
shell.cd('/project/build/');
var branch = null;
var app = buildRequest.app;
if (app === APP_01) {
branch = '2.0';
}
else if (app === APP_02) {
branch = '1.0';
}
else {
branch = 'master';
}
shell.exec('rm -rf ' + buildRequest.app);
shell.exec('git clone -b '+branch+' ssh://git@company-build.com/'+buildRequest.app+'.git');
shell.cd(buildRequest.app + "/app");
var exec = shell.exec('./genApp.sh ' + buildRequest.appId + " " + buildRequest.server + " " + buildRequest.db);
//var exec = shell.exec('echo "simple task will not loop"');
var code = exec.code;
var output = exec.output;
log.info(code);
log.info(output);
var fileName = null;
var matches = output.match(/The package copied to (.+apk)/);
var genFile = null;
if (matches != null && matches.length > 1) {
genFile = matches[1];
log.info(genFile);
// TODO : do not write file in public, http://stackoverflow.com/questions/13201723/generating-and-serving-static-files-with-meteor
/*
shell.mkdir(process.env.PWD + '/tmp');
shell.cp('-f', genFile, process.env.PWD + '/tmp');
*/
fileName = genFile.substring(genFile.lastIndexOf('/') + 1);
}
matches = output.match(/BUILD SUCCESSFUL/);
var successful = false;
if (matches != null && matches.length > 0) {
log.info("success");
successful = true;
}
var result = {'successful': successful, 'output': output, 'fileName': fileName, 'genFile': genFile};
log.info("return result="+result);
return result;
}
, 'download' : function(response, fullPathFile, fileName) {
var stat = fs.statSync(fullPathFile);
response.writeHead(200, {
'Content-Type': 'application/vnd.android.package-archive'
, 'Content-Length': stat.size
, 'Content-Disposition': 'attachment; filename=' + fileName
});
fs.createReadStream(result.genFile).pipe(response);
}
};
Meteor.methods(methods);
fs = Npm.require('fs');
Router.map(function() {
this.route('buildApp', {
where: 'server'
, action: function() {
var buildRequest = this.request.query;
log.info(this.request.query);
log.info(buildRequest);
var result = methods.build(buildRequest);
var response = this.response;
if (result.successful) {
methods.download(response, result.genFile, result.fileName);
}
else {
response.writeHead(500, {});
response.end("server has error: " + result.output);
}
}
}),
this.route('downloadApp', {
where: 'server'
, action: function() {
var params = this.request.query;
var fullPathFile = params.fullPathFile;
var fileName = params.fileName;
methods.download(this.response, fullPathFile, fileName);
}
})
});
Meteor.startup(function () {
// code to run on server at startup
});
}
什么原因导致循环?任何帮助将不胜感激。
即使我调用 http://localhost:3000/buildApp?app=xxx&server=live&db=DB&appId=12423
,构建也会循环。
如果我将 ./genApp.sh
更改为简单的以缩小条件,就可以了。
#!/bin/bash
echo "test"
echo "The package copied to /service/release/20150108/existing-file.apk"
echo "BUILD SUCCESSFUL"
sleep 180
当它休眠 180 秒时,它会再次调用。怎么又打这个电话了?因为我直接调用url。我认为没有客户端代码重试。
最佳答案
嗯...可能是当 shell.exec
同步运行时,它“使事件循环处于饥饿状态”,从而导致一些问题。
解决方案是使用 Future
,它允许 Meteor 在另一个 Fiber
中继续运行,同时等待 shell.exec
完成.
var Future = Npm.require('fibers/future');
Meteor.methods({
'build': function(buildRequest) {
var fut = new Future(), result;
log.info('method build get called: buildRequest=%s', JSON.stringify(buildRequest));
shell.exec('echo "simulate long Android build task."');
shell.exec('sleep ' + buildRequest.sec, function(code, output){
fut.return([code, output]);
});
result = fut.wait();
log.info(result[0], result[1])
var result = {'successful': true, 'output': "OK", 'fileName': 'test.apk', 'genFile': '/tmp/test.apk'}
log.info("method return result=%s", JSON.stringify(result));
return result;
}
});
关于javascript - Meteor 方法调用循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27838869/