-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: classify #23
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ npm-debug.log | |
node_modules/ | ||
coverage/ | ||
run/ | ||
.vscode |
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 |
---|---|---|
|
@@ -2,7 +2,6 @@ sudo: false | |
language: node_js | ||
node_js: | ||
- '6' | ||
- '7' | ||
- '8' | ||
install: | ||
- npm i npminstall && npminstall | ||
|
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,121 +1,42 @@ | ||
'use strict'; | ||
|
||
const loadSchedule = require('./lib/load_schedule'); | ||
const parser = require('cron-parser'); | ||
const ms = require('humanize-ms'); | ||
const safetimers = require('safe-timers'); | ||
const SCHEDULE_HANDLER = Symbol.for('egg#scheduleHandler'); | ||
const WorkerStrategy = require('./lib/strategy/worker'); | ||
const AllStrategy = require('./lib/strategy/all'); | ||
|
||
module.exports = agent => { | ||
// add handler into `agent[SCHEDULE_HANDLER]` for extend other kind of schedule type. | ||
// worker: will excute in one random worker when schedule excuted. | ||
// all: will excute in all workers when schedule excuted. | ||
const handlers = agent[SCHEDULE_HANDLER] = { | ||
worker: workerHandler, | ||
all: allHander, | ||
}; | ||
|
||
agent.messenger.once('egg-ready', startSchedule); | ||
|
||
function startSchedule() { | ||
agent.disableSchedule = false; | ||
const schedules = loadSchedule(agent); | ||
for (const s in schedules) { | ||
const schedule = schedules[s]; | ||
if (schedule.schedule.disable) continue; | ||
|
||
const type = schedule.schedule.type; | ||
const handler = handlers[type]; | ||
if (!handler) { | ||
const err = new Error(`schedule type [${type}] is not defined`); | ||
err.name = 'EggScheduleError'; | ||
throw err; | ||
} | ||
handler(schedule.schedule, { | ||
one() { | ||
sendMessage(agent, 'sendRandom', schedule.key); | ||
}, | ||
all() { | ||
sendMessage(agent, 'send', schedule.key); | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
agent.on('close', () => { | ||
agent.disableSchedule = true; | ||
return; | ||
// register built-in strategy | ||
agent.schedule.use('worker', WorkerStrategy); | ||
agent.schedule.use('all', AllStrategy); | ||
|
||
// 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; | ||
}, | ||
}); | ||
}; | ||
|
||
function sendMessage(agent, method, key) { | ||
if (agent.disableSchedule) { | ||
agent.coreLogger.info(`[egg-schedule] message ${key} did not sent`); | ||
return; | ||
} | ||
agent.coreLogger.info(`[egg-schedule] send message: ${method} ${key}`); | ||
agent.messenger[method](key); | ||
} | ||
|
||
function workerHandler(schedule, sender) { | ||
baseHander(schedule, sender.one); | ||
} | ||
|
||
function allHander(schedule, sender) { | ||
baseHander(schedule, sender.all); | ||
} | ||
|
||
function baseHander(schedule, send) { | ||
if (!schedule.interval && !schedule.cron) { | ||
throw new Error('[egg-schedule] schedule.interval or schedule.cron must be present'); | ||
} | ||
|
||
if (schedule.interval) { | ||
const interval = ms(schedule.interval); | ||
safeInterval(send, interval); | ||
} | ||
|
||
if (schedule.cron) { | ||
let interval; | ||
try { | ||
interval = parser.parseExpression(schedule.cron); | ||
} catch (err) { | ||
err.message = `[egg-schedule] parse cron instruction(${schedule.cron}) error: ${err.message}`; | ||
throw err; | ||
agent.messenger.once('egg-ready', () => { | ||
// wait for other plugin to register custom strategy | ||
const keys = Object.keys(handlers); | ||
for (const type of keys) { | ||
agent.schedule.use(type, handler2Class(type, handlers[type])); | ||
} | ||
startCron(interval, send); | ||
} | ||
|
||
if (schedule.immediate) { | ||
setImmediate(send); | ||
} | ||
} | ||
|
||
function startCron(interval, listener) { | ||
const now = Date.now(); | ||
let nextTick; | ||
do { | ||
nextTick = interval.next().getTime(); | ||
} while (now >= nextTick); | ||
|
||
safeTimeout(() => { | ||
listener(); | ||
startCron(interval, listener); | ||
}, nextTick - now); | ||
} | ||
|
||
function safeTimeout(fn, delay, ...args) { | ||
if (delay < safetimers.maxInterval) { | ||
setTimeout(fn, delay, ...args); | ||
} else { | ||
safetimers.setTimeout(fn, delay, ...args); | ||
} | ||
} | ||
// start schedule after worker ready | ||
agent.schedule.start(); | ||
}); | ||
|
||
function safeInterval(fn, delay, ...args) { | ||
if (delay < safetimers.maxInterval) { | ||
setInterval(fn, delay, ...args); | ||
} else { | ||
safetimers.setInterval(fn, delay, ...args); | ||
function handler2Class(type, fn) { | ||
return class CustomStrategy extends agent.ScheduleStrategy { | ||
constructor(...args) { | ||
super(...args); | ||
fn(this.schedule, { | ||
one: this.sendOne.bind(this), | ||
all: this.sendAll.bind(this), | ||
}); | ||
} | ||
}; | ||
} | ||
} | ||
}; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加一下注释,需要等插件注册 strategy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加了,在下一行