Compare commits
2 Commits
registerAp
...
redis-json
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
941d17d669 | ||
|
|
fd2b93d38b |
@@ -34,6 +34,16 @@ import Collection, { ModelAlreadyExists } from '../collection'
|
|||||||
|
|
||||||
const VERSION = '20170905'
|
const VERSION = '20170905'
|
||||||
|
|
||||||
|
const serialize = JSON.stringify
|
||||||
|
const unserialize = JSON.parse
|
||||||
|
const tryUnserialize = value => {
|
||||||
|
try {
|
||||||
|
return unserialize(value)
|
||||||
|
} catch (_) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default class Redis extends Collection {
|
export default class Redis extends Collection {
|
||||||
constructor({ connection, indexes = [], prefix, uri }) {
|
constructor({ connection, indexes = [], prefix, uri }) {
|
||||||
super()
|
super()
|
||||||
@@ -108,6 +118,10 @@ export default class Redis extends Collection {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object.keys(model).forEach(key => {
|
||||||
|
model[key] = tryUnserialize(model[key])
|
||||||
|
})
|
||||||
|
|
||||||
// Mix the identifier in.
|
// Mix the identifier in.
|
||||||
model.id = id
|
model.id = id
|
||||||
|
|
||||||
@@ -161,7 +175,7 @@ export default class Redis extends Collection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
params.push(name, value)
|
params.push(name, serialize(value))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export class Groups extends Collection {
|
|||||||
async save(group) {
|
async save(group) {
|
||||||
// Serializes.
|
// Serializes.
|
||||||
let tmp
|
let tmp
|
||||||
group.users = isEmpty((tmp = group.users)) ? undefined : JSON.stringify(tmp)
|
group.users = isEmpty((tmp = group.users)) ? undefined : tmp
|
||||||
|
|
||||||
return /* await */ this.update(group)
|
return /* await */ this.update(group)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
import Collection from '../collection/redis'
|
import Collection from '../collection/redis'
|
||||||
import createLogger from '@xen-orchestra/log'
|
|
||||||
import Model from '../model'
|
import Model from '../model'
|
||||||
import { forEach } from '../utils'
|
|
||||||
|
|
||||||
const log = createLogger('xo:plugin-metadata')
|
|
||||||
|
|
||||||
// ===================================================================
|
// ===================================================================
|
||||||
|
|
||||||
@@ -16,14 +12,6 @@ export class PluginsMetadata extends Collection {
|
|||||||
return PluginMetadata
|
return PluginMetadata
|
||||||
}
|
}
|
||||||
|
|
||||||
async save({ id, autoload, configuration }) {
|
|
||||||
return /* await */ this.update({
|
|
||||||
id,
|
|
||||||
autoload: autoload ? 'true' : 'false',
|
|
||||||
configuration: configuration && JSON.stringify(configuration),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async merge(id, data) {
|
async merge(id, data) {
|
||||||
const pluginMetadata = await this.first(id)
|
const pluginMetadata = await this.first(id)
|
||||||
if (pluginMetadata === undefined) {
|
if (pluginMetadata === undefined) {
|
||||||
@@ -35,23 +23,4 @@ export class PluginsMetadata extends Collection {
|
|||||||
...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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import Collection from '../collection/redis'
|
import Collection from '../collection/redis'
|
||||||
import Model from '../model'
|
import Model from '../model'
|
||||||
import { forEach } from '../utils'
|
|
||||||
|
|
||||||
// ===================================================================
|
// ===================================================================
|
||||||
|
|
||||||
@@ -10,16 +9,4 @@ export class Remotes extends Collection {
|
|||||||
get Model() {
|
get Model() {
|
||||||
return Remote
|
return Remote
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(properties) {
|
|
||||||
const remotes = await super.get(properties)
|
|
||||||
forEach(remotes, remote => {
|
|
||||||
remote.benchmarks =
|
|
||||||
remote.benchmarks !== undefined
|
|
||||||
? JSON.parse(remote.benchmarks)
|
|
||||||
: undefined
|
|
||||||
remote.enabled = remote.enabled === 'true'
|
|
||||||
})
|
|
||||||
return remotes
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,49 +71,7 @@ export type Executor = ({|
|
|||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
const normalize = job => {
|
class JobsDb extends Collection {}
|
||||||
Object.keys(job).forEach(key => {
|
|
||||||
try {
|
|
||||||
const value = (job[key] = JSON.parse(job[key]))
|
|
||||||
|
|
||||||
// userId are always strings, even if the value is numeric, which might to
|
|
||||||
// them being parsed as numbers.
|
|
||||||
//
|
|
||||||
// The issue has been introduced by
|
|
||||||
// 48b2297bc151df582160be7c1bf1e8ee160320b8.
|
|
||||||
if (key === 'userId' && typeof value === 'number') {
|
|
||||||
job[key] = String(value)
|
|
||||||
}
|
|
||||||
} catch (_) {}
|
|
||||||
})
|
|
||||||
return job
|
|
||||||
}
|
|
||||||
|
|
||||||
const serialize = (job: {| [string]: any |}) => {
|
|
||||||
Object.keys(job).forEach(key => {
|
|
||||||
const value = job[key]
|
|
||||||
if (typeof value !== 'string') {
|
|
||||||
job[key] = JSON.stringify(job[key])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return job
|
|
||||||
}
|
|
||||||
|
|
||||||
class JobsDb extends Collection {
|
|
||||||
async create(job): Promise<Job> {
|
|
||||||
return normalize((await this.add(serialize((job: any)))).properties)
|
|
||||||
}
|
|
||||||
|
|
||||||
async save(job): Promise<void> {
|
|
||||||
await this.update(serialize((job: any)))
|
|
||||||
}
|
|
||||||
|
|
||||||
async get(properties): Promise<Array<Job>> {
|
|
||||||
const jobs = await super.get(properties)
|
|
||||||
jobs.forEach(normalize)
|
|
||||||
return jobs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
@@ -150,7 +108,7 @@ export default class Jobs {
|
|||||||
xo.addConfigManager(
|
xo.addConfigManager(
|
||||||
'jobs',
|
'jobs',
|
||||||
() => jobsDb.get(),
|
() => jobsDb.get(),
|
||||||
jobs => Promise.all(mapToArray(jobs, job => jobsDb.save(job))),
|
jobs => Promise.all(mapToArray(jobs, job => jobsDb.update(job))),
|
||||||
['users']
|
['users']
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -211,8 +169,8 @@ export default class Jobs {
|
|||||||
return job
|
return job
|
||||||
}
|
}
|
||||||
|
|
||||||
createJob(job: $Diff<Job, {| id: string |}>): Promise<Job> {
|
async createJob(job: $Diff<Job, {| id: string |}>): Promise<Job> {
|
||||||
return this._jobs.create(job)
|
return (await this._jobs.add(job)).properties
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateJob(job: $Shape<Job>, merge: boolean = true) {
|
async updateJob(job: $Shape<Job>, merge: boolean = true) {
|
||||||
@@ -221,7 +179,7 @@ export default class Jobs {
|
|||||||
job = await this.getJob(id)
|
job = await this.getJob(id)
|
||||||
patch(job, props)
|
patch(job, props)
|
||||||
}
|
}
|
||||||
return /* await */ this._jobs.save(job)
|
return /* await */ this._jobs.update(job)
|
||||||
}
|
}
|
||||||
|
|
||||||
registerJobExecutor(type: string, executor: Executor): void {
|
registerJobExecutor(type: string, executor: Executor): void {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export default class {
|
|||||||
() => this._pluginsMetadata.get(),
|
() => this._pluginsMetadata.get(),
|
||||||
plugins =>
|
plugins =>
|
||||||
Promise.all(
|
Promise.all(
|
||||||
mapToArray(plugins, plugin => this._pluginsMetadata.save(plugin))
|
mapToArray(plugins, plugin => this._pluginsMetadata.update(plugin))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -78,7 +78,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,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user