Various updates.

This commit is contained in:
Julien Fontanet 2013-10-03 17:53:07 +02:00
parent e703824198
commit 9a813adf0e

View File

@ -19,10 +19,12 @@ require('util').inherits(Xapi, require('events').EventEmitter);
Xapi.prototype.call = function (method) {
var args = arguments;
var params = Array.prototype.slice.call(arguments, 1);
var tries = 0;
var self = this;
return function helper() {
return Q(self.sessionId).then(function (session_id) {
var params = Array.prototype.slice.call(args, 1);
if (session_id)
{
params.unshift(session_id);
@ -42,14 +44,29 @@ Xapi.prototype.call = function (method) {
return value.Value;
}).fail(function (error) {
if ('HOST_IS_SLAVE' !== error[0])
// XAPI sommetimes close the connection when the server is
// no longer pool master (`event.next`), so we have to
// retry at least once to know who is the new pool master.
if ((0 === tries) && ('ECONNRESET' === error.code))
{
throw error;
// @todo Does not work because it seems to reuse the
// broken socket.
++tries;
return helper();
}
if ('HOST_IS_SLAVE' === error[0])
{
tries = 0;
self.changeHost(error[1]);
return self.call.apply(self, args);
return helper();
}
throw error;
});
}();
};
Xapi.prototype.changeHost = function (host) {