index.js
1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* zookeeper client
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/10/12
*/
'use strict';
const zookeeper = require('node-zookeeper-client');
const _ = require('lodash');
const ONE_MONTH = 60 * 60 * 24 * 30;
const getter = (client, path, memory, cache) => {
client.getData(
path,
event => {
getter(client, path, memory, cache);
},
(err, data, stat) => {
if (err) {
console.log('Got path %s data error', path);
return;
}
let keys = path.replace(/^\/(pc|wap)\//, '').split('/');
memory && _.set(memory, keys, data.toString('utf8') === 'true' ? true : false);
if (cache) {
cache.set('zookeeper:' + path, data.toString('utf8'), ONE_MONTH).catch(console.error);
console.log('cache %s data: %s', 'zookeeper:' + path, data.toString('utf8'));
}
console.log('%s data: %s', path, data.toString('utf8'));
}
)
};
// 遍历node path
const walkPath = (client, path, memory, cache) => {
client.getChildren(
path,
(err, children, stat) => {
if (err) {
console.log('Failed to list children of %s due to: %s', path, err);
return;
}
if (children.length === 0) {
// watch the path
getter(client, path, memory, cache);
} else {
_.forEach(children, child => {
walkPath(client, `${path}/${child}`, memory, cache);
});
}
}
)
};
module.exports = (server, host, memory, cache) => {
let client = zookeeper.createClient(server, {
retries: 3
});
host = (host && _.indexOf(['pc', 'wap'], host) >= 0) ? host : 'wap';
client.once('connected', () => {
walkPath(client, `/${host}`, memory, cache)
});
client.connect();
};