main.go:
package main
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/toolbox"
"traceability_management/utils"
)
func main() {
// 定时任务
utils.InitTask()
toolbox.StartTask()
defer toolbox.StopTask()
beego.Run()
}
Task.go:
package utils
import (
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/toolbox"
"time"
"traceability_management/models"
)
func InitTask(){
tk := toolbox.NewTask("generateWarning", "0 0 0 * * *", GenerateWarning)
//err := tk.Run()
//if err != nil {
// fmt.Println(err)
//}
toolbox.AddTask("generateWarning",tk)
}
func GenerateWarning() error {
// 业务
return err
}
https://github.com/astaxie/beego/issues/602
https://beego.me/docs/module/toolbox.md#task (beego文档略坑)