Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hey, how do I set the last day of the month to be executed on time? #106

Open
JokerDevops opened this issue Sep 9, 2019 · 8 comments
Open

Comments

@JokerDevops
Copy link

Hey, how do I set the last day of the month to be executed on time?

@PriyaSharma45
Copy link

Did you find any way to do it?

@JohnRoesler
Copy link
Contributor

It doesn't look like there is a way to do that currently. Seems like a feature would need to be added. Perhaps a Monthly() option that allows for things like FirstDay(), LastDay(), DayOfMonth(15).

@JokerDevops
Copy link
Author

Yes, currently he needs FirstDay(), LastDay(), DayOfMonth(15)

@wishdev
Copy link
Contributor

wishdev commented Dec 4, 2019

I'm not certain this is the purview of gocron.

For example, I've needed to create a report on the 12th of each month - EXCEPT if the 12th was on the weekend and then it was either the 11th if the 12th was a Saturday or the 13th if the 12th was a Sunday. That would be an interesting set of options to describe in a single line of gocron scheduling.

So, maybe something like this for the question at hand

 func coreTask() {
     fmt.Println("Must be last day of month - time to make the donuts")
 }
 
 func task() {
     now := time.Now()
     currentYear, currentMonth, currentDay := now.Date()
     currentLocation := now.Location()
 
     firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
     lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
     if lastOfMonth.Day() == currentDay {
         coreTask()
     }
}

gocron.Every(1).Day().At("10:30").Do(task)

Last day of month code from here

@JohnRoesler
Copy link
Contributor

I like your solution @wishdev! Seems very reasonable to handle outside of gocron as to your point - the options could be highly involved getting exactly the right cron schedule unless it was a simple, I want this date of the month always, or the first day or last day.

@003random
Copy link

Nice solution @wishdev. Was also looking for something similar and this does the job. Good enough for me 👍

@JohnRoesler
Copy link
Contributor

@003random come check out our active fork https://github.com/go-co-op/gocron we’ve added support for Monthly schedules 👍

@003random
Copy link

Oh awesome! Thanks @JohnRoesler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants