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,10 +400,9 @@ export class Xapi extends EventEmitter {
// Medium level call: handle session errors. // Medium level call: handle session errors.
_sessionCall (method, args) { _sessionCall (method, args) {
try {
if (startsWith(method, 'session.')) { if (startsWith(method, 'session.')) {
return Promise.reject( throw new Error('session.*() methods are disabled from this interface')
new Error('session.*() methods are disabled from this interface')
)
} }
return this._transportCall(method, [this.sessionId].concat(args)) return this._transportCall(method, [this.sessionId].concat(args))
@ -416,6 +414,9 @@ export class Xapi extends EventEmitter {
this._sessionId = null this._sessionId = null
return this.connect().then(() => this._sessionCall(method, args)) return this.connect().then(() => this._sessionCall(method, args))
}) })
} catch (error) {
return Promise.reject(error)
}
} }
// 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
if (pool) {
this._sessionCall('event.inject', [ 'pool', pool.$ref ]).catch(console.error)
}
}
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, this._fromToken,
1e3 + 0.1 // Force float. 1e3 + 0.1 // Force float.
])::pTimeout(600000, call) ]).then(onSuccess, onFailure)
}
const loop = () => call().then(onSuccess, onFailure)
const onSuccess = ({token, events}) => { const onSuccess = ({token, events}) => {
this._fromToken = token this._fromToken = token