Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Sep 29, 2017
1 parent 256329b commit 60aeba7
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 39 deletions.
31 changes: 15 additions & 16 deletions agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,41 @@
const SCHEDULE_HANDLER = Symbol.for('egg#scheduleHandler');
const WorkerStrategy = require('./lib/strategy/worker');
const AllStrategy = require('./lib/strategy/all');
const BaseStrategy = require('./lib/strategy/base');
const Schedule = require('./lib/schedule');

module.exports = agent => {
const handlers = agent[SCHEDULE_HANDLER] = {};
agent.schedule = new Schedule(agent);
agent.ScheduleStrategy = BaseStrategy;
// register built-in strategy
agent.schedule.use('worker', WorkerStrategy);
agent.schedule.use('all', AllStrategy);

agent.beforeClose(() => {
return agent.schedule.close();
// TODO: compatible, will remove at next major
const handlers = {};
Object.defineProperty(agent, SCHEDULE_HANDLER, {
get() {
agent.deprecate('should use `agent.schedule.use()` instead of `agent[Symbol.for(\'egg#scheduleHandler\')]` to register handler.');
return handlers;
},
});

agent.messenger.once('egg-ready', () => {
agent.schedule.use('worker', WorkerStrategy);
agent.schedule.use('all', AllStrategy);
//TODO: compatible, will remove at next major
const keys = Object.keys(handlers);
if (keys.length) agent.deprecate('should use `schedule.use()` instead of `agent[Symbol.for(\'egg#scheduleHandler\')]` to register handler.');
for (const type of keys) {
agent.schedule.use(type, handler2Class(type, handlers[type]));
}
// start schedule
agent.schedule.start();
});

function handler2Class(type, fn) {
return class CustomStrategy extends BaseStrategy {
constructor(...args) {
super(...args);
this.type = type;
}
return class CustomStrategy extends agent.ScheduleStrategy {
start() {
fn(this.schedule, {
one: this.sendOne.bind(this),
all: this.sendAll.bind(this),
});
}
close() {
this.agent.logger.warn(`schedule type [${type}] stop is not implemented yet`);
}
};
}
};
22 changes: 11 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ module.exports = app => {

const start = Date.now();
task(ctx)
.then(() => true) // succeed
.catch(err => {
err.message = `[egg-schedule] ${key} excute error. ${err.message}`;
app.logger.error(err);
return false; // failed
})
.then(success => {
const rt = Date.now() - start;
const status = success ? 'succeed' : 'failed';
app.coreLogger.info(`[egg-schedule] ${key} excute ${status}, used ${rt}ms`);
});
.then(() => true) // succeed
.catch(err => {
err.message = `[egg-schedule] ${key} excute error. ${err.message}`;
app.logger.error(err);
return false; // failed
})
.then(success => {
const rt = Date.now() - start;
const status = success ? 'succeed' : 'failed';
app.coreLogger.info(`[egg-schedule] ${key} excute ${status}, used ${rt}ms`);
});
});
}
};
25 changes: 25 additions & 0 deletions app/extend/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const Strategy = require('../../lib/strategy/base');
const Schedule = require('../../lib/schedule');
const SCHEDULE = Symbol('agent#schedule');

module.exports = {
/**
* @member agent#ScheduleStrategy
*/
ScheduleStrategy: Strategy,

/**
* @member agent#schedule
*/
get schedule() {
if (!this[SCHEDULE]) {
this[SCHEDULE] = new Schedule(this);
this.beforeClose(() => {
return this[SCHEDULE].close();
});
}
return this[SCHEDULE];
},
};
8 changes: 4 additions & 4 deletions lib/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ module.exports = class Schedule {
this.agent = agent;
this[STRATEGY] = new Map();
this[HANDLER] = new Map();
this.closed = false;
}

use(type, clz) {
assert(typeof clz.prototype.start === 'function', `schedule type [${type}] should implement \`start\` method`)
clz.prototype.type = type;
assert(typeof clz.prototype.start === 'function', `schedule type [${type}] should implement \`start\` method`);
this[STRATEGY].set(type, clz);
}

Expand Down Expand Up @@ -42,9 +42,9 @@ module.exports = class Schedule {

close() {
this.closed = true;
for (const handler of this[HANDLER].values()) {
for (const [ key, handler ] of this[HANDLER].entries()) {
this.agent.coreLogger.info(`[egg-schedule] close tasks: ${key}`);
handler.close();
}
this.agent.coreLogger.info('[egg-schedule] close tasks.');
}
};
6 changes: 0 additions & 6 deletions lib/strategy/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,4 @@ module.exports = class BaseStrategy {
this.agent.coreLogger.info(`[egg-schedule] send message to all worker: ${this.key}`);
this.agent.messenger.send(this.key);
}

/* istanbul ignore next */
close() {
/* istanbul ignore next */
this.agent.logger.warn(`schedule type [${this.type}] stop is not implemented yet`);
}
};
2 changes: 1 addition & 1 deletion lib/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ class Timer {
}
}

module.exports = Timer;
module.exports = Timer;
1 change: 0 additions & 1 deletion test/schedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ describe('test/schedule.test.js', () => {
yield app.ready();
yield sleep(5000);
const log = getLogContent('customType');
const errorLog = getErrorLogContent('customType');
// console.log(log);
assert(contains(log, 'custom_log') === 1);
assert(contains(log, 'cluster_log') === 1);
Expand Down

0 comments on commit 60aeba7

Please sign in to comment.