feat(Xapi#_watchEvents): keep-alive, inject 1 event per minute (#45)

See vatesfr/xo-web#1384
This commit is contained in:
Julien Fontanet 2016-08-18 09:37:31 +02:00 committed by GitHub
parent 556ca71c3a
commit f82a6efda1

View File

@ -13,8 +13,7 @@ import { EventEmitter } from 'events'
import { import {
catchPlus as pCatch, catchPlus as pCatch,
delay as pDelay, delay as pDelay,
promisify, promisify
timeout as pTimeout
} from 'promise-toolbox' } from 'promise-toolbox'
import { import {
createClient as createXmlRpcClient, createClient as createXmlRpcClient,
@ -401,21 +400,23 @@ export class Xapi extends EventEmitter {
// Medium level call: handle session errors. // Medium level call: handle session errors.
_sessionCall (method, args) { _sessionCall (method, args) {
if (startsWith(method, 'session.')) { try {
return Promise.reject( if (startsWith(method, 'session.')) {
new Error('session.*() methods are disabled from this interface') throw new Error('session.*() methods are disabled from this interface')
) }
return this._transportCall(method, [this.sessionId].concat(args))
::pCatch(isSessionInvalid, () => {
// XAPI is sometimes reinitialized and sessions are lost.
// Try to login again.
debug('%s: the session has been reinitialized', this._humanId)
this._sessionId = null
return this.connect().then(() => this._sessionCall(method, args))
})
} catch (error) {
return Promise.reject(error)
} }
return this._transportCall(method, [this.sessionId].concat(args))
::pCatch(isSessionInvalid, () => {
// XAPI is sometimes reinitialized and sessions are lost.
// Try to login again.
debug('%s: the session has been reinitialized', this._humanId)
this._sessionId = null
return this.connect().then(() => this._sessionCall(method, args))
})
} }
// Low level call: handle transport errors. // Low level call: handle transport errors.
@ -611,13 +612,25 @@ export class Xapi extends EventEmitter {
} }
_watchEvents () { _watchEvents () {
const call = () => this._sessionCall('event.from', [ const injectEvent = () => {
['*'], const { pool } = this
this._fromToken, if (pool) {
1e3 + 0.1 // Force float. this._sessionCall('event.inject', [ 'pool', pool.$ref ]).catch(console.error)
])::pTimeout(600000, call) }
}
const loop = () => call().then(onSuccess, onFailure) let timeout
const loop = () => {
// Keep alive: force at least one event per minute.
clearTimeout(timeout)
timeout = setTimeout(injectEvent, 60e3)
return this._sessionCall('event.from', [
['*'],
this._fromToken,
1e3 + 0.1 // Force float.
]).then(onSuccess, onFailure)
}
const onSuccess = ({token, events}) => { const onSuccess = ({token, events}) => {
this._fromToken = token this._fromToken = token