Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
Charlie Crane edited this page Oct 31, 2013 · 5 revisions

In pomelo 0.6, we created the plugin mechanism for convenience of developers to extend pomelo as their own demands.

Structure

The plugin mainly contains two parts: components and events. The components is necessary while the events is optional according to self demands. The structure is as follow diagram:

plugin

Components is the origin components in pomelo, which can refer to pomelo component.In Pomelo, a component is a resusable service unit. A component instance provides some kind of service. A plugin can have several components, and developers can implement different life cycle callbacks, including start, afterStart, stop.

Developers can deal with events in pomelo using events in plugin. The events include add_servers, remove_servers, replace_servers, bind_session, close_session.

  • add_servers:add server event,parameter is server information, parameter type is array.
  • remove_servers: remove server event,parameter is server information,parameter type is array.
  • replace_servers: servers disconnect from intranet(not master) and reconnect event, parameter is servers information which do not disconnect from intranet, parameter type is array.
  • bind_session: bind session event, parameter is session object.
  • close_session: close session event(including connection close and error), parameter is session object.

How to use

###API

####app.use(plugin, opts) use plugin in pomelo ####Arguments

  • plugin - plugin that uses in pomelo
  • opts - attach parameters

In pomelo only need to configure in app.js, the example code is as follow:

javascript var statusPlugin = require('pomelo-status-plugin');

app.use(statusPlugin, { status:{ host: '127.0.0.1', port: 6379 } });


##How to build

First, you need to create a plugin project, the directories is as follow diagram:

![plugin-dir](http://pomelo.netease.com/resource/documentImage/plugin-dir.png)

Second, you need to configure the components and events in index.js, and the example code is as follow:

```javascript```
module.exports = {
 components: __dirname + '/lib/components/',
 events: __dirname + '/lib/events/'
};

At last, you can add your components and events in plugin. For components, you need to export its constructor, the example code is as follow:

javascript module.exports = function(app, opts) { return new Component(app, opts); };

var Component = function(app, opts) { //do construction };

Component.prototype.start = function(cb) { // do something application start };

Component.prototype.afterStart = function(cb) { // do something after application started };

Component.prototype.afterStart = function(cb) { // do something on application stop };


For events, it is the same as components, the example code is as follow:

```javascript```
module.exports = function(app) {
 return new Event(app, opts);
};

var Event = function(app) {
 //do construction
};

Event.prototype.add_servers = function(servers) {
 //do something when application add servers
};

Event.prototype.remove_servers = function(ids) {
 //do something when application remove servers
};

Event.prototype.replace_servers = function(servers) {
 //do something when server reconnected
};

Event.prototype.bind_session = function(session) {
 //do something when session binded
};

Event.prototype.close_session = function(session) {
 //do something when session closed
};
Clone this wiki locally