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) => { @@ -67,16 +67,35 @@ const walkPath = (client, path, memory, cache) => {
67 ) 67 )
68 }; 68 };
69 69
70 -module.exports = (server, host, memory, cache) => { 70 +module.exports = (server, host, memory, cache, extra = {}) => {
71 let client = zookeeper.createClient(server, { 71 let client = zookeeper.createClient(server, {
72 - retries: 3 72 + spinDelay: 1000,
  73 + retries: 10
73 }); 74 });
74 75
  76 + client.state = Symbol();
  77 +
75 host = (host && _.indexOf(['pc', 'wap'], host) >= 0) ? host : 'wap'; 78 host = (host && _.indexOf(['pc', 'wap'], host) >= 0) ? host : 'wap';
76 79
77 - client.once('connected', () => { 80 + client.on('connected', () => {
78 walkPath(client, `/${host}`, memory, cache) 81 walkPath(client, `/${host}`, memory, cache)
79 }); 82 });
80 83
  84 + client.on('state', state => {
  85 + if (!extra || !extra.onerror) {
  86 + return;
  87 + }
  88 +
  89 + switch (state) {
  90 + case zookeeper.State.DISCONNECTED:
  91 + case zookeeper.State.EXPIRED:
  92 + case zookeeper.State.AUTH_FAILED:
  93 + extra.onerror({...state});
  94 + break;
  95 + default:
  96 + return;
  97 + }
  98 + });
  99 +
81 client.connect(); 100 client.connect();
82 }; 101 };