chore(xo-server/plugin-metadata): use builtin (un)serialize
This commit is contained in:
parent
f1fa811e5c
commit
93d1c6c3fc
@ -1,18 +1,26 @@
|
|||||||
import Collection from '../collection/redis.mjs'
|
import Collection from '../collection/redis.mjs'
|
||||||
import { createLogger } from '@xen-orchestra/log'
|
import { createLogger } from '@xen-orchestra/log'
|
||||||
import { forEach } from '../utils.mjs'
|
|
||||||
|
|
||||||
const log = createLogger('xo:plugin-metadata')
|
const log = createLogger('xo:plugin-metadata')
|
||||||
|
|
||||||
// ===================================================================
|
// ===================================================================
|
||||||
|
|
||||||
export class PluginsMetadata extends Collection {
|
export class PluginsMetadata extends Collection {
|
||||||
async save({ id, autoload, configuration }) {
|
_serialize(metadata) {
|
||||||
return /* await */ this.update({
|
const { autoload, configuration } = metadata
|
||||||
id,
|
metadata.autoload = JSON.stringify(autoload)
|
||||||
autoload: autoload ? 'true' : 'false',
|
metadata.configuration = JSON.stringify(configuration)
|
||||||
configuration: configuration && JSON.stringify(configuration),
|
}
|
||||||
})
|
|
||||||
|
_unserialize(metadata) {
|
||||||
|
const { autoload, configuration } = metadata
|
||||||
|
metadata.autoload = autoload === 'true'
|
||||||
|
try {
|
||||||
|
metadata.configuration = configuration && JSON.parse(configuration)
|
||||||
|
} catch (error) {
|
||||||
|
log.warn(`cannot parse pluginMetadata.configuration: ${configuration}`)
|
||||||
|
metadata.configuration = []
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async merge(id, data) {
|
async merge(id, data) {
|
||||||
@ -21,27 +29,9 @@ export class PluginsMetadata extends Collection {
|
|||||||
throw new Error('no such plugin metadata')
|
throw new Error('no such plugin metadata')
|
||||||
}
|
}
|
||||||
|
|
||||||
return /* await */ this.save({
|
return /* await */ this.update({
|
||||||
...pluginMetadata,
|
...pluginMetadata,
|
||||||
...data,
|
...data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(properties) {
|
|
||||||
const pluginsMetadata = await super.get(properties)
|
|
||||||
|
|
||||||
// Deserializes.
|
|
||||||
forEach(pluginsMetadata, pluginMetadata => {
|
|
||||||
const { autoload, configuration } = pluginMetadata
|
|
||||||
pluginMetadata.autoload = autoload === 'true'
|
|
||||||
try {
|
|
||||||
pluginMetadata.configuration = configuration && JSON.parse(configuration)
|
|
||||||
} catch (error) {
|
|
||||||
log.warn(`cannot parse pluginMetadata.configuration: ${configuration}`)
|
|
||||||
pluginMetadata.configuration = []
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return pluginsMetadata
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ export default class {
|
|||||||
plugins =>
|
plugins =>
|
||||||
Promise.all(
|
Promise.all(
|
||||||
plugins.map(async plugin => {
|
plugins.map(async plugin => {
|
||||||
await this._pluginsMetadata.save(plugin)
|
await this._pluginsMetadata.update(plugin)
|
||||||
if (plugin.configuration !== undefined && this._plugins[plugin.id] !== undefined) {
|
if (plugin.configuration !== undefined && this._plugins[plugin.id] !== undefined) {
|
||||||
await this.configurePlugin(plugin.id, plugin.configuration)
|
await this.configurePlugin(plugin.id, plugin.configuration)
|
||||||
}
|
}
|
||||||
@ -88,7 +88,7 @@ export default class {
|
|||||||
;({ autoload, configuration } = metadata)
|
;({ autoload, configuration } = metadata)
|
||||||
} else {
|
} else {
|
||||||
log.info(`[NOTICE] register plugin ${name} for the first time`)
|
log.info(`[NOTICE] register plugin ${name} for the first time`)
|
||||||
await this._pluginsMetadata.save({
|
await this._pluginsMetadata.update({
|
||||||
id,
|
id,
|
||||||
autoload,
|
autoload,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user