[Groonga-commit] droonga/express-droonga at 2f8bbf8 [master] Add command line tool to collect live engine hosts from the backend.

Back to archive index

YUKI Hiroshi null+****@clear*****
Fri Oct 31 19:08:34 JST 2014


YUKI Hiroshi	2014-10-31 19:08:34 +0900 (Fri, 31 Oct 2014)

  New Revision: 2f8bbf8aa5b3138bdbe6bf98c54239cc5460e8cb
  https://github.com/droonga/express-droonga/commit/2f8bbf8aa5b3138bdbe6bf98c54239cc5460e8cb

  Message:
    Add command line tool to collect live engine hosts from the backend.
    
    Codes are imported from droonga-http-server.

  Added files:
    bin/express-droonga-report-live-engine-hosts
    lib/serf/client.js
    lib/serf/options.js
  Modified files:
    package.json

  Added: bin/express-droonga-report-live-engine-hosts (+24 -0) 100755
===================================================================
--- /dev/null
+++ bin/express-droonga-report-live-engine-hosts    2014-10-31 19:08:34 +0900 (8df0c0c)
@@ -0,0 +1,24 @@
+#!/usr/bin/env node
+// -*- js -*-
+var SerfClient = require('../lib/serf/client');
+
+var options = require('../lib/serf/options');
+options = options.define().parse(process.argv);
+
+var client = new SerfClient({
+  host:       options.droongaEngineHostName,
+  enginePort: options.droongaEnginePort,
+  tag:        options.tag
+});
+
+client.getLiveEngineNodes()
+  .then(function(members) {
+    members.forEach(function(member) {
+      console.log(member.HostName);
+    });
+    process.exit(true);
+  })
+  .catch(function(error) {
+    console.error(error);
+    process.exit(false);
+  });

  Added: lib/serf/client.js (+102 -0) 100644
===================================================================
--- /dev/null
+++ lib/serf/client.js    2014-10-31 19:08:34 +0900 (9cc9494)
@@ -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._droongaEnginePort &&
+                 matched[3] == this._droongaTag &&
+                 member.Status == 'alive';
+      }, this);
+    }).bind(this));
+  }
+};
+
+module.exports = Client;

  Added: lib/serf/options.js (+42 -0) 100644
===================================================================
--- /dev/null
+++ lib/serf/options.js    2014-10-31 19:08:34 +0900 (5109433)
@@ -0,0 +1,42 @@
+var options = require('commander');
+
+options.droongaEngineHostName = '127.0.0.1';
+options.droongaEnginePort     = 10031;
+options.tag                   = 'droonga';
+
+function intOption(newValue, oldValue) {
+  return parseInt(newValue);
+}
+
+function generateOptionHandler(onHandle, converter) {
+  return function(newValue, oldValue) {
+    onHandle(newValue);
+    if (converter)
+      return converter(newValue);
+    else
+      return newValue;
+  };
+}
+
+function add() {
+  options = options.option.apply(options, arguments);
+  return exports;
+}
+exports.add = add;
+
+function define() {
+  add('--droonga-engine-host-name <name>',
+      'Host name of Droonga engine (' + options.droongaEngineHostName + ')');
+  add('--droonga-engine-port <port>',
+      'Port number of Droonga engine (' + options.droongaEnginePort + ')',
+      intOption);
+  add('--tag <tag>',
+      'The tag (' + options.tag + ')');
+  return exports;
+}
+exports.define = define;
+
+function parse(argv) {
+  return options.parse(argv);
+}
+exports.parse = parse;

  Modified: package.json (+3 -0)
===================================================================
--- package.json    2014-10-30 18:36:24 +0900 (18b392e)
+++ package.json    2014-10-31 19:08:34 +0900 (11c4ecb)
@@ -20,6 +20,7 @@
   "license": "MIT",
   "dependencies": {
     "body-parser": "*",
+    "commander": "*",
     "connect": "*",
     "cookie": "*",
     "errorhandler": "*",
@@ -32,6 +33,7 @@
     "method-override": "*",
     "msgpack": "*",
     "q": "*",
+    "serf-rpc": "*",
     "serve-static": "*",
     "socket.io": ">=1.0",
     "winston": "*"
@@ -45,6 +47,7 @@
     "supertest": "*"
   },
   "scripts": {
+    "express-droonga-report-live-engine-hosts": "./bin/express-droonga-report-live-engine-hosts",
     "test": "NODE_ENV=test ./node_modules/.bin/mocha --reporter list --ui tdd --timeout 5s --recursive"
   },
   "main": "index",
-------------- next part --------------
HTML����������������������������...
下載 



More information about the Groonga-commit mailing list
Back to archive index