chore(xo-server): delete unused schedules on clean

This commit is contained in:
Julien Fontanet 2018-03-05 17:22:40 +01:00
parent 481bc9430a
commit b7d3762c06
3 changed files with 17 additions and 4 deletions

View File

@ -1,7 +1,7 @@
// FIXME so far, no acls for jobs
export async function getAll () {
return /* await */ this.getAllJobs()
return /* await */ this.getAllJobs('call')
}
getAll.permission = 'admin'

View File

@ -141,13 +141,13 @@ export default class Jobs {
})
}
async getAllJobs (type: string = 'call'): Promise<Array<Job>> {
async getAllJobs (type: ?string): Promise<Array<Job>> {
// $FlowFixMe don't know what is the problem (JFT)
const jobs = await this._jobs.get()
const runningJobs = this._runningJobs
const result = []
jobs.forEach(job => {
if (job.type === type) {
if (type === undefined || job.type === type) {
job.runId = runningJobs[job.id]
result.push(job)
}

View File

@ -1,6 +1,8 @@
// @flow
import { createSchedule } from '@xen-orchestra/cron'
// $FlowFixMe
import { keyBy } from 'lodash'
import { noSuchObject } from 'xo-common/api-errors'
import Collection from '../collection/redis'
@ -58,7 +60,18 @@ export default class Scheduling {
this._runs = { __proto__: null }
app.on('clean', () => db.rebuildIndexes())
app.on('clean', async () => {
const [jobsById, schedules] = await Promise.all([
app.getAllJobs().then(_ => keyBy(_, 'id')),
app.getAllSchedules(),
])
await db.remove(
schedules.filter(_ => !(_.jobId in jobsById)).map(_ => _.id)
)
return db.rebuildIndexes()
})
app.on('start', async () => {
app.addConfigManager(
'schedules',