Showing
2 changed files
with
160 additions
and
0 deletions
apps/web/models/zookeeperModel.js
0 → 100644
1 | +const model = require('../../../lib/model'); | ||
2 | +const {DegradeServer} = require('../../models'); | ||
3 | +const zookeeperHelpers = require('../../zookeeper-helpers'); | ||
4 | + | ||
5 | +class ZookeeperModel extends model { | ||
6 | + constructor(ctx) { | ||
7 | + super(ctx); | ||
8 | + } | ||
9 | + async getConfigAll() { | ||
10 | + return await DegradeServer.findAll(); | ||
11 | + } | ||
12 | + async setPath(path, val) { | ||
13 | + let degradeServer = await DegradeServer.findAll(); | ||
14 | + let results = []; | ||
15 | + | ||
16 | + (degradeServer || []).each(item => { | ||
17 | + results.push(await zookeeperHelpers.creator(`${item.ip}:${item.port}`, path, val, true)); | ||
18 | + }); | ||
19 | + | ||
20 | + return results[0]; // return Boolean | ||
21 | + } | ||
22 | + async getPath(path) { | ||
23 | + let degradeServer = await DegradeServer.findAll(); | ||
24 | + | ||
25 | + if (degradeServer[0]) { | ||
26 | + return await zookeeperHelpers.getter(`${item.ip}:${item.port}`, path); | ||
27 | + } | ||
28 | + | ||
29 | + return null; | ||
30 | + } | ||
31 | +} | ||
32 | + | ||
33 | +module.exports = ZookeeperModel; |
apps/zookeeper/zookeeper-helpers.js
0 → 100644
1 | +'usu strict'; | ||
2 | + | ||
3 | +const _ = require('lodash'); | ||
4 | +const zookeeper = require('node-zookeeper-client'); | ||
5 | + | ||
6 | +const _exists = (client, path) => new Promise((resolve, reject) => { | ||
7 | + client.exists(path, (err, stat) => { | ||
8 | + if (err) { | ||
9 | + console.log('path %s exits error', path, err.stack); | ||
10 | + resolve(false); | ||
11 | + } else { | ||
12 | + resolve(stat ? true : false); | ||
13 | + } | ||
14 | + }); | ||
15 | +}); | ||
16 | + | ||
17 | +const exists = (server, path) => new Promise((resolve, reject) => { | ||
18 | + const client = zookeeper.createClient(server); | ||
19 | + | ||
20 | + client.once('connected', function () { | ||
21 | + _exists(client, path).then(stat => { | ||
22 | + resolve(stat); | ||
23 | + client.close(); | ||
24 | + }); | ||
25 | + }); | ||
26 | + | ||
27 | + client.connect(); | ||
28 | +}); | ||
29 | + | ||
30 | +const creator = (server, path, value, iscover) => new Promise((resolve, reject) => { | ||
31 | + const client = zookeeper.createClient(server); | ||
32 | + | ||
33 | + client.once('connected', function () { | ||
34 | + _exists(client, path).then(stat => { | ||
35 | + if (stat && iscover) { | ||
36 | + client.setData(path, new Buffer(val.toString()), function(err, data, stat) { | ||
37 | + if (err) { | ||
38 | + console.log('update path %s data error'); | ||
39 | + resolve(false); | ||
40 | + } else { | ||
41 | + console.log('path %s data change to', path, val); | ||
42 | + resolve(true); | ||
43 | + } | ||
44 | + }); | ||
45 | + } else if (stat) { | ||
46 | + resolve(true); | ||
47 | + } else { | ||
48 | + client.mkdirp(path, new Buffer(value), (err, path) => { | ||
49 | + if (err) { | ||
50 | + console.log('Node %s create err', path, err.stack); | ||
51 | + resolve(false); | ||
52 | + } else { | ||
53 | + console.log('Node %s is created', path); | ||
54 | + resolve(true); | ||
55 | + } | ||
56 | + }); | ||
57 | + } | ||
58 | + client.close(); | ||
59 | + }); | ||
60 | + }); | ||
61 | + | ||
62 | + client.connect(); | ||
63 | +}); | ||
64 | + | ||
65 | +const getter = (server, path) => new Promise((resolve, reject) => { | ||
66 | + const client = zookeeper.createClient(server); | ||
67 | + | ||
68 | + client.once('connected', function () { | ||
69 | + _exists(client, path).then(stat => { | ||
70 | + if (stat) { | ||
71 | + client.getData( | ||
72 | + path, | ||
73 | + (err, data, statData) => { | ||
74 | + if (err) { | ||
75 | + console.log('Got path %s data error', path, err.stack); | ||
76 | + } | ||
77 | + | ||
78 | + client.close(); | ||
79 | + | ||
80 | + resolve(data && data.toString('utf8')); | ||
81 | + } | ||
82 | + ) | ||
83 | + } else { | ||
84 | + // 不存在的路径 | ||
85 | + console.log('no path %s, we will create it with value "false" automatic', path); | ||
86 | + client.close(); | ||
87 | + resolve(); | ||
88 | + } | ||
89 | + }) | ||
90 | + }); | ||
91 | + | ||
92 | + client.connect(); | ||
93 | +}); | ||
94 | + | ||
95 | +const setter = (server, path, val) => new Promise((resolve, reject) => { | ||
96 | + const client = zookeeper.createClient(server); | ||
97 | + | ||
98 | + client.once('connected', function () { | ||
99 | + _exists(client, path).then(stat => { | ||
100 | + if (stat) { | ||
101 | + client.setData(path, new Buffer(val.toString()), function(err, data, stat) { | ||
102 | + if (err) { | ||
103 | + console.log('update path %s data error'); | ||
104 | + resolve(false); | ||
105 | + } else { | ||
106 | + console.log('path %s data change to', path, val); | ||
107 | + resolve(true); | ||
108 | + } | ||
109 | + | ||
110 | + client.close(); | ||
111 | + }); | ||
112 | + } else { | ||
113 | + resolve(false); | ||
114 | + } | ||
115 | + }); | ||
116 | + }); | ||
117 | + | ||
118 | + client.connect(); | ||
119 | +}); | ||
120 | + | ||
121 | + | ||
122 | +module.exports = { | ||
123 | + exists, | ||
124 | + creator, | ||
125 | + setter, | ||
126 | + getter | ||
127 | +}; |
-
Please register or login to post a comment