go – Use gocron scheduler to schedule job on specific day at specific time-ThrowExceptions

Exception or error:

I want to schedule a job on a specific day at a specific time with some interval. I am using gocron scheduler for this. But I can’t find a way to start a job on specific day. e.g. I want to execute a job on 7 Sept 2019 at 330pm. From 7 Sept, I want that job to be executed daily or weekly. How can I do that using gocron. or Any other packages available?

I tried passing UTC time to gocron.At() but its panics as it’s expecting only “03:30” time formats and doesn’t expect date.

How to solve:

When looking at the documentation for gocron, it does not seem to be designed to support scheduling things for specific days. It seems to be designed as a way to schedule things to run at various intervals, very similar to what the original cron utility was designed to do. So you would specify “I want this function to get called every 2 hours” or “I want this function to get called every Sunday at 3PM”. There does not seem to be any documentation about starting jobs from a specific day.

The mentioned At(string) method is documented as allowing you to specify a time of day to run something. So you would use that to set that your job runs at 3:30PM.

If you wish to specify a start time, you would likely need to find another scheduling library or implement it yourself by creating a goroutine that sleeps until a specific time. The StackOverflow post mentioned by domcyrus looks like an excellent resource for implementing it yourself as well as listing some other scheduling libraries.

Leave a Reply

Your email address will not be published. Required fields are marked *