Xo#getStore(namespace).

This commit is contained in:
Julien Fontanet 2016-02-02 17:45:42 +01:00
parent 5456e4fe75
commit c0ed3a9e3c
4 changed files with 59 additions and 19 deletions

View File

@ -1,5 +1,5 @@
export async function get ({namespace}) {
const logger = this.getLogger(namespace)
const logger = await this.getLogger(namespace)
return new Promise((resolve, reject) => {
const logs = {}
@ -17,8 +17,8 @@ export async function get ({namespace}) {
get.description = 'returns logs list for one namespace'
function delete_ ({namespace, id}) {
const logger = this.getLogger(namespace)
async function delete_ ({namespace, id}) {
const logger = await this.getLogger(namespace)
logger.del(id)
}

View File

@ -50,9 +50,9 @@ export default class JobExecutor {
}
// The logger is not available until Xo has started.
xo.on('started', () => {
this._logger = this.xo.getLogger('jobs')
})
xo.on('starting', () => xo.getLogger('jobs').then(logger => {
this._logger = logger
}))
}
async exec (job) {

50
src/xo-mixins/store.js Normal file
View File

@ -0,0 +1,50 @@
import levelup from 'level-party'
import sublevel from 'level-sublevel'
import { ensureDir } from 'fs-promise'
// ===================================================================
const _levelHas = function has (key, cb) {
if (cb) {
return this.get(key, (error, value) => error
? (
error.notFound
? cb(null, false)
: cb(error)
)
: cb(null, true)
)
}
try {
this.get(key)
return true
} catch (error) {
if (!error.notFound) {
throw error
}
}
return false
}
const levelHas = db => {
db.has = _levelHas
return db
}
// ===================================================================
export default class {
constructor (xo) {
const dir = `${xo._config.datadir}/leveldb`
this._db = ensureDir(dir).then(() => {
return sublevel(levelup(dir, {
valueEncoding: 'json'
}))
})
}
getStore (namespace) {
return this._db.then(db => levelHas(db.sublevel(namespace)))
}
}

View File

@ -4,8 +4,6 @@ import fs from 'fs-promise'
import includes from 'lodash.includes'
import isFunction from 'lodash.isfunction'
import isString from 'lodash.isstring'
import levelup from 'level-party'
import sublevel from 'level-sublevel'
import XoCollection from 'xo-collection'
import XoUniqueIndex from 'xo-collection/unique-index'
import {createClient as createRedisClient} from 'redis'
@ -107,16 +105,8 @@ export default class Xo extends EventEmitter {
async start () {
this.start = noop
const { _config: config } = this
this._watchObjects()
await fs.mkdirp(config.datadir)
this._leveldb = sublevel(levelup(`${config.datadir}/leveldb`, {
valueEncoding: 'json'
}))
// ---------------------------------------------------------------
const redis = this._redis
@ -212,10 +202,10 @@ export default class Xo extends EventEmitter {
// -----------------------------------------------------------------
getLogger (namespace) {
return new LevelDbLogger(
this._leveldb.sublevel('logs'),
return this.getStore('logs').then(store => new LevelDbLogger(
store,
namespace
)
))
}
// -----------------------------------------------------------------