-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
200 lines (154 loc) · 4.78 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 2014, Joyent, Inc.
*/
var fs = require('fs');
var assert = require('assert-plus');
var bunyan = require('bunyan');
var clone = require('clone');
var dashdash = require('dashdash');
var app = require('./lib');
///--- Globals
var LOG = bunyan.createLogger({
level: (process.env.LOG_LEVEL || 'info'),
name: 'registrar',
serializers: bunyan.stdSerializers,
stream: process.stdout
});
var OPTIONS = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Print this help and exit.'
},
{
names: ['verbose', 'v'],
type: 'arrayOfBool',
help: 'Verbose output. Use multiple times for more verbose.'
},
{
names: ['file', 'f'],
type: 'string',
help: 'File to process',
helpArg: 'FILE'
}
];
///--- CLI Helpers
function configure(argv) {
assert.object(argv, 'options');
assert.string(argv.file, 'options.file');
var cfg;
try {
cfg = JSON.parse(fs.readFileSync(argv.file, 'utf8'));
} catch (e) {
LOG.fatal(e, 'unable to read configuration %s', argv.file);
process.exit(1);
}
LOG.info(cfg, 'configuration loaded from %s', argv.file);
if (cfg.logLevel)
LOG.level(cfg.logLevel);
if (argv.verbose) {
argv.verbose.forEach(function () {
LOG.level(Math.max(bunyan.TRACE, (LOG.level() - 10)));
});
}
if (LOG.level() <= bunyan.DEBUG)
LOG = LOG.child({src: true});
assert.object(cfg.zookeeper, 'config.zookeeper');
assert.optionalObject(cfg.healthCheck, 'config.healthCheck');
cfg.zookeeper.log = LOG;
return (cfg);
}
function usage(help, msg) {
if (msg)
console.error(msg);
console.log('usage: registrar [OPTIONS]\n'
+ 'options:\n'
+ help);
process.exit(msg ? 1 : 0);
}
///--- Mainline
(function main() {
var argv;
var help;
var parser;
try {
parser = dashdash.createParser({options: OPTIONS});
argv = parser.parse(process.argv);
help = parser.help({includeEnv: true}).trimRight();
} catch (e) {
console.error('foo: error: %s', e.message);
process.exit(1);
}
if (argv.help)
usage(help);
if (!argv.file)
usage(help, 'file is required');
var cfg = configure(argv);
app.createZKClient(cfg.zookeeper, function (init_err, zk) {
if (init_err) {
LOG.fatal(init_err, 'unable to create ZooKeeper client');
process.exit(1);
}
zk.on('close', function () {
LOG.warn('zookeeper: disconnected');
});
// annoyingly this fires twice, so ignore the first one
zk.once('connect', function () {
zk.on('connect', function () {
LOG.info('zookeeper: reconnected');
});
});
zk.on('session_expired', function force_restart() {
LOG.fatal('Zookeeper session_expired event; exiting');
process.exit(1);
});
// backward compatible with top-level 'adminIp' in configs.
cfg.registration.adminIp = cfg.registration.adminIp || cfg.adminIp;
var is_down = false;
var opts = clone(cfg.registration);
if (cfg.healthCheck) {
opts.healthCheck = clone(cfg.healthCheck);
opts.healthCheck.log = LOG;
opts.healthCheck.zk = zk;
}
opts.log = LOG;
opts.registration = cfg.registration;
opts.zk = zk;
var eventStream = app(opts);
eventStream.on('fail', function (err) {
LOG.error(err, 'registrar: healthcheck failed');
});
eventStream.on('ok', function () {
LOG.info('registrar: healthcheck ok (was down)');
});
eventStream.on('error', function (err) {
LOG.error(err, 'registrar: unexpected error');
});
eventStream.on('register', function (nodes) {
LOG.info({
znodes: nodes
}, 'registrar: registered');
});
eventStream.on('unregister', function (err, nodes) {
LOG.warn({
err: err,
znodes: nodes
}, 'registrar: unregistered');
});
eventStream.on('heartbeatFailure', function (err) {
if (!is_down)
LOG.error(err, 'zookeeper: heartbeat failed');
is_down = true;
});
eventStream.on('heartbeat', function () {
if (is_down)
LOG.info('zookeeper heartbeat ok');
is_down = false;
});
});
})();