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

View File

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

View File

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

View File

@@ -73,19 +73,20 @@ export default class Jobs {
constructor(app) { constructor(app) {
this._app = app this._app = app
const executors = (this._executors = { __proto__: null }) 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._logger = undefined
this._runningJobs = { __proto__: null } this._runningJobs = { __proto__: null }
this._runs = { __proto__: null } this._runs = { __proto__: null }
executors.call = executeCall executors.call = executeCall
app.hooks.on('clean', () => jobsDb.rebuildIndexes()) app.hooks.on('clean', () => this._jobs.rebuildIndexes())
app.hooks.on('start', async () => { 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') this._logger = await app.getLogger('jobs')
app.addConfigManager( app.addConfigManager(

View File

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

View File

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

View File

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

View File

@@ -31,11 +31,6 @@ export default class Scheduling {
constructor(app) { constructor(app) {
this._app = app this._app = app
const db = (this._db = new Schedules({
connection: app._redis,
namespace: 'schedule',
}))
this._runs = { __proto__: null } this._runs = { __proto__: null }
app.hooks.on('clean', async () => { app.hooks.on('clean', async () => {
@@ -44,11 +39,17 @@ export default class Scheduling {
app.getAllSchedules(), 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 () => { app.hooks.on('start', async () => {
const db = (this._db = new Schedules({
connection: app._redis,
namespace: 'schedule',
}))
app.addConfigManager( app.addConfigManager(
'schedules', 'schedules',
() => db.get(), () => db.get(),

View File

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

View File

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