Compare commits
2 Commits
gab-test-x
...
redis-json
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
941d17d669 | ||
|
|
fd2b93d38b |
@@ -34,6 +34,16 @@ import Collection, { ModelAlreadyExists } from '../collection'
|
||||
|
||||
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 {
|
||||
constructor({ connection, indexes = [], prefix, uri }) {
|
||||
super()
|
||||
@@ -108,6 +118,10 @@ export default class Redis extends Collection {
|
||||
return
|
||||
}
|
||||
|
||||
Object.keys(model).forEach(key => {
|
||||
model[key] = tryUnserialize(model[key])
|
||||
})
|
||||
|
||||
// Mix the identifier in.
|
||||
model.id = id
|
||||
|
||||
@@ -161,7 +175,7 @@ export default class Redis extends Collection {
|
||||
}
|
||||
|
||||
if (value !== undefined) {
|
||||
params.push(name, value)
|
||||
params.push(name, serialize(value))
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ export class Groups extends Collection {
|
||||
async save(group) {
|
||||
// Serializes.
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import Collection from '../collection/redis'
|
||||
import createLogger from '@xen-orchestra/log'
|
||||
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
|
||||
}
|
||||
|
||||
async save({ id, autoload, configuration }) {
|
||||
return /* await */ this.update({
|
||||
id,
|
||||
autoload: autoload ? 'true' : 'false',
|
||||
configuration: configuration && JSON.stringify(configuration),
|
||||
})
|
||||
}
|
||||
|
||||
async merge(id, data) {
|
||||
const pluginMetadata = await this.first(id)
|
||||
if (pluginMetadata === undefined) {
|
||||
@@ -35,23 +23,4 @@ export class PluginsMetadata extends Collection {
|
||||
...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 Model from '../model'
|
||||
import { forEach } from '../utils'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
@@ -10,16 +9,4 @@ export class Remotes extends Collection {
|
||||
get Model() {
|
||||
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 => {
|
||||
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
|
||||
}
|
||||
}
|
||||
class JobsDb extends Collection {}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -150,7 +108,7 @@ export default class Jobs {
|
||||
xo.addConfigManager(
|
||||
'jobs',
|
||||
() => jobsDb.get(),
|
||||
jobs => Promise.all(mapToArray(jobs, job => jobsDb.save(job))),
|
||||
jobs => Promise.all(mapToArray(jobs, job => jobsDb.update(job))),
|
||||
['users']
|
||||
)
|
||||
})
|
||||
@@ -211,8 +169,8 @@ export default class Jobs {
|
||||
return job
|
||||
}
|
||||
|
||||
createJob(job: $Diff<Job, {| id: string |}>): Promise<Job> {
|
||||
return this._jobs.create(job)
|
||||
async createJob(job: $Diff<Job, {| id: string |}>): Promise<Job> {
|
||||
return (await this._jobs.add(job)).properties
|
||||
}
|
||||
|
||||
async updateJob(job: $Shape<Job>, merge: boolean = true) {
|
||||
@@ -221,7 +179,7 @@ export default class Jobs {
|
||||
job = await this.getJob(id)
|
||||
patch(job, props)
|
||||
}
|
||||
return /* await */ this._jobs.save(job)
|
||||
return /* await */ this._jobs.update(job)
|
||||
}
|
||||
|
||||
registerJobExecutor(type: string, executor: Executor): void {
|
||||
|
||||
@@ -28,7 +28,7 @@ export default class {
|
||||
() => this._pluginsMetadata.get(),
|
||||
plugins =>
|
||||
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)
|
||||
} else {
|
||||
log.info(`[NOTICE] register plugin ${name} for the first time`)
|
||||
await this._pluginsMetadata.save({
|
||||
await this._pluginsMetadata.update({
|
||||
id,
|
||||
autoload,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user