Skip to content

Commit

Permalink
fix: agent startup become compatible between egg&midway
Browse files Browse the repository at this point in the history
  • Loading branch information
Lellansin authored and czy88840616 committed Nov 15, 2018
1 parent 6a94e01 commit 47f46c3
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 10 deletions.
6 changes: 2 additions & 4 deletions packages/midway-schedule/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ const wrapped = require('egg-schedule/agent');

module.exports = (agent) => {
if (!agent.loggers.scheduleLogger) {
agent.loggers.scheduleLogger = {
unredirect: () => {},
warn: console.warn,
};
agent.loggers.scheduleLogger = console;
agent.loggers.scheduleLogger.unredirect = () => {};
}
wrapped(agent);
};
10 changes: 6 additions & 4 deletions packages/midway-schedule/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import * as qs from 'querystring';
import * as path from 'path';
import loadSchedule from './lib/load_schedule';

const loadEggSchedule = require('egg-schedule/lib/load_schedule');

module.exports = (app) => {
// don't redirect scheduleLogger
// app.loggers.scheduleLogger.unredirect('error');

const schedules = loadSchedule(app);
// 'app/schedule' load egg-schedule (spec for egg-logxx rotate)
const schedules = loadEggSchedule(app);
// 'lib/schedule' load midway-schedule (class only with decorator support)
loadSchedule(app);

// for test purpose
app.runSchedule = (schedulePath) => {
Expand Down Expand Up @@ -52,7 +57,6 @@ module.exports = (app) => {

// register schedule event
app.messenger.on('egg-schedule', (data) => {
throw new Error('hahaha');
const id = data.id;
const key = data.key;
const schedule = schedules[key];
Expand All @@ -75,8 +79,6 @@ module.exports = (app) => {
const start = Date.now();
const task = schedule.task;
logger.info(`[${id}] ${key} executing by app`);
console.log('hi ctx', ctx);
console.log('hi ctx.logger', ctx.logger);
// execute
task(ctx, ...data.args)
.then(() => true) // succeed
Expand Down
42 changes: 41 additions & 1 deletion packages/midway-schedule/app/extend/agent.ts
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
module.exports = require('egg-schedule/app/extend/agent');
'use strict';

const Strategy = require('egg-schedule/lib/strategy/base');
const Timer = require('egg-schedule/lib/timer');
const Schedule = require('../../lib/schedule');

const SCHEDULE = Symbol('agent#schedule');
const TIMER = Symbol('agent#scheduleTimer');

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];
},

/**
* @member agent#scheduleTimer
*/
get scheduleTimer() {
if (!this[TIMER]) {
this[TIMER] = new Timer(this);
this.beforeClose(() => {
return this[TIMER].close();
});
}
return this[TIMER];
},
};
2 changes: 1 addition & 1 deletion packages/midway-schedule/lib/load_schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default (app) => {
.getLoadUnits()
.map((unit) => require('path').join(unit.path, 'lib/schedule'));
const Loader = getScheduleLoader(app);
const schedules = (app.schedules = {});
const schedules = app.schedules;
new Loader({
directory: dirs,
target: schedules,
Expand Down
61 changes: 61 additions & 0 deletions packages/midway-schedule/lib/schedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

import loadSchedule from './load_schedule';
const BaseSchedule = require('egg-schedule/lib/schedule');

const M_STRATEGY: string = Symbol('strategy') as any;
const M_STRATEGY_INSTANCE: string = Symbol('strategy_instance') as any;

module.exports = class Schedule extends BaseSchedule {
constructor(agent) {
super(agent);
this[M_STRATEGY] = new Map();
this[M_STRATEGY_INSTANCE] = new Map();
}

/**
* register a custom Schedule Strategy
* @param {String} type - strategy type
* @param {Strategy} clz - Strategy class
*/
use(type, clz) {
super.use(type, clz);
this[M_STRATEGY].set(type, clz);
}

init() {
// load egg-schedule (inside 'app/schedule')
super.init();
// load midway-schedule (inside 'lib/schedule')
this.loadSchedule();
}

loadSchedule() {
const scheduleItems = loadSchedule(this.agent);

for (const k of Object.keys(scheduleItems)) {
const { key, schedule } = scheduleItems[k];
const type = schedule.type;
if (schedule.disable) continue;

const Strategy = this[M_STRATEGY].get(type);
if (!Strategy) {
const err = new Error(`schedule type [${type}] is not defined`);
err.name = 'EggScheduleError';
throw err;
}

const instance = new Strategy(schedule, this.agent, key);
this[M_STRATEGY_INSTANCE].set(key, instance);
}
}

start() {
// start egg-schedule
super.start();
// start midway-schedule
for (const instance of this[M_STRATEGY_INSTANCE].values()) {
instance.start();
}
}
};
1 change: 1 addition & 0 deletions packages/midway-schedule/test/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('test/schedule.test.js', () => {
// app.debug();
await app.ready();
console.log(app.schedules);
await sleep(5000);
});
});
});
Expand Down

0 comments on commit 47f46c3

Please sign in to comment.