refactor: impl Application and Agent extend with Class (#65)
Summary by CodeRabbit
Release Notes
New Features
Introduced new lifecycle method
beforeCloseto manage application shutdown.Enhanced asynchronous handling in various classes, including
Schedule,BaseStrategy, andTimerStrategy.Bug Fixes
Corrected documentation in the
initmethod of theScheduleclass.Refactor
Updated class structures to use explicit class declarations instead of object exports.
Streamlined property access in several classes by transitioning to getter methods.
Chores
Updated import paths for
AgentandApplicationtypes across multiple files.
- Expanded exports in
src/index.tsto include new modules.
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802032778号
@eggjs/schedule
A schedule plugin for egg, has been built-in plugin for egg enabled by default.
It’s fully extendable for a developer and provides a simple built-in TimerStrategy.
Usage
Just add your job file to
{baseDir}/app/schedule.You can also use function simply like:
Overview
@eggjs/schedulesupports both cron-based scheduling and interval-based scheduling.Schedule decision is being made by
agentprocess.agenttriggers a task and sends a message toworkerprocess. Then, one or allworkerprocess(es) execute the task based on schedule type.To setup a schedule task, simply create a job file in
{app_root}/app/schedule. A file contains one job and exportsscheduleandtaskproperties.The rule of thumbs is one job per file.
Task
Task is a class which will be instantiated with every schedule, and a
subscribemethod will be invoked.You can get anonymous context with
this.ctx.SCHEDULE/__schedule?path=${schedulePath}&${schedule}.To create a task,
subscribecan be a generator function or async function. For example:Scheduling
scheduleis an object that contains one required property,type, and optional properties,{ cron, cronOptions, interval, immediate, disable, env }.Cron-style Scheduling
Use cron-parser.
Example:
Interval-style Scheduling
To use
setInterval, and support ms conversion styleExample:
Notice: Egg built-in TimerStrategy will schedule each execution at a fix rate, regardless of its execution time. So you have to make sure that your actual execution time of your
task/subscribemust be smaller than your delay time.Schedule Type
Build-in support is:
worker: will be executed in one random worker when a schedule runs.all: will be executed in all workers when a schedule runs.Custom schedule:
To create a custom schedule, simply extend
agent.ScheduleStrategyand register it byagent.schedule.use(type, clz). You can schedule the task to be executed by one random worker or all workers with the built-in methodthis.sendOne(...args)orthis.sendAll(...args)which support params, it will pass tosubscribe(...args)ortask(ctx, ...args).Then you could use it to defined your job:
Dynamic schedule
Configuration
Logging
See
${appInfo.root}/logs/{app_name}/egg-schedule.logwhich provided by config.customLogger.scheduleLogger.Customize directory
If you want to add additional schedule directories, you can use this config.
Testing
app.runSchedule(scheduleName)is provided by@eggjs/scheduleplugin only for test purpose.Example:
Questions & Suggestions
Please open an issue here.
License
MIT
Contributors
Made with contributors-img.