Skip to content

Commit

Permalink
更新 timed_task.go 增加了两个方法 (#1504)
Browse files Browse the repository at this point in the history
Add:Timer工具增加AddTaskByFuncWithSeconds与AddTaskByJobWithSeconds方法
Fix:解决添加定时任务没有按时运行问题
  • Loading branch information
yaoyaochil committed Aug 9, 2023
1 parent 91c2909 commit 432725b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions server/utils/timer/timed_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ func (t *timer) AddTaskByFunc(taskName string, spec string, task func(), option
return id, err
}

// AddTaskByFuncWithSeconds 通过函数的方法使用WithSeconds添加任务
func (t *timer) AddTaskByFuncWhithSecond(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error) {
t.Lock()
defer t.Unlock()
option = append(option, cron.WithSeconds())
if _, ok := t.taskList[taskName]; !ok {
t.taskList[taskName] = cron.New(option...)
}
id, err := t.taskList[taskName].AddFunc(spec, task)
t.taskList[taskName].Start()
return id, err
}

// AddTaskByJob 通过接口的方法添加任务
func (t *timer) AddTaskByJob(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error) {
t.Lock()
Expand All @@ -47,6 +60,19 @@ func (t *timer) AddTaskByJob(taskName string, spec string, job interface{ Run()
return id, err
}

// AddTaskByJobWithSeconds 通过接口的方法添加任务
func (t *timer) AddTaskByJobWithSeconds(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error) {
t.Lock()
defer t.Unlock()
option = append(option, cron.WithSeconds())
if _, ok := t.taskList[taskName]; !ok {
t.taskList[taskName] = cron.New(option...)
}
id, err := t.taskList[taskName].AddJob(spec, job)
t.taskList[taskName].Start()
return id, err
}

// FindCron 获取对应taskName的cron 可能会为空
func (t *timer) FindCron(taskName string) (*cron.Cron, bool) {
t.Lock()
Expand Down

0 comments on commit 432725b

Please sign in to comment.