YUKI Hiroshi
null+****@clear*****
Wed Nov 19 16:50:58 JST 2014
YUKI Hiroshi 2014-11-19 16:50:58 +0900 (Wed, 19 Nov 2014) New Revision: 83611b9e3d37a6be39b963bed054cd83620db7ca https://github.com/droonga/express-droonga/commit/83611b9e3d37a6be39b963bed054cd83620db7ca Message: Make more methods to communicate with Serf thenable Modified files: lib/droonga-protocol/connection-pool.js lib/serf/agent.js Modified: lib/droonga-protocol/connection-pool.js (+23 -16) =================================================================== --- lib/droonga-protocol/connection-pool.js 2014-11-19 16:45:07 +0900 (6c9351e) +++ lib/droonga-protocol/connection-pool.js 2014-11-19 16:50:58 +0900 (fe1c777) @@ -169,8 +169,9 @@ ConnectionPool.prototype = { }, startWatchClusterChanges: function() { + return Q.Promise((function(resolve, reject, notify) { if (this._watching) - return; + return resolve(); this._serf = new SerfAgent({ serf: this._params.serf, hostName: this._params.receiveHostName, @@ -180,11 +181,7 @@ ConnectionPool.prototype = { this._serf.start() .then((function() { this._logger.info('Start to watch changes in the cluster.'); - }).bind(this)) - .catch(function(error) { - this._logger.error('Failed to start watching of changes in the cluster.'); - this._logger.error(error); - }); + this._watching = true; this._serf.on('member-change', (function() { if (this._updateHostNamesTimer) return; @@ -195,27 +192,37 @@ ConnectionPool.prototype = { .then(this.startWatchClusterChanges.bind(this)); }).bind(this), 500); }).bind(this)); - this._watching = true; + resolve(); + }).bind(this)) + .catch(function(error) { + this._logger.error('Failed to start watching of changes in the cluster.'); + this._logger.error(error); + reject(error); + }); + }).bind(this)); }, endWatchClusterChanges: function() { + return Q.Promise((function(resolve, reject, notify) { if (!this._watching) - return; - this._serf.shutdown(); - delete this._serf; - this._watching = false; + return resolve(); + this._serf.shutdown() + .then((function() { + delete this._serf; + this._watching = false; + resolve(); + }).bind(this)); + }).bind(this)); }, startSyncHostNamesFromCluster: function() { - this.updateHostNamesFromCluster() - .then((function(hostNames) { - this.startWatchClusterChanges(); - }).bind(this)); + return this.updateHostNamesFromCluster() + .then(this.startWatchClusterChanges.bind(this)); }, stopSyncHostNamesFromCluster: function() { - this.endWatchClusterChanges(); + return this.endWatchClusterChanges(); } }; Modified: lib/serf/agent.js (+5 -1) =================================================================== --- lib/serf/agent.js 2014-11-19 16:45:07 +0900 (77e3638) +++ lib/serf/agent.js 2014-11-19 16:50:58 +0900 (c5c1d4e) @@ -153,17 +153,21 @@ Agent.prototype._handleOutput = function(output) { Agent.prototype.shutdown = function() { this._logger.info('Shutting down Serf agent'); + return Q.Promise((function(resolve, reject, notify) { this.removeAllListeners(); if (!this._agentProcess) - return; + return resolve(); try { this._agentProcess.stdout.removeAllListeners(); this._agentProcess.removeAllListeners(); this._agentProcess.kill(); this._agentProcess = null; + resolve(); } catch(error) { this._logger.error(error); + reject(error); } + }).bind(this)); }; module.exports = Agent; -------------- next part -------------- HTML����������������������������... ダウンロード