Skip to content

Commit

Permalink
feat(xo-server/api): add a schedule.runSequence method (#7985)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeauchamp authored Sep 27, 2024
1 parent 558ea84 commit f7df1c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/xo-server/src/api/schedule.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// FIXME so far, no acls for schedules

import { Task } from '@xen-orchestra/mixins/Tasks.mjs'

export async function getAll() {
return /* await */ this.getAllSchedules()
}
Expand Down Expand Up @@ -64,3 +66,25 @@ delete_.params = {
}

export { delete_ as delete }

export async function runSequence({ schedules }) {
const t = this.tasks.create({ type: 'xo:schedule:sequence', name: 'Schedule sequence' })
await t.run(async () => {
const nb = schedules.length
const signal = Task.abortSignal
for (let i = 0; i < nb; i++) {
signal.throwIfAborted()
Task.set('progress', Math.round((i * 100) / nb))
const idSchedule = schedules[i]
// we can't auto resolve array parameters, we have to resolve them by hand
const schedule = await this.getSchedule(idSchedule)
const job = await this.getJob(schedule.jobId)
await this.runJob(job, schedule)
}
})
}
runSequence.permission = 'admin'
runSequence.description = 'Run a sequence of schedules, one after the other'
runSequence.params = {
schedules: { type: 'array', items: { type: 'string' } },
}
4 changes: 2 additions & 2 deletions packages/xo-server/src/xo-mixins/jobs/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default class Jobs {
}

@decorateWith(defer)
async _runJob($defer, job, schedule, data_) {
async runJob($defer, job, schedule, data_) {
const logger = this._logger
const { id, type } = job

Expand Down Expand Up @@ -316,7 +316,7 @@ export default class Jobs {
const jobs = await Promise.all(idSequence.map(id => this.getJob(id)))

for (const job of jobs) {
await this._runJob(job, schedule, data)
await this.runJob(job, schedule, data)
}
}
}

0 comments on commit f7df1c4

Please sign in to comment.