[BUG] Wrong next run's time #277
Answered
by
JohnRoesler
hungdoansy
asked this question in
Q&A
-
Hi, I tried to get time of the next run, but the function kept returning I tried this code on playground (link) and it worked just fine. I have no idea what's wrong in my implemetation. Below is the code I used in my macbook s := gocron.NewScheduler(time.UTC)
// Every 1 hour
job, err := s.Cron("0 * * * *").Do(func() {
logger.Info("starts")
// ... do something
logger.Info("ends")
})
t := job.NextRun()
logger.Infof("next run %s", t) |
Beta Was this translation helpful? Give feedback.
Answered by
JohnRoesler
Jan 7, 2022
Replies: 1 comment 1 reply
-
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)
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
hungdoansy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!