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] - job.NextRub and job.LastRun not immediately available inside the job when the job begins #690

Closed
daviesluke opened this issue Mar 12, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@daviesluke
Copy link

daviesluke commented Mar 12, 2024

Describe the bug

When a job begins and a call is made to the NextRun or LastRun function inside the job function being executed then if it is requested too quickly within the job code then get the error -> "gocron: job not found"

To Reproduce

Steps to reproduce the behavior:

Here is some test code to reproduce the data

package main

import (
	"fmt"
	"sync"
	"time"

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

var job gocron.Job

func runJob() {
	fmt.Printf("Job %s starting\n", job.ID())

	// Get nextrun time
	if nextRun, err := job.NextRun(); err != nil {
		fmt.Printf("Error getting NextRun -> %v\n", err)
	} else {
		fmt.Printf("Got NextRun first time -> %v\n", nextRun)
	}

	time.Sleep(100 * time.Millisecond)

	if nextRun, err := job.NextRun(); err != nil {
		fmt.Printf("Error getting NextRun -> %v\n", err)
	} else {
		fmt.Printf("Got NextRun second time -> %v\n", nextRun)
	}
	fmt.Printf("Job %s complete\n", job.ID())
}

func main() {
	fmt.Println("Creating the scheduler")

	newSched, _ := gocron.NewScheduler()

	job, _ = newSched.NewJob(
		gocron.DurationJob(10*time.Second),
		gocron.NewTask(runJob),
	)

	fmt.Printf("Job %s created\n", job.ID())

	newSched.Start()

	fmt.Println("Scheduler Started")

	// Waiting forever - interrupt to quit
	wg := &sync.WaitGroup{}
	wg.Add(1)
	wg.Wait()

	// Not necessary just for documentation
	newSched.Shutdown()
}

This is the output received

Creating the scheduler
Job 263bf685-fb4d-4155-b9b2-a6314a4c2cf5 created
Scheduler Started
Job 263bf685-fb4d-4155-b9b2-a6314a4c2cf5 starting
Error getting NextRun -> gocron: job not found
Got NextRun second time -> 2024-03-12 15:12:48.750701944 +0000 GMT
Job 263bf685-fb4d-4155-b9b2-a6314a4c2cf5 complete
Job 263bf685-fb4d-4155-b9b2-a6314a4c2cf5 starting
Error getting NextRun -> gocron: job not found
Got NextRun second time -> 2024-03-12 15:12:58.750701944 +0000 GMT
Job 263bf685-fb4d-4155-b9b2-a6314a4c2cf5 complete
^C

Version

V2.5.2

Expected behavior

Expected to be able to examine NextRun and LastRun output immediately on job start

Additional context

None

@JohnRoesler
Copy link
Contributor

Hey @daviesluke i'm not able to reproduce this. Running the same code you provided:

I get

Creating the scheduler
Job 2f6f1077-940c-4279-95a8-72993c1ff0e6 created
Scheduler Started
Job 2f6f1077-940c-4279-95a8-72993c1ff0e6 starting
Got NextRun first time -> 2024-03-13 21:17:30.576713
Got NextRun second time -> 2024-03-13 21:17:30.576713
Job 2f6f1077-940c-4279-95a8-72993c1ff0e6 complete

@daviesluke
Copy link
Author

Yes I also receive intermittent results

[oracle@orabox test_nextrun]$ ./test_nextrun
Creating the scheduler
Job bb40a041-602b-4e12-a158-288638d1c0a1 created
Scheduler Started
Job bb40a041-602b-4e12-a158-288638d1c0a1 starting
Got NextRun first time -> 2024-03-15 10:29:15.79272006 +0000 GMT
Got NextRun second time -> 2024-03-15 10:29:15.79272006 +0000 GMT
Job bb40a041-602b-4e12-a158-288638d1c0a1 complete
^C
[oracle@orabox test_nextrun]$ ./test_nextrun
Creating the scheduler
Job e19c12dc-dd9b-40de-97d5-ac5217a0b8d2 created
Scheduler Started
Job e19c12dc-dd9b-40de-97d5-ac5217a0b8d2 starting
Error getting NextRun -> gocron: job not found
Got NextRun second time -> 2024-03-15 10:29:30.795880785 +0000 GMT
Job e19c12dc-dd9b-40de-97d5-ac5217a0b8d2 complete
^C
[oracle@orabox test_nextrun]$ ./test_nextrun
Creating the scheduler
Job 31e77aac-c091-4601-98e8-bc91a4b68d81 created
Scheduler Started
Job 31e77aac-c091-4601-98e8-bc91a4b68d81 starting
Error getting NextRun -> gocron: job not found
Got NextRun second time -> 2024-03-15 10:29:56.283641162 +0000 GMT
Job 31e77aac-c091-4601-98e8-bc91a4b68d81 complete
Job 31e77aac-c091-4601-98e8-bc91a4b68d81 starting
Error getting NextRun -> gocron: job not found
Got NextRun second time -> 2024-03-15 10:30:06.283641162 +0000 GMT
Job 31e77aac-c091-4601-98e8-bc91a4b68d81 complete
Job 31e77aac-c091-4601-98e8-bc91a4b68d81 starting
Got NextRun first time -> 2024-03-15 10:30:16.283641162 +0000 GMT
Got NextRun second time -> 2024-03-15 10:30:16.283641162 +0000 GMT
Job 31e77aac-c091-4601-98e8-bc91a4b68d81 complete
^C

@JohnRoesler
Copy link
Contributor

I believe this is solved by #699

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
2 participants