Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix update when used with DoWithJobDetails #532

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
timeout: 2m
timeout: 5m
issues-exit-code: 1
tests: true

Expand All @@ -15,8 +15,7 @@ issues:
linters:
enable:
- bodyclose
- errcheck
- gofmt
- exportloopref
- gofumpt
- goimports
- gosec
Expand All @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down