fix(xo-server): create collection after connected to Redis

Introduced by 36b94f745

Redis is now connected in `start core` hook and should not be used before.

Some minor initialization stuff (namespace and version registration) where failing silently before
this fix.
This commit is contained in:
Julien Fontanet
2023-05-24 17:40:20 +02:00
parent ef35021a44
commit 9f3b020361
10 changed files with 79 additions and 72 deletions

View File

@@ -12,13 +12,13 @@ export default class {
constructor(app) {
this._app = app
const aclsDb = (this._acls = new Acls({
connection: app._redis,
namespace: 'acl',
indexes: ['subject', 'object'],
}))
app.hooks.on('start', () => {
const aclsDb = (this._acls = new Acls({
connection: app._redis,
namespace: 'acl',
indexes: ['subject', 'object'],
}))
app.addConfigManager(
'acls',
() => aclsDb.get(),
@@ -28,6 +28,7 @@ export default class {
})
app.hooks.on('clean', async () => {
const aclsDb = this._acls
const acls = await aclsDb.get()
const toRemove = []
forEach(acls, ({ subject, object, action, id }) => {

View File

@@ -36,13 +36,6 @@ export default class {
// attacks).
this._failures = { __proto__: null }
// Creates persistent collections.
const tokensDb = (this._tokens = new Tokens({
connection: app._redis,
namespace: 'token',
indexes: ['user_id'],
}))
// Password authentication provider.
this.registerAuthenticationProvider(async ({ username, password }, { ip } = {}) => {
if (username === undefined || password === undefined) {
@@ -74,6 +67,7 @@ export default class {
})
app.hooks.on('clean', async () => {
const tokensDb = this._tokens
const tokens = await tokensDb.get()
const toRemove = []
const now = Date.now()
@@ -87,6 +81,13 @@ export default class {
})
app.hooks.on('start', () => {
// Creates persistent collections.
const tokensDb = (this._tokens = new Tokens({
connection: app._redis,
namespace: 'token',
indexes: ['user_id'],
}))
app.addConfigManager(
'authTokens',
() => tokensDb.get(),

View File

@@ -12,19 +12,20 @@ class CloudConfigs extends Collection {
export default class {
constructor(app) {
this._app = app
const db = (this._db = new CloudConfigs({
connection: app._redis,
namespace: 'cloudConfig',
}))
app.hooks.on('clean', () => db.rebuildIndexes())
app.hooks.on('start', () =>
app.addConfigManager(
app.hooks.on('clean', () => this._db.rebuildIndexes())
app.hooks.on('start', () => {
const db = (this._db = new CloudConfigs({
connection: app._redis,
namespace: 'cloudConfig',
}))
return app.addConfigManager(
'cloudConfigs',
() => db.get(),
cloudConfigs => db.update(cloudConfigs)
)
)
})
}
createCloudConfig(cloudConfig) {

View File

@@ -73,19 +73,20 @@ export default class Jobs {
constructor(app) {
this._app = app
const executors = (this._executors = { __proto__: null })
const jobsDb = (this._jobs = new JobsDb({
connection: app._redis,
namespace: 'job',
indexes: ['user_id', 'key'],
}))
this._logger = undefined
this._runningJobs = { __proto__: null }
this._runs = { __proto__: null }
executors.call = executeCall
app.hooks.on('clean', () => jobsDb.rebuildIndexes())
app.hooks.on('clean', () => this._jobs.rebuildIndexes())
app.hooks.on('start', async () => {
const jobsDb = (this._jobs = new JobsDb({
connection: app._redis,
namespace: 'job',
indexes: ['user_id', 'key'],
}))
this._logger = await app.getLogger('jobs')
app.addConfigManager(

View File

@@ -20,12 +20,12 @@ export default class {
}).addVocabulary(['$multiline', '$type', 'enumNames'])
this._plugins = { __proto__: null }
this._pluginsMetadata = new PluginsMetadata({
connection: app._redis,
namespace: 'plugin-metadata',
})
app.hooks.on('start', () => {
this._pluginsMetadata = new PluginsMetadata({
connection: app._redis,
namespace: 'plugin-metadata',
})
app.addConfigManager(
'plugins',
() => this._pluginsMetadata.get(),

View File

@@ -68,20 +68,21 @@ export default class Proxy {
this._generateDefaultProxyName = compileTemplate(xoProxyConf.proxyName, rules)
this._generateDefaultVmName = compileTemplate(xoProxyConf.vmName, rules)
})
const db = (this._db = new Collection({
connection: app._redis,
indexes: ['address', 'vmUuid'],
namespace: 'proxy',
}))
app.hooks.on('clean', () => db.rebuildIndexes())
app.hooks.on('start', () =>
app.addConfigManager(
app.hooks.on('clean', () => this._db.rebuildIndexes())
app.hooks.on('start', () => {
const db = (this._db = new Collection({
connection: app._redis,
indexes: ['address', 'vmUuid'],
namespace: 'proxy',
}))
return app.addConfigManager(
'proxies',
() => db.get(),
proxies => db.update(proxies)
)
)
})
}
async _getChannel() {

View File

@@ -33,16 +33,17 @@ function validatePath(url) {
export default class {
constructor(app) {
this._handlers = { __proto__: null }
this._remotes = new Remotes({
connection: app._redis,
namespace: 'remote',
indexes: ['enabled'],
})
this._remotesInfo = {}
this._app = app
app.hooks.on('clean', () => this._remotes.rebuildIndexes())
app.hooks.on('start', async () => {
this._remotes = new Remotes({
connection: app._redis,
namespace: 'remote',
indexes: ['enabled'],
})
app.addConfigManager(
'remotes',
() => this._remotes.get(),

View File

@@ -31,11 +31,6 @@ export default class Scheduling {
constructor(app) {
this._app = app
const db = (this._db = new Schedules({
connection: app._redis,
namespace: 'schedule',
}))
this._runs = { __proto__: null }
app.hooks.on('clean', async () => {
@@ -44,11 +39,17 @@ export default class Scheduling {
app.getAllSchedules(),
])
await db.remove(schedules.filter(_ => !(_.jobId in jobsById)).map(_ => _.id))
const db = this._db
return db.rebuildIndexes()
await db.remove(schedules.filter(_ => !(_.jobId in jobsById)).map(_ => _.id))
await db.rebuildIndexes()
})
app.hooks.on('start', async () => {
const db = (this._db = new Schedules({
connection: app._redis,
namespace: 'schedule',
}))
app.addConfigManager(
'schedules',
() => db.get(),

View File

@@ -23,20 +23,19 @@ export default class {
constructor(app) {
this._app = app
const redis = app._redis
const groupsDb = (this._groups = new Groups({
connection: redis,
namespace: 'group',
}))
const usersDb = (this._users = new Users({
connection: redis,
namespace: 'user',
indexes: ['email'],
}))
app.hooks.on('clean', () => Promise.all([groupsDb.rebuildIndexes(), usersDb.rebuildIndexes()]))
app.hooks.on('clean', () => Promise.all([this._groups.rebuildIndexes(), this._users.rebuildIndexes()]))
app.hooks.on('start', async () => {
const redis = app._redis
const groupsDb = (this._groups = new Groups({
connection: redis,
namespace: 'group',
}))
const usersDb = (this._users = new Users({
connection: redis,
namespace: 'user',
indexes: ['email'],
}))
app.addConfigManager(
'groups',
() => groupsDb.get(),

View File

@@ -46,11 +46,6 @@ const log = createLogger('xo:xo-mixins:xen-servers')
export default class XenServers {
constructor(app, { safeMode }) {
this._objectConflicts = { __proto__: null } // TODO: clean when a server is disconnected.
const serversDb = (this._servers = new Servers({
connection: app._redis,
namespace: 'server',
indexes: ['host'],
}))
this._serverIdsByPool = { __proto__: null }
this._stats = new XapiStats()
this._xapis = { __proto__: null }
@@ -60,8 +55,14 @@ export default class XenServers {
this._xapiMarkDisconnectedDelay = xapiMarkDisconnectedDelay
})
app.hooks.on('clean', () => serversDb.rebuildIndexes())
app.hooks.on('clean', () => this._servers.rebuildIndexes())
app.hooks.on('start', async () => {
const serversDb = (this._servers = new Servers({
connection: app._redis,
namespace: 'server',
indexes: ['host'],
}))
const connectServers = async () => {
// Connects to existing servers.
for (const server of await serversDb.get()) {