Skip to content

[BUG] Wrong next run's time #277

Answered by JohnRoesler
hungdoansy asked this question in Q&A
Discussion options

You must be logged in to vote

The scheduler doesn't calculate next run times until it has been started. Once you start the scheduler and call next run you'll get the right time!

package main

import (
	"log"
	"time"

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

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

	// Every 1 hour
	job, err := s.Cron("0 * * * *").Do(func() {
		log.Println("start")

		// ... do something

		log.Println("ends")
	})
	if err != nil {
		log.Fatalln(err)
	}

	t := job.NextRun()
	log.Printf("next run %s", t)

	s.StartAsync()

	t = job.NextRun()
	log.Printf("next run %s", t)
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@hungdoansy
Comment options

Answer selected by hungdoansy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants