-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: agent startup become compatible between egg&midway
- Loading branch information
1 parent
6a94e01
commit 47f46c3
Showing
6 changed files
with
112 additions
and
10 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
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]; | ||
}, | ||
}; |
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,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(); | ||
} | ||
} | ||
}; |
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