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

v2 daily job seems to execute in an infinite loop #634

Closed
USA-RedDragon opened this issue Dec 13, 2023 · 3 comments · Fixed by #635
Closed

v2 daily job seems to execute in an infinite loop #634

USA-RedDragon opened this issue Dec 13, 2023 · 3 comments · Fixed by #635
Labels
bug Something isn't working

Comments

@USA-RedDragon
Copy link

USA-RedDragon commented Dec 13, 2023

Describe the bug

When starting the scheduler after adding a daily job it seems to execute in an infinite loop. A minimal reproduction of the issue is below. I could be "holding it wrong", so to speak too.

Prior to v2 I was using this a daily job like so:

_, err := scheduler.Every(1).Day().At("00:00").Do(func() {
	// Do the thing
})

After checking the godocs for DailyJob, it looked like this would be the v2 equivalent:

_, err = scheduler.NewJob(
	gocron.DailyJob(1, gocron.NewAtTimes(
		gocron.NewAtTime(0, 0, 0),
	)),
	gocron.NewTask(func() {
		// Do the thing
	}),
)

To Reproduce

I have a minimal reproduction of the issue.

go.mod entry:
github.com/go-co-op/gocron/v2 v2.0.0

Current hashes in go.sum:

github.com/go-co-op/gocron/v2 v2.0.0 h1:wVB5Bh+665+r/zQ+ErsyUypyPWGozEnNV7EEZ8D0HIU=
github.com/go-co-op/gocron/v2 v2.0.0/go.mod h1:DodDqurAedt8cj/dbFM8obVSgPv0Vch80eF7neNVwmg=
package main

import (
	"fmt"
	"log"
	"time"

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

func main() {
	scheduler, err := gocron.NewScheduler()
	if err != nil {
		log.Fatalf("Failed to create scheduler: %s", err)
	}

	_, err = scheduler.NewJob(
		gocron.DailyJob(
			1,
			gocron.NewAtTimes(
				gocron.NewAtTime(0, 0, 0),
			),
		),
		gocron.NewTask(func() {
			fmt.Printf("Task ran!\n")
		}),
	)

	if err != nil {
		log.Fatalf("Failed to schedule job: %s", err)
	}

	scheduler.Start()

	time.Sleep(1 * time.Second)

	err = scheduler.StopJobs()
	if err != nil {
		log.Fatalf("Failed to stop jobs: %s", err)
	}
	err = scheduler.Shutdown()
	if err != nil {
		log.Fatalf("Failed to shutdown scheduler: %s", err)
	}
}

Running this results in about 45k "Task ran!" messages on my machine.

Version

The release version or commit SHA you're using.

v2.0.0

Have you tried the latest version?

Yes

Expected behavior

The daily job should execute daily.

Additional context

N/A

@JohnRoesler
Copy link
Contributor

@USA-RedDragon Thanks for the call out! v2.0.1 has the fix.

package main

import (
	"fmt"
	"log"
	"time"

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

func main() {
	scheduler, err := gocron.NewScheduler()
	if err != nil {
		log.Fatalf("Failed to create scheduler: %s", err)
	}

	job, err := scheduler.NewJob(
		gocron.DailyJob(
			1,
			gocron.NewAtTimes(
				gocron.NewAtTime(0, 0, 0),
			),
		),
		gocron.NewTask(func() {
			fmt.Printf("Task ran!\n")
		}),
	)

	if err != nil {
		log.Fatalf("Failed to schedule job: %s", err)
	}

	scheduler.Start()

	time.Sleep(1 * time.Second)

	nextRun, err := job.NextRun()
	if err != nil {
		log.Printf("failed to get nextRun: %s\n", err)
	}
	log.Printf("job next run: %s\n", nextRun)

	err = scheduler.StopJobs()
	if err != nil {
		log.Fatalf("Failed to stop jobs: %s", err)
	}
	err = scheduler.Shutdown()
	if err != nil {
		log.Fatalf("Failed to shutdown scheduler: %s", err)
	}
}

@USA-RedDragon
Copy link
Author

Thanks for the fast fix!

@JohnRoesler
Copy link
Contributor

Sometimes the timing just lines up perfectly. I should be careful though, I don't want to set an expectation that I always fix things that fast! 😂

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