Authored by yyq

zk onerror callback

Showing 1 changed file with 22 additions and 3 deletions
... ... @@ -67,16 +67,35 @@ const walkPath = (client, path, memory, cache) => {
)
};
module.exports = (server, host, memory, cache) => {
module.exports = (server, host, memory, cache, extra = {}) => {
let client = zookeeper.createClient(server, {
retries: 3
spinDelay: 1000,
retries: 10
});
client.state = Symbol();
host = (host && _.indexOf(['pc', 'wap'], host) >= 0) ? host : 'wap';
client.once('connected', () => {
client.on('connected', () => {
walkPath(client, `/${host}`, memory, cache)
});
client.on('state', state => {
if (!extra || !extra.onerror) {
return;
}
switch (state) {
case zookeeper.State.DISCONNECTED:
case zookeeper.State.EXPIRED:
case zookeeper.State.AUTH_FAILED:
extra.onerror({...state});
break;
default:
return;
}
});
client.connect();
};
... ...