-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
252 lines (211 loc) · 6.68 KB
/
index.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'use strict';
var childProcess = require('child_process'),
Client = require('cassandra-driver').Client,
EventEmitter = require('events').EventEmitter,
fs = require('fs-extra'),
path = require('path'),
Q = require('q'),
yaml = require('js-yaml');
// the server process
var child;
// the Cassandra client
var client;
// the Cassandra log levels
var levels = ['debug', 'info', 'warn'];
// the amount of time to wait before restarting the server
var restartWaitTime = 3000;
// the amount of time to wait before connecting the client
var startWaitTime = 5000;
/**
* Creates a YAML configuration file after overriding defaults with user options.
*
* <p>
* Cassandra expects it's configuration to reside in $CASSANDRA_DIR/conf/cassandra.yaml.
* After loading the default options from cassandra.json and overriding them with user provided options,
* this function converts the final object to YAML and writes it to the target location.
* </p>
*
* @param {Object} options - the user options
* @private
* @returns {Promise} - a promise that resolves to the merged server options
*/
function resolveOptions(options) {
var jsonFileName = path.join(__dirname, 'cassandra.json');
cassandra.emit('log', 'debug', 'Reading default configuration: ' + jsonFileName);
return Q.nfcall(fs.readFile, jsonFileName, 'utf-8')
.then(function(json) {
var defaults = JSON.parse(json);
// overriding default options
for(var property in options) {
if(options.hasOwnProperty(property)) {
defaults[property] = options[property];
}
}
cassandra.emit('log', 'debug', 'Resolved Cassandra options are: ' + JSON.stringify(defaults));
var yamlFileName = path.join(__dirname, 'apache-cassandra-2.1.0', 'conf', 'cassandra.yaml');
cassandra.emit('log', 'debug', 'Creating YAML configuration file: ' + yamlFileName);
return Q.nfcall(fs.writeFile, yamlFileName, yaml.dump(defaults))
.then(function() {
return defaults;
});
});
}
var cassandra = Object.create(EventEmitter.prototype);
module.exports = cassandra;
/**
* Nukes the entire database.
* @returns {Promise}
*/
cassandra.nuke = function() {
var deferred = Q.defer();
// if running
if(child) {
var error = new Error('Cannot delete database when Cassandra is running');
cassandra.emit('log', 'error', error);
deferred.reject(error);
return deferred.promise;
}
cassandra.emit('log', 'warn', 'Deleting all data in the database');
return Q.nfcall(fs.remove, path.join(__dirname, 'apache-cassandra-2.1.0', 'data'))
.then(Q.nfcall(fs.remove, path.join(__dirname, 'apache-cassandra-2.1.0', 'logs')));
};
/**
* Restarts the server.
* @param {Object} [options] - the server options
* @returns {Promise} - a promise that resolves to the Cassandra client
*/
cassandra.restart = function(options) {
var deferred = Q.defer();
cassandra.stop()
.then(function() {
cassandra.emit('log', 'info', 'Waiting ' + restartWaitTime + ' seconds before starting');
setTimeout(function() {
cassandra.start(options)
.then(function(client) {
deferred.resolve(client);
})
.catch(function(err) {
deferred.reject(err);
});
}, restartWaitTime);
});
return deferred.promise;
};
/**
* Starts the server.
* @param {Object} [options] - the server options
* @returns {Promise} - a promise that resolves to the Cassandra client
*/
cassandra.start = function(options) {
options = options || {};
var deferred = Q.defer(),
timeoutId;
// if already started
if(child) {
cassandra.emit('log', 'info', 'Cassandra is already started');
return deferred.resolve();
}
cassandra.emit('log', 'info', 'Starting Cassandra with options: ' + JSON.stringify(options));
resolveOptions(options)
// start the server
.then(function(merged) {
child = childProcess.spawn(path.join(__dirname, 'apache-cassandra-2.1.0', 'bin', 'cassandra'), ['-f']);
// error handler
child.on('error', function(err) {
var error = new Error('Cassandra process error');
error.cause = err;
cassandra.emit('log', 'error', error);
// if we're starting up then reject the promise
if(timeoutId) {
clearTimeout(timeoutId);
timeoutId = undefined;
deferred.reject(error);
}
});
// exit handler
child.on('exit', function(code) {
cassandra.emit('log', 'info', 'Cassandra exited with code: ' + code);
// if we're starting up then reject the promise
if(timeoutId) {
clearTimeout(timeoutId);
timeoutId = undefined;
deferred.reject(new Error('Could not start Cassandra'));
}
});
// stderr handler
child.stderr.on('data', function(data) {
cassandra.emit('log', 'stderr', data.toString());
});
// track the last log level and use it for lines that don't have a log level prefix
var lastLevel;
// stdout listener
child.stdout.on('data', function(data) {
var line = data.toString(),
isLogLine = false;
// for each log level
for(var index = 0; index < levels.length; index++) {
var regexp = new RegExp('^' + levels[index], 'i');
// if the line is prefixed by a log level
if(line.match(regexp)) {
lastLevel = levels[index];
cassandra.emit('log', levels[index], line.replace(regexp, '').trim());
isLogLine = true;
break;
}
}
// if not a log line then use the last level logged
if(!isLogLine) {
cassandra.emit('log', lastLevel, line.trim());
}
});
// give it time to start
timeoutId = setTimeout(function() {
var seeds = merged.seed_provider[0].parameters[0].seeds;
client = new Client({contactPoints: seeds});
// resolve the startup promise when the client connects
Q.ninvoke(client, 'connect')
.then(function() {
timeoutId = undefined;
cassandra.emit('log', 'info', 'Server started');
deferred.resolve();
})
.catch(function(err) {
timeoutId = undefined;
var error = new Error('Could not connect to Cassandra');
error.cause = err;
cassandra.emit('log', 'error', error);
// since the client could not connect we may as well kill the server
cassandra.stop()
.done(function() {
deferred.reject(error);
});
})
}, startWaitTime);
});
return deferred.promise;
};
/**
* Stops the server.
* @returns {Promise}
*/
cassandra.stop = function() {
// if already stopped
if(!child) {
cassandra.emit('log', 'info', 'Cassandra is already stopped');
return Q();
}
// function to stop the server
function doStop() {
cassandra.emit('log', 'info', 'Stopping Cassandra');
child.kill();
child = client = undefined;
return Q();
}
// if the client couldn't connect
if(!client) {
return doStop();
}
// shutdown the client
return Q.ninvoke(client, 'shutdown')
.then(doStop);
};