May 15, 2021 Revel
Revel supports scheduled tasks (asynchronous execution) that run outside the request process. For example, periodic tasks to update cached data, or temporary tasks to send e-mail messages.
The framework is an optional module and is disabled by default. To activate it, you need to add the module to the profile:
module.jobs = github.com/revel/revel/modules/jobs
In addition, in order to access the monitoring page of the scheduled task, you need to add the following to the routing file:
module:jobs
This statement inserts
/@jobs
route
There are two options to limit scheduled tasks.
This example shows their default values.
jobs.pool = 10 # 允许同时运行的任务数
jobs.selfconcurrent = false # 一个任务只允许一个实例
When the application starts,
revel.OnAppStart
registers a function to run a task. R
evel starts these tasks continuously before the service starts.
Note that this feature does not actually use the Scheduled Tasks module, which is used to submit tasks, but does not prevent the server from starting.
func init() {
revel.OnAppStart(func() { jobs.Now(populateCache{}) })
}
Tasks can be specified to run at any time. There are two options for using a schedule:
Revel uses cron library to parse schedules and tasks. The description of the cron library provides a detailed description of the time format.
Scheduled tasks usually
revel.OnAppStart
hook is registered, but can also be registered at any time in the future.
Here are some examples:
import (
"github.com/revel/revel"
"github.com/revel/revel/modules/jobs/app/jobs"
"time"
)
type ReminderEmails struct {
// 过滤
}
func (e ReminderEmails) Run() {
// 查询数据库
// 发送电子邮件
}
func init() {
revel.OnAppStart(func() {
jobs.Schedule("0 0 0 * * ?", ReminderEmails{})
jobs.Schedule("@midnight", ReminderEmails{})
jobs.Schedule("@every 24h", ReminderEmails{})
jobs.Every(24 * time.Hour, ReminderEmails{})
})
}
You can configure the schedule in the
app.conf
file and reference it anywhere.
This provides easy-to-reuse and useful instructions for the crontab format.
Define
app.conf
cron.workhours_15m = 0 */15 9-17 ? * MON-FRI
By specifying a timesheet using a cron specification, you can reference it anywhere.
func init() {
revel.OnAppStart(func() {
jobs.Schedule("cron.workhours_15m", ReminderEmails{})
})
}
Note: The name of the cron schedule must be "cron." Beginning
Sometimes something has to be handled in response to an action by the user. In this case, the module can run a task at some point.
The only control provided by the module is how long to wait for the task to run.
type AppController struct { *revel.Controller }
func (c AppController) Action() revel.Result {
// 处理请求
...
// 立即发送电子邮件(异步)
jobs.Now(SendConfirmationEmail{})
// 或者,一分钟后发送电子邮件(异步)。
jobs.In(time.Minute, SendConfirmationEmail{})
}
By
jobs.Func
type wraps
func()
function to register a task.
For example:
func sendReminderEmails() {
// 查询数据库
// 发送电子邮件
}
func init() {
revel.OnAppStart(func() {
jobs.Schedule("@midnight", jobs.Func(sendReminderEmails))
})
}
The module provides a status page that shows the running status (IDLE or RUNNING) of the task, as well as the previous and next run times.