Jam*_*ner 10 javascript http request node.js
我们有一个 Node.js 脚本,每分钟运行一次来检查应用程序的状态。通常情况下,它工作得很好。如果服务启动,则以 0 退出。如果服务关闭,则以 1 退出。一切正常。
但每隔一段时间,它就会停下来。控制台报告“正在调用状态 API...”并无限期地停止在那里。它甚至不会在 Node 内置的两分钟超时时超时。没有错误,什么都没有。它只是坐在那里,等待,永远。这是一个问题,因为它会阻止以下状态检查作业的运行。
此时,我的整个团队都已经研究过它,但我们都无法弄清楚什么情况会导致它挂起。我们建立了从开始到结束的超时,以便我们可以继续下一个工作,但这本质上会跳过状态检查并产生盲点。所以,我向你们好人提出这个问题。
这是脚本(删除了名称/网址):
#!/usr/bin/env node
// SETTINGS: -------------------------------------------------------------------------------------------------
/** URL to contact for status information. */
const STATUS_API = process.env.STATUS_API;
/** Number of attempts to make before reporting as a failure. */
const ATTEMPT_LIMIT = 3;
/** Amount of time to wait before starting another attempt, in milliseconds. */
const ATTEMPT_DELAY = 5000;
// RUNTIME: --------------------------------------------------------------------------------------------------
const URL = require('url');
const https = require('https');
// Make the first attempt.
make_attempt(1, STATUS_API);
// FUNCTIONS: ------------------------------------------------------------------------------------------------
function make_attempt(attempt_number, url) {
console.log('\n\nCONNECTION ATTEMPT:', attempt_number);
check_status(url, function (success) {
console.log('\nAttempt', success ? 'PASSED' : 'FAILED');
// If this attempt succeeded, report success.
if (success) {
console.log('\nSTATUS CHECK PASSED after', attempt_number, 'attempt(s).');
process.exit(0);
}
// Otherwise, if we have additional attempts, try again.
else if (attempt_number < ATTEMPT_LIMIT) {
setTimeout(make_attempt.bind(null, attempt_number + 1, url), ATTEMPT_DELAY);
}
// Otherwise, we're out of attempts. Report failure.
else {
console.log("\nSTATUS CHECK FAILED");
process.exit(1);
}
})
}
function check_status(url, callback) {
var handle_error = function (error) {
console.log("\tFailed.\n");
console.log('\t' + error.toString().replace(/\n\r?/g, '\n\t'));
callback(false);
};
console.log("\tCalling status API...");
try {
var options = URL.parse(url);
options.timeout = 20000;
https.get(options, function (response) {
var body = '';
response.setEncoding('utf8');
response.on('data', function (data) {body += data;});
response.on('end', function () {
console.log("\tConnected.\n");
try {
var parsed = JSON.parse(body);
if ((!parsed.started || !parsed.uptime)) {
console.log('\tReceived unexpected JSON response:');
console.log('\t\t' + JSON.stringify(parsed, null, 1).replace(/\n\r?/g, '\n\t\t'));
callback(false);
}
else {
console.log('\tReceived status details from API:');
console.log('\t\tServer started:', parsed.started);
console.log('\t\tServer uptime:', parsed.uptime);
callback(true);
}
}
catch (error) {
console.log('\tReceived unexpected non-JSON response:');
console.log('\t\t' + body.trim().replace(/\n\r?/g, '\n\t\t'));
callback(false);
}
});
}).on('error', handle_error);
}
catch (error) {
handle_error(error);
}
}
Run Code Online (Sandbox Code Playgroud)
如果你们中的任何人都可以看到任何可能在没有输出或超时的情况下挂起的地方,那将非常有帮助!
谢谢你,詹姆斯·坦纳
编辑:https ps 我们直接使用,而不是request这样我们在脚本运行时不需要进行任何安装。这是因为该脚本可以在分配给 Jenkins 的任何构建计算机上运行,无需自定义安装。
你不缺的是吗.end()?
http.request(options, callback).end()
Run Code Online (Sandbox Code Playgroud)
就像这里解释的那样。
在您的响应回调中,您没有检查状态..
用于.on('error', handle_error);连接到服务器时发生的错误,状态代码错误是服务器在成功连接后响应的错误。
通常,您期望从成功的请求中得到 200 状态响应。
所以你的 http.get 的一个小模块来处理这个应该可以。
例如。
https.get(options, function (response) {
if (response.statusCode != 200) {
console.log('\tHTTP statusCode not 200:');
callback(false);
return; //no point going any further
}
....
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9132 次 |
| 最近记录: |