From b55c4492606d554f905911ad85e16aa007546d72 Mon Sep 17 00:00:00 2001 From: John Roesler Date: Mon, 31 Jul 2023 11:34:22 -0500 Subject: [PATCH] fix update when used with DoWithJobDetails --- .golangci.yaml | 6 +++--- job.go | 6 ++++++ scheduler.go | 11 ++++++----- scheduler_test.go | 13 +++++++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 7c86335f..03545280 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,5 +1,5 @@ run: - timeout: 2m + timeout: 5m issues-exit-code: 1 tests: true @@ -15,8 +15,7 @@ issues: linters: enable: - bodyclose - - errcheck - - gofmt + - exportloopref - gofumpt - goimports - gosec @@ -28,6 +27,7 @@ linters: - staticcheck - typecheck - unused + - whitespace output: # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" diff --git a/job.go b/job.go index 4744bac0..d72a0f11 100644 --- a/job.go +++ b/job.go @@ -312,6 +312,12 @@ func (j *Job) setDuration(t time.Duration) { j.duration = t } +func (j *Job) setInterval(i int) { + j.mu.Lock() + defer j.mu.Unlock() + j.interval = i +} + // hasTags returns true if all tags are matched on this Job func (j *Job) hasTags(tags ...string) bool { // Build map of all Job tags for easy comparison diff --git a/scheduler.go b/scheduler.go index 9d7f6147..9fa2e137 100644 --- a/scheduler.go +++ b/scheduler.go @@ -289,7 +289,6 @@ func (s *Scheduler) calculateMonths(job *Job, lastRun time.Time) nextRun { } if len(job.daysOfTheMonth) != 0 { // calculate days to job.daysOfTheMonth - nextRunDateMap := make(map[int]nextRun) for _, day := range job.daysOfTheMonth { nextRunDateMap[day] = calculateNextRunForMonth(s, job, lastRun, day) @@ -548,7 +547,7 @@ func (s *Scheduler) Every(interval interface{}) *Scheduler { job.error = wrapOrError(job.error, ErrInvalidInterval) } case time.Duration: - job.interval = 0 + job.setInterval(0) job.setDuration(interval) job.setUnit(duration) case string: @@ -1153,10 +1152,8 @@ func (s *Scheduler) Months(daysOfTheMonth ...int) *Scheduler { job.error = wrapOrError(job.error, ErrInvalidDayOfMonthEntry) } } else { - repeatMap := make(map[int]int) for _, dayOfMonth := range daysOfTheMonth { - if dayOfMonth < 1 || dayOfMonth > 28 { job.error = wrapOrError(job.error, ErrInvalidDayOfMonthEntry) break @@ -1298,7 +1295,11 @@ func (s *Scheduler) Update() (*Job, error) { job.setStartsImmediately(false) if job.runWithDetails { - return s.DoWithJobDetails(job.function, job.parameters...) + params := job.parameters + if len(params) > 0 { + params = job.parameters[:len(job.parameters)-1] + } + return s.DoWithJobDetails(job.function, params...) } if job.runConfig.mode == singletonMode { diff --git a/scheduler_test.go b/scheduler_test.go index 6a677960..9cfb3b39 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -1931,6 +1931,19 @@ func TestScheduler_Update(t *testing.T) { require.NoError(t, err) s.Stop() }) + + // Verifies https://github.com/go-co-op/gocron/issues/528 + t.Run("update with DoWithJobDetails", func(t *testing.T) { + s := NewScheduler(time.UTC) + j, err := s.Every(time.Second).DoWithJobDetails(func(job Job) {}) + require.NoError(t, err) + s.StartAsync() + time.Sleep(time.Millisecond * 500) + + _, err = s.Job(j).Every(2 * time.Second).Update() + require.NoError(t, err) + s.Stop() + }) } func TestScheduler_RunByTag(t *testing.T) {