mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 07:50:25 +08:00
65 lines
1.0 KiB
Go
65 lines
1.0 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"mayfly-go/pkg/biz"
|
|
"mayfly-go/pkg/global"
|
|
"sync"
|
|
|
|
"github.com/robfig/cron/v3"
|
|
)
|
|
|
|
func init() {
|
|
Start()
|
|
}
|
|
|
|
var (
|
|
cronService = cron.New()
|
|
key2IdMap sync.Map
|
|
)
|
|
|
|
func Start() {
|
|
cronService.Start()
|
|
}
|
|
|
|
func Stop() {
|
|
cronService.Stop()
|
|
}
|
|
|
|
// 根据任务id移除
|
|
func Remove(id cron.EntryID) {
|
|
cronService.Remove(id)
|
|
}
|
|
|
|
// 根据任务key移除
|
|
func RemoveByKey(key string) {
|
|
global.Log.Debugf("移除cron任务 => [key = %s]", key)
|
|
id, ok := key2IdMap.Load(key)
|
|
if ok {
|
|
Remove(id.(cron.EntryID))
|
|
key2IdMap.Delete(key)
|
|
}
|
|
}
|
|
|
|
func GetCron() *cron.Cron {
|
|
return cronService
|
|
}
|
|
|
|
// 添加任务
|
|
func AddFun(spec string, cmd func()) cron.EntryID {
|
|
id, err := cronService.AddFunc(spec, cmd)
|
|
biz.ErrIsNilAppendErr(err, "添加任务失败: %s")
|
|
return id
|
|
}
|
|
|
|
// 根据key添加定时任务
|
|
func AddFunByKey(key, spec string, cmd func()) {
|
|
global.Log.Debugf("添加cron任务 => [key = %s]", key)
|
|
RemoveByKey(key)
|
|
key2IdMap.Store(key, AddFun(spec, cmd))
|
|
}
|
|
|
|
func ExistKey(key string) bool {
|
|
_, ok := key2IdMap.Load(key)
|
|
return ok
|
|
}
|