Skip to content

Commit

Permalink
fix(xo-server-recover-account): connect Redis client (#6398)
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-f committed Sep 2, 2022
1 parent 62591e1 commit 8c14906
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
31 changes: 26 additions & 5 deletions @xen-orchestra/mixins/Hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,41 @@ export default class Hooks extends EventEmitter {
// Run *start* async listeners.
//
// They initialize the application.
//
// *startCore* is automatically called if necessary.
async start() {
assert.strictEqual(this._status, 'stopped')
if (this._status === 'stopped') {
await this.startCore()
} else {
assert.strictEqual(this._status, 'core started')
}
this._status = 'starting'
await runHook(this, 'start')
this.emit((this._status = 'started'))
}

// Run *stop* async listeners.
// Run *start core* async listeners.
//
// They initialize core features of the application (connect to databases,
// etc.) and should be fast and side-effects free.
async startCore() {
assert.strictEqual(this._status, 'stopped')
this._status = 'starting core'
await runHook(this, 'start core')
this.emit((this._status = 'core started'))
}

// Run *stop* async listeners if necessary and *stop core* listeners.
//
// They close connections, unmount file systems, save states, etc.
async stop() {
assert.strictEqual(this._status, 'started')
this._status = 'stopping'
await runHook(this, 'stop')
if (this._status !== 'core started') {
assert.strictEqual(this._status, 'started')
this._status = 'stopping'
await runHook(this, 'stop')
this._status = 'core started'
}
await runHook(this, 'stop core')
this.emit((this._status = 'stopped'))
}
}
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<!--packages-start-->

- @xen-orchestra/fs minor
- @xen-orchestra/mixins minor
- vhd-lib patch
- xo-server minor
- xo-web minor
Expand Down
4 changes: 4 additions & 0 deletions packages/xo-server/src/recover-account-cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ xo-server-recover-account <user name or email>
}),
})

await xo.hooks.startCore()

const user = await xo.getUserByName(name, true)
if (user !== null) {
await xo.updateUser(user.id, {
Expand All @@ -46,4 +48,6 @@ xo-server-recover-account <user name or email>
await xo.createUser({ name, password, permission: 'admin' })
console.log(`user ${name} has been successfully created`)
}

await xo.hooks.stop()
})
4 changes: 2 additions & 2 deletions packages/xo-server/src/xo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export default class Xo extends EventEmitter {
const redis = createRedisClient({ path, url })

this._redis = redis
this.hooks.on('start', () => redis.connect())
this.hooks.on('stop', () => redis.quit())
this.hooks.on('start core', () => redis.connect())
this.hooks.on('stop core', () => redis.quit())
}

this.hooks.on('start', () => this._watchObjects())
Expand Down

0 comments on commit 8c14906

Please sign in to comment.