Many fixes. Configure must operate if cron job is running.
This commit is contained in:
parent
b0bf18e235
commit
d9bf7c7d12
@ -1,3 +1,6 @@
|
|||||||
|
import EventEmitter from 'events'
|
||||||
|
|
||||||
|
import eventToPromise from 'event-to-promise'
|
||||||
import filter from 'lodash.filter'
|
import filter from 'lodash.filter'
|
||||||
import includes from 'lodash.includes'
|
import includes from 'lodash.includes'
|
||||||
import intersection from 'lodash.intersection'
|
import intersection from 'lodash.intersection'
|
||||||
@ -6,6 +9,8 @@ import uniq from 'lodash.uniq'
|
|||||||
import { CronJob } from 'cron'
|
import { CronJob } from 'cron'
|
||||||
import { default as mapToArray } from 'lodash.map'
|
import { default as mapToArray } from 'lodash.map'
|
||||||
|
|
||||||
|
class Emitter extends EventEmitter {}
|
||||||
|
|
||||||
// ===================================================================
|
// ===================================================================
|
||||||
|
|
||||||
const noop = () => {}
|
const noop = () => {}
|
||||||
@ -111,24 +116,30 @@ export const configurationSchema = {
|
|||||||
// ===================================================================
|
// ===================================================================
|
||||||
|
|
||||||
const makeCronJob = (cronPattern, fn) => {
|
const makeCronJob = (cronPattern, fn) => {
|
||||||
let running
|
const job = {
|
||||||
|
running: false,
|
||||||
|
emitter: new Emitter()
|
||||||
|
}
|
||||||
|
|
||||||
const job = new CronJob(cronPattern, async () => {
|
job.cron = new CronJob(cronPattern, async () => {
|
||||||
if (running) {
|
if (job.running) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
running = true
|
job.running = true
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fn()
|
await fn()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[WARN] scheduled function:', error && error.stack || error)
|
console.error('[WARN] scheduled function:', error && error.stack || error)
|
||||||
} finally {
|
} finally {
|
||||||
running = false
|
job.running = false
|
||||||
|
job.emitter.emit('finish')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
job.isEnabled = () => job.cron.running
|
||||||
|
|
||||||
return job
|
return job
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,6 +253,7 @@ class Plan {
|
|||||||
|
|
||||||
// No ressource's utilization problem.
|
// No ressource's utilization problem.
|
||||||
if (exceededHosts.length === 0) {
|
if (exceededHosts.length === 0) {
|
||||||
|
debug('No ressource\'s utilization problem.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,6 +265,7 @@ class Plan {
|
|||||||
|
|
||||||
// No ressource's utilization problem.
|
// No ressource's utilization problem.
|
||||||
if (exceededHosts.length === 0) {
|
if (exceededHosts.length === 0) {
|
||||||
|
debug('No ressource\'s utilization problem.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -322,9 +335,9 @@ class Plan {
|
|||||||
|
|
||||||
debug(`Migrate VM (${vm.id}) to Host (${destination.id}) from Host (${exceededHost.id})`)
|
debug(`Migrate VM (${vm.id}) to Host (${destination.id}) from Host (${exceededHost.id})`)
|
||||||
|
|
||||||
promises.push(
|
// promises.push(
|
||||||
xapiSrc.migrateVm(vm._xapiId, this.xo.getXapi(destination), destination._xapiId)
|
// xapiSrc.migrateVm(vm._xapiId, this.xo.getXapi(destination), destination._xapiId)
|
||||||
)
|
// )
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@ -384,18 +397,21 @@ class LoadBalancerPlugin {
|
|||||||
constructor (xo) {
|
constructor (xo) {
|
||||||
this.xo = xo
|
this.xo = xo
|
||||||
this._cronJob = makeCronJob(`*/${EXECUTION_DELAY} * * * *`, ::this._executePlans)
|
this._cronJob = makeCronJob(`*/${EXECUTION_DELAY} * * * *`, ::this._executePlans)
|
||||||
|
this._emitter
|
||||||
}
|
}
|
||||||
|
|
||||||
async configure ({ plans }) {
|
async configure ({ plans }) {
|
||||||
const cronJob = this._cronJob
|
const cronJob = this._cronJob
|
||||||
const enabled = cronJob.running
|
const enabled = cronJob.isEnabled()
|
||||||
|
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
cronJob.stop()
|
cronJob.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait until all old plans stopped running.
|
// Wait until all old plans stopped running.
|
||||||
await this._plansPromise
|
if (cronJob.running) {
|
||||||
|
await eventToPromise(cronJob.emitter, 'finish')
|
||||||
|
}
|
||||||
|
|
||||||
this._plans = []
|
this._plans = []
|
||||||
this._poolIds = [] // Used pools.
|
this._poolIds = [] // Used pools.
|
||||||
@ -454,9 +470,9 @@ class LoadBalancerPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_executePlans () {
|
_executePlans () {
|
||||||
return (this._plansPromise = Promise.all(
|
return Promise.all(
|
||||||
mapToArray(this._plans, plan => plan.execute())
|
mapToArray(this._plans, plan => plan.execute())
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user