Skip to content

Commit

Permalink
refact: use cluster-client
Browse files Browse the repository at this point in the history
  • Loading branch information
shaoshuai0102 committed Jan 24, 2017
1 parent 2b57dab commit e506404
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 207 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage/
*.log
.DS_Store
run/
.vscode
34 changes: 11 additions & 23 deletions agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,26 @@
const Watcher = require('./lib/watcher');

module.exports = function(agent) {
const done = agent.readyCallback('agent_watcher');
const logger = agent.logger;
const logger = agent.coreLogger;
const config = agent.config.watcher;

agent.watcher = new Watcher(config)
.on('error', function(e) {
logger.error(e);
});

const type = config.type;
if (!type) {
throw new Error('config.watcher.type is required!');
}

const watcher = agent.watcher = agent.cluster(Watcher)
.delegate('watch', 'subscribe')
.delegate('useEventSource')
.create(config)
.on('error', e => logger.error(e));

try {
const EventSource = require('./lib/event-sources/' + type);
agent.watcher.useEventSource(new EventSource(agent));
watcher.useEventSource(new EventSource(agent));
} catch (e) {
// event source of type should be implemented
// won't throw error any way, allow other plugin implement.
}

agent.startAgent({
name: 'watcher',
client: agent.watcher,
subscribe(path, listener) {
agent.watcher.watch(path, listener);
},
});

agent.watcher.ready(() => {
logger.info('[egg:watcher:agent] watcher start success');
done();
agent.beforeStart(function* () {
yield watcher.ready();
logger.info('[watcher:agent] watcher start success');
});
};
44 changes: 14 additions & 30 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
'use strict';

module.exports = function(app) {
app.watcher = app.createAppWorkerClient('watcher', {

watch(path, listener) {
if (!path) return;

if (Array.isArray(path)) {
path.forEach(function(p) {
this.watch(p, listener);
}, this);
return;
}

this._subscribe(path, listener);
return;
},
const Watcher = require('./lib/watcher');

unwatch(path, listener) {
if (!path) return;

if (Array.isArray(path)) {
path.forEach(function(p) {
this.unwatch(p, listener);
}, this);
return;
}

this._unSubscribe(path, listener);
},

}, app.config.watcher);
module.exports = function(app) {
const logger = app.coreLogger;
const config = app.config.watcher;

const watcher = app.watcher = app.cluster(Watcher)
.delegate('watch', 'subscribe')
.create(config)
.on('error', e => logger.error(e));

app.beforeStart(function* () {
yield watcher.ready();
logger.info('[watcher:app] watcher start success');
});
};
4 changes: 1 addition & 3 deletions lib/event-sources/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ class DefaultEventSource extends Base {

constructor(agent) {
super();

this.logger = agent.logger;

this.logger = agent.coreLogger;
this.logger.warn('[egg-watcher] defaultEventSource watcher will NOT take effect');
this.ready(true);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/event-sources/development.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const Wt = require('wt');
const Base = require('sdk-base');
const fs = require('fs');
const Base = require('sdk-base');

// only used by local dev enviroment
class DevelopmentEventSource extends Base {
Expand Down
28 changes: 16 additions & 12 deletions lib/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,39 @@ const Base = require('sdk-base');
const utils = require('./utils');

module.exports = class Watcher extends Base {
constructor() {
super();

// cluster-client stops invoking any method if client is not ready
// so set ready first. The app start logic depends on eventSource
// if needed to finish initialization before app starting.
this.ready(true);
}

watch(path, callback) {
if (!path) return;

// support array
if (Array.isArray(path)) {
path.forEach(function(p) {
this.watch(p, callback);
}, this);
path.forEach(p => this.watch(p, callback));
return;
}

// skip if the path is already under watch
if (!this._events[path]) {
this._eventSource.watch(path);
}
// skip if the path is already under watching
if (this._events[path]) return;

this._eventSource.watch(path);
this.on(path, callback);
}

/*
// TODO wait unsubscribe implementation of cluster-client
unwatch(path, callback) {
if (!path) return;
// support array
if (Array.isArray(path)) {
path.forEach(function(p) {
this.unwatch(p, callback);
}, this);
path.forEach(p => this.unwatch(p, callback));
return;
}
Expand All @@ -47,6 +52,7 @@ module.exports = class Watcher extends Base {
this.removeAllListeners(path);
this._eventSource.unwatch(path);
}
*/

_onChange(info) {
const path = info.path;
Expand Down Expand Up @@ -80,8 +86,6 @@ module.exports = class Watcher extends Base {
.on('change', this._onChange.bind(this))
.on('fuzzy-change', this._onFuzzyChange.bind(this))
.on('error', this.emit.bind(this, 'error'));

source.ready(this.ready.bind(this, true));
}

};
25 changes: 12 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@
"lib"
],
"scripts": {
"test": "npm run lint && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"lint": "eslint --ext js . --fix",
"test": "egg-bin test",
"cov": "egg-bin cov",
"ci": "npm run lint && npm run cov",
"autod": "autod"
},
"dependencies": {
"sdk-base": "~1.1.0",
"wt": "~1.1.0"
"detect-port": "^1.1.0",
"sdk-base": "^3.0.1",
"wt": "^1.1.1"
},
"devDependencies": {
"autod": "2",
"autod": "^2.7.1",
"egg": "*",
"egg-bin": "1",
"egg-ci": "1",
"egg-mock": "*",
"eslint": "3",
"eslint-config-egg": "3",
"should": "^9.0.2",
"supertest": "^1.2.0"
"egg-bin": "^2.0.1",
"egg-ci": "^1.1.0",
"egg-mock": "^2.1.0",
"eslint": "^3.14.0",
"eslint-config-egg": "^3.2.0",
"supertest": "^2.0.1"
},
"homepage": "https://github.com/eggjs/egg-watcher",
"repository": {
Expand Down
Loading

0 comments on commit e506404

Please sign in to comment.