diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 060612be..c9507896 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -13,13 +13,13 @@ name: "CodeQL" on: push: - branches: [ main ] + branches: [ v2 ] branches-ignore: "dependabot/**" pull_request: paths-ignore: - '**.md' # The branches below must be a subset of the branches above - branches: [ main ] + branches: [ v2 ] schedule: - cron: '34 7 * * 1' diff --git a/.github/workflows/file_formatting.yml b/.github/workflows/file_formatting.yml index 9b20d3dd..eb3d5889 100644 --- a/.github/workflows/file_formatting.yml +++ b/.github/workflows/file_formatting.yml @@ -1,10 +1,10 @@ on: push: branches: - - main + - v2 pull_request: branches: - - main + - v2 name: formatting jobs: diff --git a/.github/workflows/go_test.yml b/.github/workflows/go_test.yml index dc3853dd..9168f453 100644 --- a/.github/workflows/go_test.yml +++ b/.github/workflows/go_test.yml @@ -1,10 +1,10 @@ on: push: branches: - - main + - v2 pull_request: branches: - - main + - v2 name: golangci-lint jobs: diff --git a/example_test.go b/example_test.go index 95230cb5..7910a827 100644 --- a/example_test.go +++ b/example_test.go @@ -30,10 +30,6 @@ func ExampleDailyJob() { _, _ = gocron.NewScheduler() } -func ExampleMinuteJob() { - _, _ = gocron.NewScheduler() -} - func ExampleDurationJob() { _, _ = gocron.NewScheduler() } @@ -42,10 +38,6 @@ func ExampleDurationRandomJob() { _, _ = gocron.NewScheduler() } -func ExampleHourlyJob() { - _, _ = gocron.NewScheduler() -} - func ExampleJob_ID() { _, _ = gocron.NewScheduler() } @@ -58,8 +50,22 @@ func ExampleJob_NextRun() { _, _ = gocron.NewScheduler() } -func ExampleMillisecondJob() { - _, _ = gocron.NewScheduler() +func ExampleMonthlyJob() { + s, _ := gocron.NewScheduler() + + _, _ = s.NewJob( + gocron.MonthlyJob( + 1, + gocron.NewDaysOfTheMonth(3, -5, -1), + gocron.NewAtTimes( + gocron.NewAtTime(10, 30, 0), + gocron.NewAtTime(11, 15, 0), + ), + gocron.NewTask( + func() {}, + ), + ), + ) } func ExampleNewScheduler() { @@ -89,7 +95,7 @@ func ExampleScheduler_RemoveJob() { _, _ = gocron.NewScheduler() } -func ExampleWithShutdownTimeout() { +func ExampleWithStopTimeout() { _, _ = gocron.NewScheduler() } @@ -97,18 +103,13 @@ func ExampleScheduler_Start() { _, _ = gocron.NewScheduler() } -func ExampleScheduler_Stop() { +func ExampleScheduler_StopJobs() { _, _ = gocron.NewScheduler() } func ExampleScheduler_Update() { _, _ = gocron.NewScheduler() } - -func ExampleSecondJob() { - _, _ = gocron.NewScheduler() -} - func ExampleWeeklyJob() { _, _ = gocron.NewScheduler() } diff --git a/job.go b/job.go index f68fe1f5..b315d067 100644 --- a/job.go +++ b/job.go @@ -524,7 +524,12 @@ func (m monthlyJob) next(lastRun time.Time) time.Time { if !next.IsZero() { return next } - return m.nextMonthDayAtTime(firstDayNextMonth, days) + for next.IsZero() { + next = m.nextMonthDayAtTime(firstDayNextMonth, days) + firstDayNextMonth = firstDayNextMonth.AddDate(0, 1, 0) + } + + return next } func (m monthlyJob) nextMonthDayAtTime(lastRun time.Time, days []int) time.Time { @@ -532,6 +537,11 @@ func (m monthlyJob) nextMonthDayAtTime(lastRun time.Time, days []int) time.Time if day >= lastRun.Day() { for _, at := range m.atTimes { atDate := time.Date(lastRun.Year(), lastRun.Month(), day, at.Hour(), at.Minute(), at.Second(), lastRun.Nanosecond(), lastRun.Location()) + // this check handles if we're setting a day not in the current month + // e.g. setting day 31 in Feb results in March 2nd + if atDate.Month() != lastRun.Month() { + continue + } if atDate.After(lastRun) { return atDate } diff --git a/job_test.go b/job_test.go index 8f93c33b..4c92a76d 100644 --- a/job_test.go +++ b/job_test.go @@ -86,6 +86,17 @@ func TestMonthlyJob_next(t *testing.T) { time.Date(2000, 1, 31, 5, 30, 0, 0, time.UTC), 2 * 24 * time.Hour, }, + { + "day not in current month, runs next month", + []int{31}, + nil, + []time.Time{ + time.Date(0, 0, 0, 5, 30, 0, 0, time.UTC), + }, + time.Date(2000, 1, 31, 5, 30, 0, 0, time.UTC), + time.Date(2000, 3, 31, 5, 30, 0, 0, time.UTC), + 29*24*time.Hour + 31*24*time.Hour, + }, } for _, tt := range tests {