Skip to content

Commit

Permalink
fix(core): sync is done correctly when enabling bpfs first time
Browse files Browse the repository at this point in the history
  • Loading branch information
allardy committed Dec 5, 2019
1 parent c36ad89 commit 20c8fba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/bp/bootstrap.ts
Expand Up @@ -18,10 +18,10 @@ import { setupMasterNode } from './cluster'
import { FatalError } from './errors'

async function setupEnv() {
const useDbDriver = process.BPFS_STORAGE === 'database'
Ghost.initialize(useDbDriver)

await Db.initialize()

const useDbDriver = process.BPFS_STORAGE === 'database'
await Ghost.initialize(useDbDriver)
}

async function getLogger(loggerName: string) {
Expand Down
15 changes: 0 additions & 15 deletions src/bp/core/botpress.ts
Expand Up @@ -273,21 +273,6 @@ export class Botpress {
await Promise.map(botsToMount, botId => this.botService.mountBot(botId))
}

@WrapErrorsWith('Error initializing Ghost Service')
async initializeGhost(): Promise<void> {
const useDbDriver = process.BPFS_STORAGE === 'database'
this.ghostService.initialize(useDbDriver)
const global = await this.ghostService.global().directoryListing('/')

if (useDbDriver && _.isEmpty(global)) {
this.logger.info('Syncing data/global/ to database')
await this.ghostService.global().sync()

this.logger.info('Syncing data/bots/ to database')
await this.ghostService.bots().sync()
}
}

private async initializeServices() {
await this.loggerDbPersister.initialize(this.database, await this.loggerProvider('LogDbPersister'))
this.loggerDbPersister.start()
Expand Down
14 changes: 12 additions & 2 deletions src/bp/core/services/ghost/service.ts
Expand Up @@ -61,9 +61,19 @@ export class GhostService {
this.cache.events.on && this.cache.events.on('syncDbFilesToDisk', this._onSyncReceived)
}

initialize(enabled: boolean) {
this.enabled = enabled
async initialize(useDbDriver: boolean) {
this.enabled = useDbDriver
this._scopedGhosts.clear()

const global = await this.global().directoryListing('/')

if (useDbDriver && _.isEmpty(global)) {
this.logger.info('Syncing data/global/ to database')
await this.global().sync()

this.logger.info('Syncing data/bots/ to database')
await this.bots().sync()
}
}

// Not caching this scope since it's rarely used
Expand Down

0 comments on commit 20c8fba

Please sign in to comment.