forked from yfinkelstein/node-zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.js
65 lines (52 loc) · 1.44 KB
/
wrapper.js
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
const { constants, Promise: ZooKeeper } = require('../lib/index');
const logger = require('./logger.js');
const host = process.argv[2] || '127.0.0.1:2181';
let client;
let isConnected = false;
/**
* @param timeoutMs {number}
* @returns {ZooKeeper}
*/
function createClient(timeoutMs = 5000) {
if (!client) {
isConnected = false;
logger.log('creating a client.');
const config = {
connect: host,
timeout: timeoutMs,
debug_level: constants.ZOO_LOG_LEVEL_INFO,
host_order_deterministic: false,
};
client = new ZooKeeper(config);
client.on('close', () => {
isConnected = false;
logger.log('close', `session closed, id=${client.client_id}`);
client = null;
});
client.on('connecting', () => {
isConnected = false;
logger.log('connecting', `session connecting, id=${client.client_id}`);
});
client.on('connect', () => {
isConnected = true;
logger.log('connect', `session connect, id=${client.client_id}`);
});
setTimeout(() => {
client.init({});
}, 1000);
}
return client;
}
/** @returns {ZooKeeper} */
function getClient() {
return createClient();
}
function isClientConnected() {
return isConnected;
}
module.exports = {
constants,
ZooKeeper,
getClient,
isClientConnected,
};