Skip to content

Instantly share code, notes, and snippets.

@cnwangjie
Last active August 30, 2018 05:21
Show Gist options
  • Save cnwangjie/03448cb3183340ade4b875b932c67ed5 to your computer and use it in GitHub Desktop.
Save cnwangjie/03448cb3183340ade4b875b932c67ed5 to your computer and use it in GitHub Desktop.
healthcheck examples
// 支持es6 module和async function的写法
export const healthcheck = {
required: '*',
info () {
return 'version: ' + this.required
},
async check () {
setTimeout(() => reject(new Error('timeout')), 1000)
if (teambitionConn.readyState !== 1) {
teambitionConn.once('error', err => throw err)
await new Promise(r => teambitionConn.once('connected', r))
}
await p.consumer.findOne({}).exec()
const info = await teambitionConn.db.command({ buildInfo: 1 })
const version = info.version
if (semver.satisfies(version, this.required)) return 'version: ' + version
throw new Error('version: ' + version)
}
}
// 支持es6 module和async function的写法
export const healthcheck = {
required: '*',
info () {
return 'version: ' + this.required // 返回需求相关的信息
},
async check () {
setTimeout(() => throw new Error('timeout'), 5000) // 超时
if (!client.clientState().connected) { // thunk-redis 获得连接状态的方法,其他库不同
client.once('error', err => throw err
await new Promise(r => client.once('connect', r) // 如果执行至此还没有连接的话则等待连接
}
const info = await client.info('server')
const version = info.redis_version
if (semver.satisfies(version, this.required)) // 使用semver匹配版本
return 'version: ' + version
throw new Error('version: ' + version)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment