Skip to content

Commit

Permalink
feat(cluster): add Cluster#quit() to quit cluster gracefully. (#339)
Browse files Browse the repository at this point in the history
Close #315
  • Loading branch information
luin authored Jun 28, 2016
1 parent 0ac7eef commit 68c4ccc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
18 changes: 16 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ the current connection.
var redis = new Redis();
redis.monitor(function (err, monitor) {
// Entering monitoring mode.
monitor.on('monitor', function (time, args) {
monitor.on('monitor', function (time, args, source, database) {
console.log(time + ": " + util.inspect(args));
});
});

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

**Kind**: instance method of <code>[Cluster](#Cluster)</code>
**Access:** public
<a name="Cluster+quit"></a>

### cluster.quit(callback) ⇒ <code>Promise</code>
Quit the cluster gracefully.

**Kind**: instance method of <code>[Cluster](#Cluster)</code>
**Returns**: <code>Promise</code> - return 'OK' if successfully
**Access:** public

| Param | Type |
| --- | --- |
| callback | <code>function</code> |

<a name="Cluster+nodes"></a>

### cluster.nodes([role]) ⇒ <code>[Array.&lt;Redis&gt;](#Redis)</code>
Expand Down
23 changes: 23 additions & 0 deletions lib/cluster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,29 @@ Cluster.prototype.disconnect = function (reconnect) {
this.connectionPool.reset([]);
};

/**
* Quit the cluster gracefully.
*
* @param {function} callback
* @return {Promise} return 'OK' if successfully
* @public
*/
Cluster.prototype.quit = function (callback) {
this.setStatus('disconnecting');

this.manuallyClosing = true;

if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
}
return Promise.all(this.nodes().map(function (node) {
return node.quit();
})).then(function () {
return 'OK';
}).nodeify(callback);
};

/**
* Get nodes with the specified role
*
Expand Down
34 changes: 34 additions & 0 deletions test/functional/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,40 @@ describe('cluster', function () {
});
});
});

describe('#quit()', function () {
it('should quit the connection gracefully', function (done) {
var slotTable = [
[0, 1, ['127.0.0.1', 30001]],
[2, 16383, ['127.0.0.1', 30002], ['127.0.0.1', 30003]]
];
var argvHandler = function (argv) {
if (argv[0] === 'cluster' && argv[1] === 'slots') {
return slotTable;
}
};
var node1 = new MockServer(30001, argvHandler);
var node2 = new MockServer(30002, argvHandler);
var node3 = new MockServer(30003, argvHandler);

var cluster = new Redis.Cluster([
{ host: '127.0.0.1', port: '30001' }
]);

var setCommandHandled = false;
cluster.on('ready', function () {
cluster.set('foo', 'bar', function () {
setCommandHandled = true;
});
cluster.quit(function (err, state) {
expect(setCommandHandled).to.eql(true);
expect(state).to.eql('OK');
cluster.disconnect();
disconnect([node1, node2, node3], done);
});
});
});
});
});

function disconnect(clients, callback) {
Expand Down

0 comments on commit 68c4ccc

Please sign in to comment.