Skip to content

Commit

Permalink
Tweaking follow behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Damon Oehlman committed Sep 17, 2012
1 parent 29a0f94 commit 9187c41
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,30 @@

The `sharedconfig` package is used to provide applications a mechanism for sharing application configuration information across a network of machines.

<a href="http://travis-ci.org/#!/DamonOehlman/sharedconfig"><img src="https://secure.travis-ci.org/DamonOehlman/sharedconfig.png" alt="Build Status"></a>

## Why?

So why would you want to use `sharedconfig` over many of the other excellent node configuration libraries. Primarily, because this configuration engine is designed to grab configuration information from a single configuration server. Additionally, the [CouchDB](http://couchdb.apache.org) `_changes` feed is used (via [nano](https://github.com/dscape/nano) and [follow](https://github.com/iriscouch/follow)) to monitor changes in the config.

Combine this with the magic of [xdiff](https://github.com/dominictarr/xdiff) and you have a really powerful little configuration service for your application. Here, let me show you:

## Getting Started

To get started with `sharedconfig` you simply need to create a new sharedconfig instance:

```js
var config = sharedconfig('http://kondoot.iriscouch.com/test-sharedconfig');
```

Once you have a config instance, you can then use a particular environment configuration. The `use` command operates asynchronously and fires a callback once the complete merged config has been loaded for the target environment.

```js
config.use('dev', function(err, data) {
console.log(data.a);
});
```

Merged config? What does that mean? Well, the merged config is the result of merging the data contained within the `default` document and the data contained within the requested environment. To get a feel for how this works in practice, why not have a look at the contents of the two documents within the test db:

<
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function SharedConfig(targetdb) {
debug('requesting environments from config endpoint');
this._db.list(function(err, info) {
if (! err) {
debug('retrieve list of config docs from db: ', info.rows);
config._environments = info.rows.map(function(doc) {
return doc.id;
});
Expand Down Expand Up @@ -96,12 +97,9 @@ SharedConfig.prototype.use = function(environment, callback) {
if (this._environments.indexOf(environment) < 0) {
return callback(new Error('Unable to use the "' + environment + '" environment'));
}

// if we have an exiting feed, then stop the feed and dereference
if (this._feed) {
this._feed.stop();
this._feed = null;
}

// release the existing configuration
this.release();

this._loadEach(targetDocs, function(err, mergedConfig) {
if (err) return callback(err);
Expand Down Expand Up @@ -138,6 +136,14 @@ SharedConfig.prototype.use = function(environment, callback) {
return this;
};

SharedConfig.prototype.release = function() {
// if we have an exiting feed, then stop the feed and dereference
if (this._feed) {
this._feed.stop();
this._feed = null;
}
};

/* "private" methods */

SharedConfig.prototype._loadEach = function(targets, callback, baseConfig) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"version": "0.1.0",
"engines": {
"node": ">= 0.4.x < 0.9.0"
"node": ">= 0.6.x < 0.9.0"
},
"dependencies": {
"debug": "*",
Expand All @@ -29,7 +29,7 @@
"url": "http://github.com/DamonOehlman/sharedconfig/issues"
},
"scripts": {
"test": "./node_modules/.bin/mocha --reporter spec --timeout 10000"
"test": "./node_modules/.bin/mocha --reporter spec --timeout 20000"
},
"contributors": [],
"optionalDependencies": {}
Expand Down
9 changes: 7 additions & 2 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('simple read tests', function() {
before(testdb.prepare);

it('should be able to connect to the config endpoint', function(done) {
config = sharedconfig('http://sidelab.iriscouch.com/sharedconfig').on('connect', done);
config = sharedconfig('http://damonoehlman.iriscouch.com/sharedconfig-test').on('connect', done);

// ensure that the config is defined
assert(config);
Expand All @@ -18,7 +18,7 @@ describe('simple read tests', function() {
});

it('can create a new config, and not wait for a connect event to continue', function() {
config = sharedconfig('http://sidelab.iriscouch.com/sharedconfig');
config = sharedconfig('http://damonoehlman.iriscouch.com/sharedconfig-test');
});

it('can retrieve the full prod config', function(done) {
Expand Down Expand Up @@ -84,4 +84,9 @@ describe('simple read tests', function() {

config.use('prod');
});

it('should be able to release a config', function() {
config.release();
assert(! config._feed);
});
});
2 changes: 1 addition & 1 deletion test/helpers/testdb.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var debug = require('debug')('sharedconfig-tests'),
nano = require('nano'),
db = nano('http://sidelab.iriscouch.com/sharedconfig'),
db = nano('http://damonoehlman.iriscouch.com/sharedconfig-test'),
async = require('async'),
xdiff = require('xdiff'),
_ = require('lodash'),
Expand Down
2 changes: 1 addition & 1 deletion test/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('follow changes tests', function() {
before(testdb.prepare);

it('should be able to connect to the config endpoint', function() {
config = sharedconfig('http://sidelab.iriscouch.com/sharedconfig');
config = sharedconfig('http://damonoehlman.iriscouch.com/sharedconfig-test');
});

it('should be able to use the development environment', function(done) {
Expand Down

0 comments on commit 9187c41

Please sign in to comment.