-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first pass * updating docs and eslint error * deprecation warning
- Loading branch information
Showing
17 changed files
with
226 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Event Bus | ||
|
||
As of Amphora version `6.6.0` the option of using [Redis as an event bus](https://redis.io/topics/pubsub) has been introduced. This event bus is intended to make it easier to a destructure a Clay instance and supporting platform packages (i.e. Amphora Search). By default the Bus module is not instantiated. Only by setting the [Redis Bus Host env var](#redis-bus-host) will the Bus module be active. | ||
|
||
The Event Bus is also intended to replace the plugin system in the next major version of Amphora (v7). On top of replacing the plugin system, the event bus will see some small changes to the payload of certain events as the plugin system is rearchitected. The end goal is to expose specific hooks in the Amphora lifecycle to the Bus as quickly as possible. | ||
|
||
## Bus Topics | ||
|
||
The following topics are published to the bus by Amphora: | ||
|
||
- `clay:publishLayout` | ||
- `clay:publishPage` | ||
- `clay:createPage` | ||
- `clay:unschedulePage` | ||
- `clay:schedulePage` | ||
- `clay:unpublishPage` | ||
- `clay:save` | ||
- `clay:delete` | ||
|
||
## Configuring the Bus | ||
|
||
The Bus module has two configurations options which are both controlled by environment variables. | ||
|
||
### Redis Bus Host | ||
|
||
As mentioned, the Bus module is turned off by default. Only by setting the `REDIS_BUS_HOST` env var to a valid Redis url (`redis://<HOST>:<PORT>`) will the module be instantiated and events will be published to the instance. | ||
|
||
### Namespace | ||
|
||
By default, all topics published to the Bus are namespaced using `clay`, i.e. `clay:<ACTION>`. This namespace can be configured by the env var `CLAY_BUS_NAMESPACE`. For example, setting `CLAY_BUS_NAMESPACE` to a value of `mysite` will publish all events as `mysite:<ACTION>`. | ||
|
||
## Subscribing To The Bus | ||
|
||
Provided you have setup Amphora to pub to a Redis instance, the following code will subscribe to all events from Clay using the [`redis`](https://www.npmjs.com/package/redis) NPM module. | ||
|
||
```javascript | ||
'use strict'; | ||
|
||
var redis = require('redis'), | ||
SUBSCRIBER = redis.createClient(process.env.REDIS_BUS_HOST), | ||
CLAY_TOPICS = [ | ||
'publishLayout', | ||
'publishPage', | ||
'unpublishPage', | ||
'createPage', | ||
'schedulePage', | ||
'unschedulePage', | ||
'save', | ||
'delete' | ||
]; | ||
|
||
for (let i = 0; i < CLAY_TOPICS.length; i++) { | ||
SUBSCRIBER.subscribe(`clay:${CLAY_TOPICS[i]}`); | ||
} | ||
|
||
SUBSCRIBER.on('message', (channel, payload) => { | ||
console.log(`Channel: ${channel}\n\n\n${payload}`); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const redis = require('redis'), | ||
NAMESPACE = process.env.CLAY_BUS_NAMESPACE || 'clay'; | ||
|
||
/** | ||
* Connect to the bus Redis instance | ||
*/ | ||
function connect() { | ||
module.exports.client = redis.createClient(process.env.REDIS_BUS_HOST); | ||
} | ||
|
||
/** | ||
* Publish a message to the bus. | ||
* | ||
* @param {String} topic | ||
* @param {String} msg | ||
*/ | ||
function publish(topic, msg) { | ||
if (!topic || !msg || typeof topic !== 'string' || typeof msg !== 'string') { | ||
throw new Error('A `topic` and `msg` property must be defined and both must be strings'); | ||
} | ||
|
||
if (module.exports.client) { | ||
module.exports.client.publish(`${NAMESPACE}:${topic}`, msg); | ||
} | ||
} | ||
|
||
module.exports.connect = connect; | ||
module.exports.publish = publish; | ||
|
||
// For testing | ||
module.exports.client = undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'), | ||
filename = __filename.split('/').pop().split('.').shift(), | ||
lib = require('./' + filename), | ||
sinon = require('sinon'), | ||
expect = require('chai').expect, | ||
redis = require('redis'); | ||
|
||
describe(_.startCase(filename), function () { | ||
var sandbox, publish; | ||
|
||
beforeEach(function () { | ||
sandbox = sinon.sandbox.create(); | ||
sandbox.stub(redis); | ||
publish = sandbox.stub(); | ||
lib.client = { publish }; | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('connect', function () { | ||
const fn = lib[this.title]; | ||
|
||
it('calls the `createClient` method for Redis', function () { | ||
fn(); | ||
sinon.assert.calledOnce(redis.createClient); | ||
}); | ||
}); | ||
|
||
describe('publish', function () { | ||
const fn = lib[this.title]; | ||
|
||
it('throws an error if topic and message are not defined', function () { | ||
expect(fn).to.throw(); | ||
}); | ||
|
||
it('throws an error if topic is not a string', function () { | ||
expect(() => fn(1, 'foo')).to.throw(); | ||
}); | ||
|
||
it('throws an error if message is not defined', function () { | ||
expect(() => fn('foo', 1)).to.throw(); | ||
}); | ||
|
||
it('calls the publish function if topic and string pass validation', function () { | ||
fn('foo', 'bar'); | ||
sinon.assert.calledOnce(publish); | ||
}); | ||
|
||
it('does not call publish if no client is defined', function () { | ||
lib.client = false; | ||
fn('foo', 'bar'); | ||
sinon.assert.notCalled(publish); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.