在 app.conf
定义你的时间表,:
cron.workhours_15m = 0 */15 9-17 ? * MON-FRI
使用一个cron规范指定时间表,就可以在任何地方引用它。
func init() {
revel.OnAppStart(func() {
jobs.Schedule("cron.workhours_15m", ReminderEmails{})
})
}
注意: cron 时间表的名字必须以 “cron.”开头
临时任务
有时候在响应用户的一个操作时,还要处理一些事情。在这种情况下,模块可以在某个时间运行一个任务。
模块提供的唯一控制是等待多长时间运行任务。
type AppController struct { *revel.Controller }
func (c AppController) Action() revel.Result {
// 处理请求
...
// 立即发送电子邮件(异步)
jobs.Now(SendConfirmationEmail{})
// 或者,一分钟后发送电子邮件(异步)。
jobs.In(time.Minute, SendConfirmationEmail{})
}
注册函数
通过使用jobs.Func
类型包装一个func()
函数,来注册一个任务。例如:
func sendReminderEmails() {
// 查询数据库
// 发送电子邮件
}
func init() {
revel.OnAppStart(func() {
jobs.Schedule("@midnight", jobs.Func(sendReminderEmails))
})
}
任务状态
模块提供了一个状态页面,用来显示任务的运行状态(IDLE 或 RUNNING), 以及之前和下次运行时间。
标签:Revel
相关阅读 >>
更多相关阅读请进入《Revel》频道 >>

Go语言101
一个与时俱进的Go编程知识库。