@ arkstack/ 排程器
任务时间安排 阿克斯达克. 在代码中流利地定义预定任务,而不是管理一大批cron条目——单一的系统cron条目驱动一切.
安装
pnpm add @arkstack/scheduler
ark publish --package @arkstack/scheduler # writes src/routes/console.ts
完整的应用程序模板包括它和一个 src/routes/console.ts 已经.
定义您的日程
单位 src/routes/console.ts数字 :
import { Schedule } from '@arkstack/scheduler'
Schedule.command('cache:prune').hourly()
Schedule.call(async () => {
await pruneTempFiles()
})
.dailyAt('01:30')
.withoutOverlapping()
Schedule.job(new SendDigest()).weeklyOn(1, '08:00')
Schedule.exec('backup.sh').daily().onOneServer()
| 任务类型 | 运行 |
|---|---|
Schedule.command(name, args?) | Arkstack CLI 命令 (ark <name>(中文(简体) ) |
Schedule.call(fn) | 回电 |
Schedule.job(job) | 已排队的任务( 通过 @arkstack/jobs(中文(简体) ) |
Schedule.exec(cmd, args?) | 一个 shell 命令 |
运行它
调度员每分钟评估一次到期任务. 在生产中,加上 一个 cron 条目 :
* * * * * cd /path/to/app && npx ark schedule:run >> /dev/null 2>&1
在开发期间,经营前景工人:
ark schedule:work # evaluates the schedule every minute until stopped
ark schedule:list # list tasks with their next run time
节奏
Schedule.command('x').everyMinute()
Schedule.command('x').everyFiveMinutes() // */5 * * * *
Schedule.command('x').everyThirtyMinutes()
Schedule.command('x').hourly()
Schedule.command('x').hourlyAt(15)
Schedule.command('x').daily()
Schedule.command('x').dailyAt('13:00')
Schedule.command('x').twiceDaily(1, 13)
Schedule.command('x').weekly()
Schedule.command('x').weeklyOn(1, '8:00') // Monday 08:00
Schedule.command('x').monthly()
Schedule.command('x').monthlyOn(15, '17:00')
Schedule.command('x').quarterly()
Schedule.command('x').yearly()
Schedule.command('x').cron('*/10 9-17 * * 1-5') // raw cron
每周的限制,上面有时间层:
Schedule.command('x').weekdays().at('9:00')
Schedule.command('x').weekends().hourly()
Schedule.command('x').mondays()
Schedule.command('x').days([1, 4]) // Monday & Thursday
设置评价表达式的时区 :
Schedule.command('x').dailyAt('09:00').timezone('America/New_York')
制约因素
Schedule.command('x')
.daily()
.when(async () => await featureEnabled('reports')) // run only when truthy
.skip(() => isHoliday()) // skip when truthy
.environments('production', 'staging') // limit by APP_ENV
.between('09:00', '17:00') // only within a daily window
unlessBetween('22:00', '06:00') 运行 外部 窗口( 处理过夜区域) .
重叠, 单服务器和背景
Schedule.command('report:build').everyFiveMinutes().withoutOverlapping()
Schedule.exec('backup.sh').daily().onOneServer()
Schedule.exec('long-import.sh').daily().runInBackground()
withoutOverlapping() 财务报告和审定财务报表 onOneServer() 通过协调 @arkstack/cache,因此它们可以跨进程和服务器工作. 配置共享缓存存储( N)redis 或者说 database用于 onOneServer() ; 没有缓存, 调度器会回到进程本地锁定.
钩子
Schedule.command('report:build')
.daily()
.before(() => logger.info('building report'))
.onSuccess(() => notifyOk())
.onFailure((error) => notifyFailed(error))
.after(() => logger.info('done'))
API 密码
Schedule.command / call / job / exec登记任务,返回 aScheduledEvent用来锁链.Schedule.events()编号 :Schedule.dueEvents(date?)检查已登记/应予处理的事件.ScheduledEvent——活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活活,timezone, (中文(简体) ),when, (中文(简体) ),skip, (中文(简体) ),environments, (中文(简体) ),between, (中文(简体) ),unlessBetween, (中文(简体) ),withoutOverlapping, (中文(简体) ),onOneServer, (中文(简体) ),runInBackground, (中文(简体) ),before编号 :after编号 :onSuccess编号 :onFailure, (中文(简体) ),description, (中文(简体) ),name) (中文(简体) ).- 图标 :
schedule:run, (中文(简体) ),schedule:work, (中文(简体) ),schedule:list。 。 。 .
许可证
麻省理工学院