Skip to content

Commit

Permalink
feat(xo-server-web-books): waiting response from server (#5420)
Browse files Browse the repository at this point in the history
Fixes #4948
  • Loading branch information
MathieuRA committed Jan 8, 2021
1 parent 9240211 commit 26614b5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.unreleased.md
Expand Up @@ -7,6 +7,8 @@

> Users must be able to say: “Nice enhancement, I'm eager to test it”
- [Web hooks] Possibility to wait a response from the server before continuing [#4948](https://github.com/vatesfr/xen-orchestra/issues/4948) (PR [#5420](https://github.com/vatesfr/xen-orchestra/pull/5420))

### Bug fixes

> Users must be able to say: “I had this issue, happy to know it's fixed”
Expand All @@ -31,4 +33,5 @@
>
> In case of conflict, the highest (lowest in previous list) `$version` wins.
- xo-server patch
- xo-server minor
- xo-server-web-hooks minor
17 changes: 13 additions & 4 deletions packages/xo-server-web-hooks/src/index.js
Expand Up @@ -6,14 +6,17 @@ function handleHook(type, data) {
const hooks = this._hooks[data.method]?.[type]
if (hooks !== undefined) {
return Promise.all(
hooks.map(({ url }) =>
this._makeRequest(url, type, data).catch(error => {
hooks.map(({ url, waitForResponse = false }) => {
const promise = this._makeRequest(url, type, data).catch(error => {
log.error('web hook failed', {
error,
webHook: { ...data, url, type },
})
})
)
if (waitForResponse && type === 'pre') {
return promise
}
})
)
}
}
Expand Down Expand Up @@ -48,7 +51,8 @@ class XoServerHooks {
// {
// method: 'vm.start',
// type: 'pre',
// url: 'https://my-domain.net/xo-hooks?action=vm.start'
// url: 'https://my-domain.net/xo-hooks?action=vm.start',
// waitForResponse: false
// },
// ...
// ],
Expand Down Expand Up @@ -139,6 +143,11 @@ export const configurationSchema = ({ xo: { apiMethods } }) => ({
title: 'URL',
type: 'string',
},
waitForResponse: {
description: 'Waiting for the server response before executing the call. Only available on "PRE" type',
title: 'Wait for response',
type: 'boolean',
},
},
required: ['method', 'type', 'url'],
},
Expand Down
13 changes: 12 additions & 1 deletion packages/xo-server/src/xo-mixins/api.js
@@ -1,4 +1,6 @@
import createLogger from '@xen-orchestra/log'
import emitAsync from '@xen-orchestra/emit-async'

import kindOf from 'kindof'
import ms from 'ms'
import schemaInspector from 'schema-inspector'
Expand Down Expand Up @@ -261,7 +263,16 @@ export default class Api {
timestamp: Date.now(),
}

xo.emit('xo:preCall', data)
await emitAsync.call(
xo,
{
onError(error) {
log.warn('xo:preCall listener failure', { error })
},
},
'xo:preCall',
data
)

try {
await checkPermission.call(context, method)
Expand Down
19 changes: 17 additions & 2 deletions packages/xo-server/src/xo-mixins/jobs/index.js
Expand Up @@ -3,6 +3,9 @@
import type { Pattern } from 'value-matcher'

import asyncMap from '@xen-orchestra/async-map'
import createLogger from '@xen-orchestra/log'
import emitAsync from '@xen-orchestra/emit-async'

import { CancelToken, ignoreErrors } from 'promise-toolbox'
import { map as mapToArray } from 'lodash'
import { noSuchObject } from 'xo-common/api-errors'
Expand All @@ -18,6 +21,8 @@ import executeCall from './execute-call'

// ===================================================================

const log = createLogger('xo:jobs')

export type Job = {
id: string,
name: string,
Expand Down Expand Up @@ -305,8 +310,18 @@ export default class Jobs {

session = app.createUserConnection()
session.set('user_id', job.userId)

type === 'backup' && app.emit('backup:preCall', data)
if (type === 'backup') {
await emitAsync.call(
app,
{
onError(error) {
log.warn('backup:preCall listener failure', { error })
},
},
'backup:preCall',
data
)
}

const status = await executor({
app,
Expand Down

0 comments on commit 26614b5

Please sign in to comment.