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
+6.3k Golang : Create zip/ePub file without compression(use Store algorithm)
+8.4k Golang : Get all countries currencies code in JSON format
+5.7k PHP : Shuffle to display different content or advertisement
+17.7k Golang : Delete duplicate items from a slice/array
+17.9k Golang : Check if directory exist and create if does not exist
+7.3k Golang : How to check variable or object type during runtime?
+7.3k Golang : How to check if input string is a word?
+6.3k Golang : Dealing with postal or zip code example
+10.4k SSL : The certificate is not trusted because no issuer chain was provided
+31.2k Golang : Regular Expression for alphanumeric and underscore
+18.8k nginx: [emerg] unknown directive "passenger_enabled"
+12.1k Golang : Read from buffered reader until specific number of bytes