Skip to content

Commit 68c4ccc

Browse files
authored
feat(cluster): add Cluster#quit() to quit cluster gracefully. (#339)
Close #315
1 parent 0ac7eef commit 68c4ccc

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

API.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ the current connection.
153153
var redis = new Redis();
154154
redis.monitor(function (err, monitor) {
155155
// Entering monitoring mode.
156-
monitor.on('monitor', function (time, args) {
156+
monitor.on('monitor', function (time, args, source, database) {
157157
console.log(time + ": " + util.inspect(args));
158158
});
159159
});
160160

161161
// supports promise as well as other commands
162162
redis.monitor().then(function (monitor) {
163-
monitor.on('monitor', function (time, args) {
163+
monitor.on('monitor', function (time, args, source, database) {
164164
console.log(time + ": " + util.inspect(args));
165165
});
166166
});
@@ -218,6 +218,7 @@ Create a Redis instance
218218
* [new Cluster(startupNodes, options)](#new_Cluster_new)
219219
* [.connect()](#Cluster+connect) ⇒ <code>Promise</code>
220220
* [.disconnect()](#Cluster+disconnect)
221+
* [.quit(callback)](#Cluster+quit) ⇒ <code>Promise</code>
221222
* [.nodes([role])](#Cluster+nodes) ⇒ <code>[Array.&lt;Redis&gt;](#Redis)</code>
222223
* [.getBuiltinCommands()](#Commander+getBuiltinCommands) ⇒ <code>Array.&lt;string&gt;</code>
223224
* [.createBuiltinCommand(commandName)](#Commander+createBuiltinCommand) ⇒ <code>object</code>
@@ -258,6 +259,19 @@ Disconnect from every node in the cluster.
258259

259260
**Kind**: instance method of <code>[Cluster](#Cluster)</code>
260261
**Access:** public
262+
<a name="Cluster+quit"></a>
263+
264+
### cluster.quit(callback) ⇒ <code>Promise</code>
265+
Quit the cluster gracefully.
266+
267+
**Kind**: instance method of <code>[Cluster](#Cluster)</code>
268+
**Returns**: <code>Promise</code> - return 'OK' if successfully
269+
**Access:** public
270+
271+
| Param | Type |
272+
| --- | --- |
273+
| callback | <code>function</code> |
274+
261275
<a name="Cluster+nodes"></a>
262276

263277
### cluster.nodes([role]) ⇒ <code>[Array.&lt;Redis&gt;](#Redis)</code>

lib/cluster/index.js

+23
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,29 @@ Cluster.prototype.disconnect = function (reconnect) {
208208
this.connectionPool.reset([]);
209209
};
210210

211+
/**
212+
* Quit the cluster gracefully.
213+
*
214+
* @param {function} callback
215+
* @return {Promise} return 'OK' if successfully
216+
* @public
217+
*/
218+
Cluster.prototype.quit = function (callback) {
219+
this.setStatus('disconnecting');
220+
221+
this.manuallyClosing = true;
222+
223+
if (this.reconnectTimeout) {
224+
clearTimeout(this.reconnectTimeout);
225+
this.reconnectTimeout = null;
226+
}
227+
return Promise.all(this.nodes().map(function (node) {
228+
return node.quit();
229+
})).then(function () {
230+
return 'OK';
231+
}).nodeify(callback);
232+
};
233+
211234
/**
212235
* Get nodes with the specified role
213236
*

test/functional/cluster.js

+34
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,40 @@ describe('cluster', function () {
12841284
});
12851285
});
12861286
});
1287+
1288+
describe('#quit()', function () {
1289+
it('should quit the connection gracefully', function (done) {
1290+
var slotTable = [
1291+
[0, 1, ['127.0.0.1', 30001]],
1292+
[2, 16383, ['127.0.0.1', 30002], ['127.0.0.1', 30003]]
1293+
];
1294+
var argvHandler = function (argv) {
1295+
if (argv[0] === 'cluster' && argv[1] === 'slots') {
1296+
return slotTable;
1297+
}
1298+
};
1299+
var node1 = new MockServer(30001, argvHandler);
1300+
var node2 = new MockServer(30002, argvHandler);
1301+
var node3 = new MockServer(30003, argvHandler);
1302+
1303+
var cluster = new Redis.Cluster([
1304+
{ host: '127.0.0.1', port: '30001' }
1305+
]);
1306+
1307+
var setCommandHandled = false;
1308+
cluster.on('ready', function () {
1309+
cluster.set('foo', 'bar', function () {
1310+
setCommandHandled = true;
1311+
});
1312+
cluster.quit(function (err, state) {
1313+
expect(setCommandHandled).to.eql(true);
1314+
expect(state).to.eql('OK');
1315+
cluster.disconnect();
1316+
disconnect([node1, node2, node3], done);
1317+
});
1318+
});
1319+
});
1320+
});
12871321
});
12881322

12891323
function disconnect(clients, callback) {

0 commit comments

Comments
 (0)