Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

woo

  • Loading branch information...
commit bdfaa4d32ed0343170b3140a80bb1df4cbfe3481 1 parent a152953
@soldair authored
Showing with 196 additions and 39 deletions.
  1. +83 −0 README.md
  2. +97 −38 index.js
  3. +16 −1 test/test.js
View
83 README.md
@@ -2,6 +2,89 @@
s3-service-registry
===================
+use s3 to publish the hosts and ports of running services
+```js
+var s3reg = require('s3-service-registry')
+var reg = s3reg(s3key,s3secret,bucket);
+
+reg.online(function(err,list){
+ console.log('online services',list)
+
+});
+
+```
+
+lists returned will be arrays of objects like. by default a tcp check is run on the host and port but you can pass in a custom check.
+
+```js
+ [ { Key: 'hi-1797733be60f08e46ee2c6a0bb41d67d5295607d-127.0.0.1-1337',
+ LastModified: Tue Feb 18 2014 16:45:29 GMT-0800 (PST),
+ ETag: '"4891f51750205e2380566db567bcd7d7"',
+ Size: 71,
+ Owner:
+ { ID: '--------',
+ DisplayName: '---------' },
+ StorageClass: 'STANDARD',
+ service: 'hi',
+ id: '-------',
+ host: '127.0.0.1',
+ port: '1337',
+ listening: false,
+ offline: true } ]
+
+```
+
+
+##API
+
+### default export
+
+```js
+ctor = require('s3-service-registry')
+instance = ctor(s3key,s3secret,bucket)
+```
+## instance methods
+
+register(service)
+
+### online(service,check,cb)
+ - service (optional)
+ prefix serce in bucket for service
+ - check(info,cb) (optional)
+ run this check instead of the default tcpCheck
+ - cb
+ callback
+
+
+### offline(service,check,cb)
+ - service (optional)
+ prefix serce in bucket for service
+ - check(info,cb) (optional)
+ run this check instead of the default tcpCheck
+ - cb
+ callback
+
+
+### status(service,check,cb)
+ - service (optional)
+ prefix serce in bucket for service
+ - check(info,cb) (optional)
+ run this check instead of the default tcpCheck
+ - cb
+ callback
+
+
+### get(key,cb)
+ - key
+ the s3 name of the file. the value of Key in list objects
+
+
+### list(service,cb)
+ - service (optional)
+ prefix serce in bucket for service
+
+### clean(keys,cb)
+ delete registered services by name.
View
135 index.js
@@ -54,9 +54,72 @@ module.exports = function(s3key,s3secret,bucket){
}));
});
},
+ offline:function(service,check,cb){
+ if(arguments.length === 2){
+ cb = check;
+ check = module.exports.tcpCheck;
+ } else if(arguments.length == 1){
+ cb = service;
+ service = false;
+ check = false;
+ }
+ var opts = {service:service,check:check,offline:true};
+ z._filtered(opts,cb);
+ },
+ online:function(service,check,cb){
+ if(arguments.length === 2){
+ cb = check;
+ check = module.exports.tcpCheck;
+ } else if(arguments.length == 1){
+ cb = service;
+ service = false;
+ check = false;
+ }
+ var opts = {service:service,check:check,offline:false};
+ z._filtered(opts,cb);
+ },
+ status:function(service,check,cb){
+ if(arguments.length === 2){
+ cb = check;
+ check = module.exports.tcpCheck;
+ } else if(arguments.length === 1){
+ cb = service;
+ service = false;
+ check = module.exports.tcpCheck;
+ }
+
+ var z = this;
+ z.list(service,function(err,list){
+ if(err) return cb(err);
+ z.check(list,check,function(err,res){
+ if(err) return cb(err);
+
+ var out = [];
+ res.forEach(function(o){
+ out.push(o);
+ });
+ cb(false,out);
+ });
+ });
+ },
+ // use online/offline/status instead
+ _filtered:function(opts,cb){
+ var service = opts.service;
+ var check = opts.check;
+ var offline = opts.offline? true:false;
+
+ var z = this;
+ z.status(service,check,function(err,list){
+ var out = [];
+ list.forEach(function(o){
+ if(o.offline === offline) online.push(o);
+ });
+ cb(false,online);
+ });
+ },
list:function(service,cb){
opts = {};
- if(typeof service == "function"){
+ if(typeof service == "function") {
cb = service;
service = undef;
}
@@ -85,8 +148,7 @@ module.exports = function(s3key,s3secret,bucket){
}
// todo list data.
- cb(err,out);
-
+ cb(err,out);
});
},
checkLimit:10,
@@ -98,45 +160,42 @@ module.exports = function(s3key,s3secret,bucket){
}
// list and delete service id files where services have not checked in in a while and or fail the check.
var z = this;
- z.list(function(err,list){
- if(err) return cb(err);
-
- var c = z.checkLimit;
- var timeout = z.staleTimeout;
+ var c = z.checkLimit;
+ var timeout = z.staleTimeout;
+
+ var todo = [];
+ list.forEach(function(o){
+ var mtime = +o.LastModified;
+ if(Date.now()-mtime > timeout){
+ o.stale = true;
+ o.offline = true;
+ } else {
+ // make sure its listening on the port.
+ todo.push(o);
+ }
+ });
- var todo = [];
- list.forEach(function(o){
- var mtime = +o.LastModified;
- if(Date.now()-mtime > timeout){
- o.stale = true;
- o.offline = true;
- } else {
- // make sure its listening on the port.
- todo.push(o);
- }
+ var active = 0, started = 0
+ , job = function fn(o){
+ ++started;
+ check(o,function(err, success){
+ active--;
+ o.listening = !!success;
+ o.offline = !success;
+ if(todo.length) fn(todo.shift());
+ else if(!active) cb(false,list);
});
+ };
- var active = 0, started = 0
- , job = function fn(o){
- ++started;
- check(o,function(err, success){
- o.listening = success;
- o.offline = !success;
- if(todo.length) fn(todo.shift());
- });
- };
-
- if(!todo.length){
- return cb(false,list);
- }
-
- while(active < c && todo.length) {
- ++active;
- job(todo.shift());
- }
+ if(!todo.length){
+ return cb(false,list);
+ }
- });
+ while(active < c && todo.length) {
+ ++active;
+ job(todo.shift());
+ }
},
clean:function(keys,cb){
client.deleteMultiple(keys,function(err,res){
@@ -165,7 +224,7 @@ module.exports.tcpCheck = function(info,cb){
s.on('error',function(e){
if(!called) {
called = true;
- cb(err);
+ cb(e);
}
});
View
17 test/test.js
@@ -5,7 +5,7 @@ var reg = require('../');
test("can foos",function(t){
- var b = "bm-service-dev";
+ var b = "fake-service-dev";
var cfg = reg.s3cfg();
r = reg(cfg.default.access_key,cfg.default.secret_key,b)
@@ -25,3 +25,18 @@ test("can foos",function(t){
});
});
+
+
+test("can status",function(t){
+
+ var b = "bm-service-dev";
+ var cfg = reg.s3cfg();
+
+ r = reg(cfg.default.access_key,cfg.default.secret_key,b)
+
+ r.status(function(err,res){
+ console.log('status?',err,res)
+ t.end();
+ })
+
+})
Please sign in to comment.
Something went wrong with that request. Please try again.