Skip to content

Commit

Permalink
clear the job context for singleton on update (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler authored Apr 13, 2021
1 parent ff5a09b commit f9bb23b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocron

import (
"context"
"fmt"
"math"
"reflect"
Expand Down Expand Up @@ -829,6 +830,7 @@ func (s *Scheduler) Update() (*Job, error) {
return job, wrapOrError(job.error, ErrUpdateCalledWithoutJob)
}
job.stop()
job.ctx, job.cancel = context.WithCancel(context.Background())
return s.Do(job.function, job.parameters...)
}

Expand Down
27 changes: 27 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,33 @@ func TestScheduler_Update(t *testing.T) {
assert.Equal(t, 3, counter)
})

t.Run("happy singleton mode", func(t *testing.T) {
s := NewScheduler(time.UTC)

var counterMutex sync.RWMutex
counter := 0

j, err := s.Every(1).Day().SingletonMode().Do(func() { counterMutex.Lock(); defer counterMutex.Unlock(); counter++ })
require.NoError(t, err)

s.StartAsync()

time.Sleep(300 * time.Millisecond)
_, err = s.Job(j).Every("500ms").Update()
require.NoError(t, err)

time.Sleep(550 * time.Millisecond)
_, err = s.Job(j).Every("750ms").Update()
require.NoError(t, err)

time.Sleep(800 * time.Millisecond)
s.Stop()

counterMutex.RLock()
defer counterMutex.RUnlock()
assert.Equal(t, 3, counter)
})

t.Run("update called with job call", func(t *testing.T) {
s := NewScheduler(time.UTC)
_, err := s.Every("1s").Do(func() {})
Expand Down

0 comments on commit f9bb23b

Please sign in to comment.