gocron
gocron copied to clipboard
[FEATURE] - Time format allow wildcards
First of all - awesome project, just what I needed in my life :smiley:
It seems like there's error on time format for when defining on which minute cronjob should start at
eg:
scheduler.Every(1).Hour().At(":00").Lock().Do(MySuperCoolMethodToDeleteOldStuff)
or
scheduler.Every(1).Hour().At("**:30").Lock().Do(MySuperCoolMethodToDeleteOldStuff)
When checking https://github.com/dbader/schedule then I can see that the time format is ":00", but on ruby clockwork https://github.com/Rykian/clockwork#event-parameters then the format is "**:30". I was hoping that similar things would work on this as well, but it seems now to me moreof like a feature request.
What I intend to do is to sync the deployments to start at same time, so only one instance of the service will execute the method.
So, that job MySuperCoolMethodToDeleteOldStuff would run at 12:00 13:00 14:00 etc..
What you need can be translated to the old-school crontab fomat:
scheduler.Every(1).Hour().At(":00").Lock()
=> 0 */1 * * *
scheduler.Every(1).Hour().At("**:30")
=> 30 */1 * * *
Actually, scheduler.Every(1).Hour()
runs at 00:00 every hour by default, you donnot need to write At(":00")
explictly.
And, try this for running at specify minute in a hour.
now := time.Now()
timeToRun := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 30, 0, 0, time.UTC) // minute 30
scheduler.Every(1).Hour().StartAt(timeToRun).Lock().Do(MySuperCoolMethodToDeleteOldStuff)
Indeed, it could be defined as crontab format - but I liked the good-and-simple solution (so anyone glancing it wouldn't have to think twice to understand) that other projects used.
I did not know that by default it will run @ 00:00 every hour, this will simplify one of them, and in another one (running at :30 each hour) I used about the same idea
nextHalfHour := time.Now().Add(30 * time.Minute).Truncate(time.Hour).Add(30 * time.Minute)
scheduler.Every(1).Hour().StartAt(nextHalfHour).Lock().Do(MySuperCoolMethodToDeleteOldStuff)
I thought that time must be in future (thus Adding is done twice), if not, can be even shorter
@GeitV interesting idea here. Right now, At()
isn’t supported on the Hour()
definition - it’s used with the Day()
interv. However you could use StartAt()
and round to the next 30 min time.
I think it could be a nice feature to add what you’re proposing. I’ll poke at it when I get some time and see what it could look like.
@GeitV I don't think we'll support this because you can accomplish it with the Cron
or CronWithSeconds
methods.
Please let me know if you don't think that addresses the need well enough.