Files
mayfly-go/server/internal/devops/infrastructure/scheudler/scheduler.go

34 lines
487 B
Go
Raw Normal View History

2020-09-01 10:34:11 +08:00
package scheduler
import (
"mayfly-go/pkg/biz"
2021-01-08 15:37:32 +08:00
2020-09-01 10:34:11 +08:00
"github.com/robfig/cron/v3"
)
2021-04-16 15:10:07 +08:00
var cronService = cron.New()
2020-09-01 10:34:11 +08:00
func Start() {
2021-04-16 15:10:07 +08:00
cronService.Start()
2020-09-01 10:34:11 +08:00
}
func Stop() {
2021-04-16 15:10:07 +08:00
cronService.Stop()
}
func Remove(id cron.EntryID) {
cronService.Remove(id)
2020-09-01 10:34:11 +08:00
}
func GetCron() *cron.Cron {
2021-04-16 15:10:07 +08:00
return cronService
2020-09-01 10:34:11 +08:00
}
func AddFun(spec string, cmd func()) cron.EntryID {
2021-04-16 15:10:07 +08:00
id, err := cronService.AddFunc(spec, cmd)
2020-09-01 10:34:11 +08:00
if err != nil {
2021-03-24 17:18:39 +08:00
panic(biz.NewBizErr("添加任务失败:" + err.Error()))
2020-09-01 10:34:11 +08:00
}
return id
}