YUKI Hiroshi
null+****@clear*****
Wed Nov 5 13:04:23 JST 2014
YUKI Hiroshi 2014-11-05 13:04:23 +0900 (Wed, 05 Nov 2014) New Revision: ab69c07fa32e142050b8b06387ecfb5f7e78819c https://github.com/droonga/express-droonga/commit/ab69c07fa32e142050b8b06387ecfb5f7e78819c Message: Supports member-* events Modified files: lib/serf/agent.js Modified: lib/serf/agent.js (+46 -14) =================================================================== --- lib/serf/agent.js 2014-11-05 12:47:29 +0900 (b1d25b6) +++ lib/serf/agent.js 2014-11-05 13:04:23 +0900 (7306bd3) @@ -9,19 +9,25 @@ * serf.start() * .then(function() { ... }) * .catch(function(error) { ... }); + * serf.on('member-join', function(nodeName) { console.log(nodeName + ' joined'); }); + * serf.on('member-leave', function(nodeName) { console.log(nodeName + ' leaved'); }); + * serf.on('member-failed', function(nodeName) { console.log(nodeName + ' failed'); }); * serf.shutdown(); */ -var exec = require('child_process').exec, +var EventEmitter = require('events').EventEmitter, + exec = require('child_process').exec, fs = require('fs'), path = require('path'), Q = require('q'), - spawn = require('child_process').spawn; + spawn = require('child_process').spawn, + util = require('util'); var ConsoleLogger = require('../console-logger').ConsoleLogger, Downloader = require('./downloader'); var NODE_NAME_PATTERN = /^([^:]+):(\d+)\/(.+)$/; +var EVENT_LOG_PATTERN = /\[INFO\] serf: ([^:]+): ([^\s]+) ([^\s]+)/; var DEFAULT_BIND_PORT = 7946; var BIND_PORT = 8946; var RPC_PORT = 8373; @@ -32,6 +38,10 @@ function Agent(options) { this._logger = options.logger || new ConsoleLogger(); this._hostName = options.hostName; + this._nodeName = this._hostName + '/protocol-adapter'; + + this.rpcAddress = this._hostName + ':' + RPC_PORT; + this._serf = options.serf || 'serf'; if (this._serf.charAt(0) == '.') this._serf = path.resolve(this._serf); @@ -43,12 +53,10 @@ function Agent(options) { this._agentProcess = null; this.shutdown = this.shutdown.bind(this); } -Agent.prototype = { - get rpcAddress() { - return this._hostName + ':' + RPC_PORT; - }, - start: function() { +util.inherits(Agent, EventEmitter); + +Agent.prototype.start = function() { this._logger.debug('Starting Serf agent (' + this._serf + ')'); return Q.Promise((function(resolve, reject, notify) { if (this._agentProcess) @@ -76,14 +84,14 @@ Agent.prototype = { } }).bind(this)); }).bind(this)); - }, + }; - tryStart: function() { +Agent.prototype.tryStart = function() { var eventHandlerPath = path.join(__dirname, '..', '..', 'bin', 'express-droonga-serf-event-handler'); var agentArgs = [ 'agent', - '-node', this._hostName + '/protocol-adapter', + '-node', this._nodeName, '-bind', this._hostName + ':' + BIND_PORT, '-rpc-addr', this._hostName + ':' + RPC_PORT, // '-event-handler', eventHandlerPath, @@ -104,24 +112,48 @@ Agent.prototype = { this._logger.error(new Error('Serf agent is closed with error: ' + exitStatusCode)); this._agentProcess = null; }).bind(this)); + this._agentProcess.stdout.on('data', (function(data) { + this._handleOutput(data); + }).bind(this)); } catch(error) { this._agentProcess = null; this._logger.error(error); } - }, + }; + +Agent.prototype._handleOutput = function(output) { + this._logger.debug('Serf agent output: ' + output); + var matched = output.match(EVENT_LOG_PATTERN); + if (!matched) + return; + + var eventName = matched[1]; + var nodeName = matched[2]; + if (nodeName == this._nodeName) + return; + + switch (eventName) { + case 'EventMemberJoin': + return this.emit('member-join', nodeName); + case 'EventMemberLeave': + return this.emit('member-leave', nodeName); + case 'EventMemberFailed': + return this.emit('member-failed', nodeName); + } + }; - shutdown: function() { +Agent.prototype.shutdown = function() { this._logger.info('Shutting down Serf agent'); if (!this._agentProcess) return; try { + this._agentProcess.stdout.removeAllListeners(); this._agentProcess.removeAllListeners(); this._agentProcess.kill(); this._agentProcess = null; } catch(error) { this._logger.error(error); } - } -}; + }; module.exports = Agent; -------------- next part -------------- HTML����������������������������... 다운로드