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

[BUG] - Current Job Not Correct In DoWithJobDetails with SingletonMode #520

Closed
forstef opened this issue Jun 26, 2023 · 2 comments · Fixed by #521
Closed

[BUG] - Current Job Not Correct In DoWithJobDetails with SingletonMode #520

forstef opened this issue Jun 26, 2023 · 2 comments · Fixed by #521
Labels
bug Something isn't working

Comments

@forstef
Copy link

forstef commented Jun 26, 2023

Describe the bug

Called function: s.Every(xxx).Seconds().DoWithJobDetails(f)
But in
func f(job gocron.Job){
// you always get the same return from job.NextRun() and LastRun(), it is the first value and did not change in every call
}

Version

1.29.0

Expected behavior

job.NextRun(),LastRun() should get the time for the next run in task function, according to scheduler.
Probably other current job related functions are not changed, eg, PreviousRun()?

@forstef forstef added the bug Something isn't working label Jun 26, 2023
@forstef forstef changed the title [BUG] - NextRun() Return Not Correct [BUG] - Current Job Not Correct In DoWithJobDetails Jun 26, 2023
@JohnRoesler
Copy link
Contributor

Hi @forstef

Could you provide more details about what you're seeing vs. expecting?

It appears to be working as I'd expect. One slightly confusing note is that within the job run the LastRun() ~= time.Now()

  • job.PreviousRun() is the run time prior
  • job.LastRun() is going to be time.Now()
  • job.NextRun() is the next time the job will run

On the initial two runs PreviousRun won't be populated, and on the first run LastRun won't be populated.

package main

import (
	"log"
	"time"

	"github.com/go-co-op/gocron"
)

func main() {
	s := gocron.NewScheduler(time.UTC)

	_, err := s.Every(1).Seconds().DoWithJobDetails(func(job gocron.Job) {
		log.Printf("\nprevious:\t%s\nlast:\t\t%s\nnow:\t\t%s\nnext:\t\t%s\n\n\n", job.PreviousRun(), job.LastRun(), time.Now().UTC(), job.NextRun())
	})
	if err != nil {
		panic(err)
	}
	s.StartBlocking()
}

Output:

2023/06/26 11:10:30 
previous:       0001-01-01 00:00:00 +0000 UTC
last:           0001-01-01 00:00:00 +0000 UTC
now:            2023-06-26 16:10:30.268039 +0000 UTC
next:           2023-06-26 16:10:31.267541 +0000 UTC


2023/06/26 11:10:31 
previous:       0001-01-01 00:00:00 +0000 UTC
last:           2023-06-26 16:10:31.267541 +0000 UTC
now:            2023-06-26 16:10:31.269261 +0000 UTC
next:           2023-06-26 16:10:32.268798 +0000 UTC


2023/06/26 11:10:32 
previous:       2023-06-26 16:10:31.267541 +0000 UTC
last:           2023-06-26 16:10:32.268798 +0000 UTC
now:            2023-06-26 16:10:32.270329 +0000 UTC
next:           2023-06-26 16:10:33.268798 +0000 UTC


2023/06/26 11:10:33 
previous:       2023-06-26 16:10:32.268798 +0000 UTC
last:           2023-06-26 16:10:33.268798 +0000 UTC
now:            2023-06-26 16:10:33.26998 +0000 UTC
next:           2023-06-26 16:10:34.268798 +0000 UTC


2023/06/26 11:10:34 
previous:       2023-06-26 16:10:33.268798 +0000 UTC
last:           2023-06-26 16:10:34.268798 +0000 UTC
now:            2023-06-26 16:10:34.269564 +0000 UTC
next:           2023-06-26 16:10:35.268798 +0000 UTC


2023/06/26 11:10:35 
previous:       2023-06-26 16:10:34.268798 +0000 UTC
last:           2023-06-26 16:10:35.268798 +0000 UTC
now:            2023-06-26 16:10:35.27005 +0000 UTC
next:           2023-06-26 16:10:36.268798 +0000 UTC

@forstef
Copy link
Author

forstef commented Jun 27, 2023

When you added s.SingletonModeAll() behind s := gocron.NewScheduler(time.UTC), you would get the issue.

@JohnRoesler JohnRoesler changed the title [BUG] - Current Job Not Correct In DoWithJobDetails [BUG] - Current Job Not Correct In DoWithJobDetails with SingletonMode Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants