Repository: go-co-op/gocron
Pull request #532 (2023-07-31 16:34:42)

fix update when used with DoWithJobDetails
### What does this do?


### Which issue(s) does this PR fix/relate to?
<!--- Put `Resolves #XXX` here to auto-close the issue that your PR fixes (if such) --->


### List any changes that modify/break current functionality


### Have you included tests for your changes?


### Did you document any new/modified functionality?

- [ ] Updated `example_test.go`
- [ ] Updated `README.md`

### Notes



Patch:
diff --git a/.golangci.yaml b/.golangci.yaml
index 7c86335..0354528 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 4744bac..d72a0f1 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 9d7f614..9fa2e13 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 {


Test patch:
diff --git a/scheduler_test.go b/scheduler_test.go
index 6a67796..9cfb3b3 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) {