我创建了以下脚本来从 Jelastic api 中提取统计信息,这样我就可以在一段时间内收集资源统计信息。最终目标是将数据记录到电子表格中。
下面是我处理身份验证然后向GetSumStats
.
如果我运行代码,有时返回的结果是预期的。
{
iops_used: 0,
duration: 3600,
cpumhz: 7,
start: '',
disk: 7857,
mem: 725212,
cpu: 24002,
capacity: 9,
net: { in_int: 96004, out_int: 96004, in_ext: 9181, out_ext: 9395 },
chanksused: 7,
nodeid: 'sum'
}
但其他时候,请求会因错误而失败。
{ result: 702,
source: 'JEL',
error: 'not authenticated (different session key)',
stats: [] }
这是时间问题还是已知问题?也许脚本太快了,API 还不知道会话 ID?这就是我介绍 setTimeout 的原因
var sites = require('./sites.json').sites,
credentials = require('./credentials.json'),
Client = require('node-rest-client').Client,
util = require('./util.js');
(function () {
"use strict";
var client = new Client();
var session;
login();
function login() {
var args = {
parameters: {
appid: sites[2].appId,
login: credentials.email,
password: credentials.password
}
};
client.registerMethod("login", "https://app.j.hostapi.co.uk/1.0/users/authentication/rest/signin", "GET");
client.methods.login(args, function (data, response) {
// parsed response body as js object
data = util.parseResponse(data);
session = data.session;
console.log(session);
// Tried to pause here in case it was too quick
setTimeout(function() {
getSumStats();
}, 3000);
});
}
function logout() {
var args = {
parameters: {
appid: sites[2].appId,
session: session
}
};
client.registerMethod("logout", "https://app.j.hostapi.co.uk/1.0/users/authentication/rest/signout", "GET");
client.methods.logout(args, function (data, response) {
// parsed response body as js object
data = util.parseResponse(data);
console.log(data);
// raw response
//console.log(response);
});
}
// Failure here
function getSumStats() {
var args = {
parameters: {
domain: sites[2].domain,
session: session,
duration: 3600
}
};
client.registerMethod("getSumStats", "https://app.j.hostapi.co.uk/1.0/environment/control/rest/getsumstat", "GET");
client.methods.getSumStats(args, function (data, response) {
// parsed response body as js object
data = util.parseResponse(data);
console.log(data.stats);
logout();
});
}
})();