Skip to content

Commit

Permalink
feat: 添加删除功能
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Jan 18, 2024
1 parent f0d8769 commit bbfdfab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
22 changes: 15 additions & 7 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,33 +145,34 @@ func (s *Server) Jobs() []*Job {
}

// Tick 添加一个新的定时任务
func (s *Server) Tick(title localeutil.Stringer, f JobFunc, dur time.Duration, imm, delay bool) {
s.New(title, f, ticker.Tick(dur, imm), delay)
func (s *Server) Tick(title localeutil.Stringer, f JobFunc, dur time.Duration, imm, delay bool) func() {
return s.New(title, f, ticker.Tick(dur, imm), delay)
}

// Cron 使用 cron 表达式新建一个定时任务
//
// 具体文件可以参考 [cron.Parse]
func (s *Server) Cron(title localeutil.Stringer, f JobFunc, spec string, delay bool) {
func (s *Server) Cron(title localeutil.Stringer, f JobFunc, spec string, delay bool) func() {
scheduler, err := cron.Parse(spec, s.Location())
if err != nil {
panic(err)
}
s.New(title, f, scheduler, delay)
return s.New(title, f, scheduler, delay)
}

// At 添加 At 类型的定时器
//
// 具体文件可以参考 [at.At]
func (s *Server) At(title localeutil.Stringer, f JobFunc, t time.Time, delay bool) {
s.New(title, f, at.At(t), delay)
func (s *Server) At(title localeutil.Stringer, f JobFunc, t time.Time, delay bool) func() {
return s.New(title, f, at.At(t), delay)
}

// New 添加一个新的定时任务
//
// title 任务的简要描述;
// delay 是否从任务执行完之后,才开始计算下个执行的时间点。
func (s *Server) New(title localeutil.Stringer, f JobFunc, scheduler Scheduler, delay bool) {
// 返回一个删除当前任务的方法
func (s *Server) New(title localeutil.Stringer, f JobFunc, scheduler Scheduler, delay bool) func() {
job := &Job{
s: scheduler,
title: title,
Expand All @@ -184,4 +185,11 @@ func (s *Server) New(title localeutil.Stringer, f JobFunc, scheduler Scheduler,
job.init(time.Now())
s.clearAndSendNextScheduled() // 执行一次调度任务
}

return func() {
s.scheduleLocker.Lock()
s.jobs = slices.DeleteFunc(s.jobs, func(e *Job) bool { return e == job })
s.scheduleLocker.Unlock()
s.clearAndSendNextScheduled() // 执行一次调度任务
}
}
10 changes: 8 additions & 2 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,16 @@ func TestServer_Jobs(t *testing.T) {
now := time.Now()
srv.At(localeutil.StringPhrase("j1"), succFunc, now, false)
srv.At(localeutil.StringPhrase("j3"), succFunc, now, false)
srv.At(localeutil.StringPhrase("j2"), succFunc, now, false)
d := srv.At(localeutil.StringPhrase("j2"), succFunc, now, false)

jobs := srv.Jobs()
a.Equal(len(jobs), len(srv.jobs))
l := len(jobs)
a.Length(srv.jobs, l)

d()
jobs = srv.Jobs()
a.Equal(len(jobs), len(srv.jobs)).
Equal(l-1, len(jobs))
}

func TestServer_Cron(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ LOOP:
go j.run(now, s.erro, s.info)
}

if len(s.exitSchedule) > 0 { // 退出前清空 exitSchedule
if len(s.exitSchedule) > 0 { // 在退出函数和执行 sendNextScheduled 清空 exitSchedule
<-s.exitSchedule
}

Expand Down

0 comments on commit bbfdfab

Please sign in to comment.