YUKI Hiroshi
null+****@clear*****
Fri Oct 31 18:31:00 JST 2014
YUKI Hiroshi 2014-10-31 18:31:00 +0900 (Fri, 31 Oct 2014) New Revision: 60a3d234d73e15df8af9d36efe836ab8058d1fee https://github.com/droonga/droonga-http-server/commit/60a3d234d73e15df8af9d36efe836ab8058d1fee Message: Add command to report list of live engine nodes. This should be a separate command, because there is no way to close the connection to the RPC node from serf-rpc based client. Added files: bin/droonga-http-server-report-live-engine-hosts lib/serf/client.js Modified files: package.json Added: bin/droonga-http-server-report-live-engine-hosts (+28 -0) 100755 =================================================================== --- /dev/null +++ bin/droonga-http-server-report-live-engine-hosts 2014-10-31 18:31:00 +0900 (e6eaf15) @@ -0,0 +1,28 @@ +#!/usr/bin/env node +// -*- js -*- +var SerfClient = require('../lib/serf/client'); + +var defaultConfigs = require('../lib/default-configs'); +var options = require('../lib/server-options'); +options = options.define().parse(process.argv); + +var baseDir = defaultConfigs.baseDir; + +var client = new SerfClient({ + host: options.droongaEngineHostName, + enginePort: options.droongaEnginePort, + tag: options.tag +}); + +client.getLiveEngineNodes() + .then(function(members) { + var hosts = members.map(function(member) { + return member.HostName; + }); + console.log(hosts.join('\n')); + process.exit(true); + }) + .catch(function(error) { + console.error(error); + process.exit(false); + }); Added: lib/serf/client.js (+102 -0) 100755 =================================================================== --- /dev/null +++ lib/serf/client.js 2014-10-31 18:31:00 +0900 (b8ab7b6) @@ -0,0 +1,102 @@ +/** + * usage: + * var Client = require('lib/serf/client'); + * var serf = new Client({ + * host: 'node0', // host name that the serf agent is working + * tag: 'droonga', // tag of engines, can be omitted + * enginePort: 10031 // port number of engines, can be omitted + * }); + * serf.getAllMembers() + * .then(function(members) { console.log(members); }) + * // members: + * // [{ Name: 'node0:10031/droonga', + * // Port: 7946, + * // Status: 'alive', ... }, ...] + * .catch(function(e) { console.error(e); }); + * serf.getLiveEngineNodes() + * .then(function(members) { console.log(members); }) + * // members: + * // [{ Name: 'node0:10031/droonga', + * // Port: 7946, + * // Status: 'alive', + * // HostName: 'node0', ... }, ...] + * .catch(function(e) { console.error(e); }); + */ + +var SerfRPC = require('serf-rpc'), + Q = require('q'); + +var ENGINE_NODE_NAME_PATTERN = /^([^:]+):(\d+)\/(.+)$/; + +function Client(options) { + options = options || {}; + + this._droongaHost = options.host || '127.0.0.1'; + this._droongaEnginePort = options.enginePort || 10031; + this._droongaTag = options.tag || 'droonga'; + + this._connectionOptions = { + rpc_host: this._droongaHost, + rpc_port: 7373 + }; + this._client = new SerfRPC(); + this._connected = false; +} +Client.prototype = { + connect: function() { + return Q.Promise((function(resolve, reject, notify) { + if (this._connected) + return resolve(); + + this._client.connect(this._connectionOptions, (function(error) { + if (error) + return reject(error); + this._connected = true; + resolve(); + }).bind(this)); + }).bind(this)); + }, + + joinTo: function(existingMemberHostName) { + return this.connect().then((function() { + return Q.Promise((function(resolve, reject, notify) { + this._client.join({ + Existing: [existingMemberHostName + ':7946'], + Replay: false + }, function(error, result) { + if (error) + return reject(error); + resolve(); + }); + }).bind(this)); + }).bind(this)); + }, + + getAllMembers: function() { + return this.connect().then((function() { + return Q.Promise((function(resolve, reject, notify) { + this._client.members(function(error, result) { + if (error) + return reject(error); + resolve(result.Members); + }); + }).bind(this)); + }).bind(this)); + }, + + getLiveEngineNodes: function() { + return this.getAllMembers().then((function(members) { + return members.filter(function(member) { + var matched = String(member.Name).match(ENGINE_NODE_NAME_PATTERN); + if (matched) + member.HostName = matched[1]; + return matched && + matched[2] == this._droongaPort && + matched[3] == this._droongaTag && + member.Status == 'alive'; + }, this); + }).bind(this)); + } +}; + +module.exports = Client; Modified: package.json (+1 -0) =================================================================== --- package.json 2014-10-31 18:28:39 +0900 (3b48414) +++ package.json 2014-10-31 18:31:00 +0900 (33e647e) @@ -33,6 +33,7 @@ "ps-node": "*", "q": "*", "response-time": "*", + "serf-rpc": "*", "touch": "*", "uid-number": "*", "winston": "*" -------------- next part -------------- HTML����������������������������... ダウンロード