Golang : Fix go-cron set time not working issue
Problem :
You followed the go-cron example at https://pkg.go.dev/github.com/go-co-op/gocron#section-readme but later on found out that the scheduled task did not execute at the specified time.
s := gocron.NewScheduler(time.UTC)
// set time
s.Every(1).Day().At("10:30").Do(func(){ ... })
// set multiple times
s.Every(1).Day().At("10:30;08:00").Do(func(){ ... })
Solution :
Instead of using time.UTC
, set the time location first to whatever timezone your server/machine is running at and then use the time for the go-cron scheduler.
For example :
localTime, err := time.LoadLocation("Asia/Singapore")
if err != nil {
logs.Fatal().Err(err).Msg("Error in time load location")
}
cronScheduler := gocron.NewScheduler(localTime)
Hope this helps and happy coding!
See also : Golang : How to get time zone and load different time zone?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.8k Golang : Take screen shot of browser with JQuery example
+7.3k Golang : alternative to os.Exit() function
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line
+22.3k Golang : Convert seconds to minutes and remainder seconds
+7.8k Swift : Convert (cast) String to Double
+9.3k Golang : How to check if a string with spaces in between is numeric?
+6.9k Golang : Pat multiplexer routing example
+6k Golang : Generate multiplication table from an integer example
+9.8k Golang : Qt get screen resolution and display on center example
+19.4k Golang : Fix cannot download, $GOPATH not set error
+6.9k How to let Facebook Login button redirect to a particular URL ?
+12.3k Golang : List running EC2 instances and descriptions