2017-03-14 06:31:46 +00:00
|
|
|
|
package crontask
|
2017-03-10 09:08:51 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/robfig/cron"
|
|
|
|
|
"errors"
|
2017-03-13 09:21:21 +00:00
|
|
|
|
"sync"
|
2017-03-24 05:06:53 +00:00
|
|
|
|
"strings"
|
2017-03-10 09:08:51 +00:00
|
|
|
|
)
|
|
|
|
|
|
2017-03-14 06:31:46 +00:00
|
|
|
|
var DefaultCronTask *CronTask
|
2017-03-10 09:08:51 +00:00
|
|
|
|
|
2017-03-24 05:06:53 +00:00
|
|
|
|
type CronMap map[string]*cron.Cron
|
|
|
|
|
|
2017-03-10 09:08:51 +00:00
|
|
|
|
type CronTask struct {
|
2017-03-13 09:21:21 +00:00
|
|
|
|
sync.RWMutex
|
2017-03-24 05:06:53 +00:00
|
|
|
|
tasks CronMap
|
2017-03-10 09:08:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 05:31:16 +00:00
|
|
|
|
func NewCronTask() *CronTask {
|
2017-03-14 06:31:46 +00:00
|
|
|
|
return &CronTask {
|
|
|
|
|
sync.RWMutex{},
|
2017-03-24 05:06:53 +00:00
|
|
|
|
make(CronMap),
|
2017-03-10 10:08:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 09:08:51 +00:00
|
|
|
|
// 新增定时任务,如果name存在,则添加失败
|
2017-03-24 05:06:53 +00:00
|
|
|
|
// name 任务名称
|
|
|
|
|
// spec crontab时间格式定义 可定义多个时间\n分隔
|
|
|
|
|
func(cronTask *CronTask) Add(name string, spec string, cmd cron.FuncJob ) (err error) {
|
2017-03-10 09:08:51 +00:00
|
|
|
|
if name == "" || spec == "" || cmd == nil {
|
|
|
|
|
return errors.New("参数不完整")
|
|
|
|
|
}
|
|
|
|
|
if cronTask.IsExist(name) {
|
|
|
|
|
return errors.New("任务已存在")
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 09:55:44 +00:00
|
|
|
|
spec = strings.TrimSpace(spec)
|
2017-03-13 09:21:21 +00:00
|
|
|
|
cronTask.Lock()
|
|
|
|
|
defer cronTask.Unlock()
|
2017-03-10 09:08:51 +00:00
|
|
|
|
cronTask.tasks[name] = cron.New()
|
2017-03-24 09:55:44 +00:00
|
|
|
|
specs := strings.Split(spec, "|||")
|
2017-03-24 05:06:53 +00:00
|
|
|
|
for _, item := range(specs) {
|
|
|
|
|
_, err = cron.Parse(item)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, item := range(specs) {
|
|
|
|
|
err = cronTask.tasks[name].AddFunc(item, cmd)
|
|
|
|
|
}
|
2017-04-01 12:28:30 +00:00
|
|
|
|
cronTask.tasks[name].Start()
|
2017-03-10 09:08:51 +00:00
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 05:06:53 +00:00
|
|
|
|
// 任务不存在则新增,任务已存在则删除后新增
|
|
|
|
|
func(cronTask *CronTask) AddOrReplace(name string, spec string, cmd cron.FuncJob) error {
|
2017-03-10 09:08:51 +00:00
|
|
|
|
if cronTask.IsExist(name) {
|
|
|
|
|
cronTask.Delete(name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cronTask.Add(name, spec, cmd)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断任务是否存在
|
|
|
|
|
func(cronTask *CronTask) IsExist(name string) bool {
|
2017-03-13 09:21:21 +00:00
|
|
|
|
cronTask.RLock()
|
|
|
|
|
defer cronTask.RUnlock()
|
2017-03-10 09:08:51 +00:00
|
|
|
|
_, ok := cronTask.tasks[name]
|
|
|
|
|
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 停止任务
|
|
|
|
|
func(cronTask *CronTask) Stop(name string) {
|
|
|
|
|
if cronTask.IsExist(name) {
|
|
|
|
|
cronTask.tasks[name].Stop()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除任务
|
|
|
|
|
func(cronTask *CronTask) Delete(name string) {
|
|
|
|
|
cronTask.Stop(name)
|
2017-03-13 09:21:21 +00:00
|
|
|
|
cronTask.Lock()
|
|
|
|
|
defer cronTask.Unlock()
|
2017-03-10 09:08:51 +00:00
|
|
|
|
delete(cronTask.tasks, name)
|
|
|
|
|
}
|