chore(xo-server): remove pSettle (#5682)

This commit is contained in:
Julien Fontanet
2021-03-22 10:37:07 +01:00
committed by GitHub
parent 65428d629c
commit 4541f7c758
4 changed files with 24 additions and 90 deletions

View File

@@ -12,7 +12,7 @@ import { createLogger } from '@xen-orchestra/log'
import { randomBytes } from 'crypto'
import { resolve } from 'path'
import { utcFormat, utcParse } from 'd3-time-format'
import { fromCallback, pAll, pReflect, promisify } from 'promise-toolbox'
import { fromCallback, promisify } from 'promise-toolbox'
import { type SimpleIdPattern } from './utils'
@@ -159,20 +159,6 @@ export const noop = () => {}
// -------------------------------------------------------------------
// Given a collection (array or object) which contains promises,
// return a promise that is fulfilled when all the items in the
// collection are either fulfilled or rejected.
//
// This promise will be fulfilled with a collection (of the same type,
// array or object) containing promise inspections.
//
// Usage: pSettle(promises) or promises::pSettle()
export function pSettle(promises) {
return (this || promises)::pAll(p => Promise.resolve(p)::pReflect())
}
// -------------------------------------------------------------------
export { pAll, pDelay, pFinally, pFromCallback, pReflect, promisify, promisifyAll } from 'promise-toolbox'
// -------------------------------------------------------------------

View File

@@ -1,6 +1,6 @@
/* eslint-env jest */
import { camelToSnakeCase, diffItems, extractProperty, generateToken, parseSize, parseXml, pSettle } from './utils'
import { camelToSnakeCase, diffItems, extractProperty, generateToken, parseSize, parseXml } from './utils'
// ===================================================================
@@ -175,54 +175,3 @@ describe('parseSize()', function () {
expect(parseSize('3MB')).toBe(3e6)
})
})
// -------------------------------------------------------------------
describe('pSettle()', () => {
it('works with arrays', async () => {
const rejection = 'fatality'
const [status1, status2, status3] = await pSettle([Promise.resolve(42), Math.PI, Promise.reject(rejection)])
expect(status1.isRejected()).toBe(false)
expect(status2.isRejected()).toBe(false)
expect(status3.isRejected()).toBe(true)
expect(status1.isFulfilled()).toBe(true)
expect(status2.isFulfilled()).toBe(true)
expect(status3.isFulfilled()).toBe(false)
expect(status1.value()).toBe(42)
expect(status2.value()).toBe(Math.PI)
expect(::status3.value).toThrow()
expect(::status1.reason).toThrow()
expect(::status2.reason).toThrow()
expect(status3.reason()).toBe(rejection)
})
it('works with objects', async () => {
const rejection = 'fatality'
const { a: status1, b: status2, c: status3 } = await pSettle({
a: Promise.resolve(42),
b: Math.PI,
c: Promise.reject(rejection),
})
expect(status1.isRejected()).toBe(false)
expect(status2.isRejected()).toBe(false)
expect(status3.isRejected()).toBe(true)
expect(status1.isFulfilled()).toBe(true)
expect(status2.isFulfilled()).toBe(true)
expect(status3.isFulfilled()).toBe(false)
expect(status1.value()).toBe(42)
expect(status2.value()).toBe(Math.PI)
expect(::status3.value).toThrow()
expect(::status1.reason).toThrow()
expect(::status2.reason).toThrow()
expect(status3.reason()).toBe(rejection)
})
})

View File

@@ -10,6 +10,7 @@ import mixin from '@xen-orchestra/mixin'
import ms from 'ms'
import synchronized from 'decorator-synchronized'
import tarStream from 'tar-stream'
import { asyncMap } from '@xen-orchestra/async-map'
import { vmdkToVhd } from 'xo-vmdk-to-vhd'
import { cancelable, defer, fromEvents, ignoreErrors, pCatch, pRetry } from 'promise-toolbox'
import { parseDuration } from '@vates/parse-duration'
@@ -23,7 +24,7 @@ import { satisfies as versionSatisfies } from 'semver'
import createSizeStream from '../size-stream'
import ensureArray from '../_ensureArray'
import fatfsBuffer, { init as fatfsBufferInit } from '../fatfs-buffer'
import { camelToSnakeCase, forEach, map, pAll, parseSize, pDelay, pFinally, promisifyAll, pSettle } from '../utils'
import { camelToSnakeCase, forEach, map, pAll, parseSize, pDelay, pFinally, promisifyAll } from '../utils'
import mixins from './mixins'
import OTHER_CONFIG_TEMPLATE from './other-config-template'
@@ -250,14 +251,11 @@ export default class Xapi extends XapiBase {
const host = this.getObject(hostId)
const vms = host.$resident_VMs
log.debug(`Emergency shutdown: ${host.name_label}`)
await pSettle(
// eslint-disable-next-line array-callback-return
vms.map(vm => {
if (!vm.is_control_domain) {
return this.callAsync('VM.suspend', vm.$ref)
}
})
)
await asyncMap(vms, vm => {
if (!vm.is_control_domain) {
return ignoreErrors.call(this.callAsync('VM.suspend', vm.$ref))
}
})
await this.call('host.disable', host.$ref)
await this.callAsync('host.shutdown', host.$ref)
}

View File

@@ -3,7 +3,7 @@ import createLogger from '@xen-orchestra/log'
import deferrable from 'golike-defer'
import execa from 'execa'
import splitLines from 'split-lines'
import { CancelToken, fromEvent, ignoreErrors } from 'promise-toolbox'
import { CancelToken, fromEvent, ignoreErrors, pReflect } from 'promise-toolbox'
import { createParser as createPairsParser } from 'parse-pairs'
import { createReadStream, readdir, stat } from 'fs'
import { decorateWith } from '@vates/decorate-with'
@@ -23,7 +23,6 @@ import {
mapFilter,
pFinally,
pFromCallback,
pSettle,
resolveSubpath,
safeDateFormat,
safeDateParse,
@@ -627,18 +626,20 @@ export default class {
$defer.onFailure(cancel)
// Save vdis.
const vdiBackups = await pSettle(
mapToArray(delta.vdis, async (vdi, key) => {
const vdiParent = xapi.getObject(vdi.snapshot_of)
const vdiBackups = await Promise.all(
Object.keys(delta.vdis)
.map(async ([key, vdi]) => {
const vdiParent = xapi.getObject(vdi.snapshot_of)
const data = await this._saveDeltaVdiBackup(xapi, {
vdiParent,
isFull: !baseVm || find(fullVdisRequired, id => vdiParent.$id === id),
handler,
stream: delta.streams[`${key}.vhd`],
dir,
retention,
})
return this._saveDeltaVdiBackup(xapi, {
vdiParent,
isFull: !baseVm || find(fullVdisRequired, id => vdiParent.$id === id),
handler,
stream: delta.streams[`${key}.vhd`],
dir,
retention,
}).then(data => {
delta.vdis[key] = {
...delta.vdis[key],
xoPath: data.path,
@@ -646,7 +647,7 @@ export default class {
return data
})
})
.map(promise => pReflect.call(promise))
)
const fulFilledVdiBackups = []