@eggjs/ 计划
彩蛋的调度插件, 默认启用了彩蛋的内置插件 .
它对于开发者来说是完全可扩展的,并且提供了简单的内置定时器战略.
使用量
只要将您的工作文件添加到 {baseDir}/app/schedule。 。 。 .
// {baseDir}/app/schedule/cleandb.ts
import { Subscription } from 'egg';
export default class CleanDB extends Subscription {
/**
* @property {Object} schedule
* - {String} type - schedule type, `worker` or `all` or your custom types.
* - {String} [cron] - cron expression, see [below](#cron-style-scheduling)
* - {Object} [cronOptions] - cron options, see [cron-parser#options](https://github.com/harrisiirak/cron-parser#options)
* - {String | Number} [interval] - interval expression in millisecond or express explicitly like '1h'. see [below](#interval-style-scheduling)
* - {Boolean} [immediate] - To run a scheduler at startup
* - {Boolean} [disable] - whether to disable a scheduler, usually use in dynamic schedule
* - {Array} [env] - only enable scheduler when match env list
*/
static get schedule() {
return {
type: 'worker',
cron: '0 0 3 * * *',
// interval: '1h',
// immediate: true,
};
}
async subscribe() {
await this.ctx.service.db.cleandb();
}
}
您也可以简单地使用诸如:
import { EggContext } from 'egg';
export const schedule = {
type: 'worker',
cron: '0 0 3 * * *',
// interval: '1h',
// immediate: true,
};
export async function task(ctx: EggContext) {
await ctx.service.db.cleandb();
}
概览
@eggjs/schedule 支持基于克龙的调度和基于间隔的调度.
附表决定由 agent 进程. agent 触发任务并向 worker 进程。 那么,一个或所有 worker 进程根据调度类型执行任务.
要设置调度任务, 只需在 {app_root}/app/schedule。一个文件包含一个任务和导出 schedule 财务报告和审定财务报表 task 属性.
拇指的规则是每个文件一个工作.
任务
任务是一个类,将随每个时间表即时进行 subscribe 将引用方法.
您可以获取匿名背景 this.ctx。 。 。 .
- ctx.方法:
SCHEDULE - ctx.path: (英语)
/__schedule?path=${schedulePath}&${schedule}。 。 。 .
为了创造任务, subscribe 可以是生成器函数或合成函数。 例如:
// A simple logger example
import { Subscription } from 'egg';
export default class LoggerExample extends Subscription {
async subscribe() {
this.ctx.logger.info('Info about your task');
}
}
// A real world example: wipe out your database.
// Use it with caution. :)
import { Subscription } from 'egg';
export default class CleanDB extends Subscription {
async subscribe() {
await this.ctx.service.db.cleandb();
}
}
时间安排
schedule 是一个包含一个所需属性的对象, type,和可选属性, { cron, cronOptions, interval, immediate, disable, env }。 。 。 .
重排时序
使用 剪贴板。 。 。 .
说明:
cron-parser支助second作为不为 linux crontab 支持的可选.
@hourly / @daily / @weekly / @monthly / @yearly也得到支持.
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, optional)
示例
// To execute task every 3 hours
export const schedule = {
type: 'worker',
cron: '0 0 */3 * * *',
cronOptions: {
// tz: 'Europe/Athens',
},
};
中间样式排程
用于 setInterval,并提供支持 分钟 转换样式
示例
// To execute task every 3 hours
export const schedule = {
type: 'worker',
interval: '3h',
};
通知: Egg内置定时器战略将按固定速度安排每次执行,无论执行时间如何. 所以,你必须保证你的实际执行时间 task/subscribe 必须小于您的延迟时间 .
计划类型
内部支持是:
worker: 当一个调度运行时, 将用一个随机工执行 .all: 在所有工人中执行时间表.
自定义时间表 :
要创建自定义时间表,只需扩展 agent.ScheduleStrategy 并登记到 agent.schedule.use(type, clz)。 。 。 .
您可以安排一个随机工人或所有工人执行的任务
内置方法 this.sendOne(...args) 或者说 this.sendAll(...args) 以物配主者,
它会传递给 subscribe(...args) 或者说 task(ctx, ...args)。 。 。 .
// {baseDir}/agent.ts
import { Agent } from 'egg';
export default (agent: Agent) => {
class CustomStrategy extends agent.ScheduleStrategy {
start() {
// such as mq / redis subscribe
agent.notify.subscribe('remote_task', (data) => {
this.sendOne(data);
});
}
}
agent.schedule.use('custom', CustomStrategy);
};
那你可以用它来定义你的工作:
// {baseDir}/app/schedule/other.ts
import { Subscription } from 'egg';
export default class ClusterTask extends Subscription {
static get schedule() {
return {
type: 'custom',
};
}
async subscribe(data) {
console.log('got custom data:', data);
await this.ctx.service.someTask.run();
}
}
动态时间表
// {baseDir}/app/schedule/sync.ts
import { Application } from 'egg';
export default (app: Application) => {
class SyncTask extends app.Subscription {
static get schedule() {
return {
interval: 10000,
type: 'worker',
// only start task when hostname match
disable: require('os').hostname() !== app.config.sync.hostname,
// only start task at prod mode
env: ['prod'],
};
}
async subscribe() {
await this.ctx.sync();
}
}
return SyncTask;
};
配置
日志
见 ${appInfo.root}/logs/{app_name}/egg-schedule.log 规定 配置、 海关。 。 。 .
// config/config.default.ts
import { EggAppConfig } from 'egg';
export default {
customLogger: {
scheduleLogger: {
// consoleLevel: 'NONE',
// file: path.join(appInfo.root, 'logs', appInfo.name, 'egg-schedule.log'),
},
},
} as Partial<EggAppConfig>;
自定义目录
如果您想要添加额外的调度目录, 您可以使用此配置 .
// config/config.default.ts
import { EggAppConfig } from 'egg';
export default {
schedule: {
directory: ['path/to/otherSchedule'],
},
} as Partial<EggAppConfig>;
测试
app.runSchedule(scheduleName) 由下列机构提供: @eggjs/schedule 仅用于测试目的的插件 .
示例
it('test a schedule task', async () => {
// get app instance
await app.runSchedule('clean_cache');
});
问题和建议
请打开一个问题 这里。 。 。 .
许可证
贡献者
制造的 贡献者-img。 。 。 .