Skip to content

Commit

Permalink
Merge branch 'v2' into monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler authored Dec 12, 2024
2 parents c4c4bc0 + 560a9dc commit 5c337a7
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
with:
go-version: ${{ matrix.go-version }}
- name: golangci-lint
uses: golangci/[email protected].0
uses: golangci/[email protected].1
with:
version: v1.59.1
- name: test
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,11 @@ We appreciate the support for free and open source software!

This project is supported by:

- [Jetbrains](https://www.jetbrains.com/?from=gocron)
- [Sentry](https://sentry.io/welcome/)
[Jetbrains](https://www.jetbrains.com/?from=gocron)
![JetBrains logo](https://resources.jetbrains.com/storage/products/company/brand/logos/jetbrains.png)


[Sentry](https://sentry.io/welcome/)

## Star History

Expand Down
3 changes: 3 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var (
ErrDailyJobAtTimeNil = fmt.Errorf("gocron: DailyJob: atTime within atTimes must not be nil")
ErrDailyJobAtTimesNil = fmt.Errorf("gocron: DailyJob: atTimes must not be nil")
ErrDailyJobHours = fmt.Errorf("gocron: DailyJob: atTimes hours must be between 0 and 23 inclusive")
ErrDailyJobZeroInterval = fmt.Errorf("gocron: DailyJob: interval must be greater than 0")
ErrDailyJobMinutesSeconds = fmt.Errorf("gocron: DailyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
ErrDurationJobIntervalZero = fmt.Errorf("gocron: DurationJob: time interval is 0")
ErrDurationRandomJobMinMax = fmt.Errorf("gocron: DurationRandomJob: minimum duration must be less than maximum duration")
Expand All @@ -20,6 +21,7 @@ var (
ErrMonthlyJobAtTimesNil = fmt.Errorf("gocron: MonthlyJob: atTimes must not be nil")
ErrMonthlyJobDaysNil = fmt.Errorf("gocron: MonthlyJob: daysOfTheMonth must not be nil")
ErrMonthlyJobHours = fmt.Errorf("gocron: MonthlyJob: atTimes hours must be between 0 and 23 inclusive")
ErrMonthlyJobZeroInterval = fmt.Errorf("gocron: MonthlyJob: interval must be greater than 0")
ErrMonthlyJobMinutesSeconds = fmt.Errorf("gocron: MonthlyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
ErrNewJobTaskNil = fmt.Errorf("gocron: NewJob: Task must not be nil")
ErrNewJobTaskNotFunc = fmt.Errorf("gocron: NewJob: Task.Function must be of kind reflect.Func")
Expand All @@ -33,6 +35,7 @@ var (
ErrWeeklyJobAtTimesNil = fmt.Errorf("gocron: WeeklyJob: atTimes must not be nil")
ErrWeeklyJobDaysOfTheWeekNil = fmt.Errorf("gocron: WeeklyJob: daysOfTheWeek must not be nil")
ErrWeeklyJobHours = fmt.Errorf("gocron: WeeklyJob: atTimes hours must be between 0 and 23 inclusive")
ErrWeeklyJobZeroInterval = fmt.Errorf("gocron: WeeklyJob: interval must be greater than 0")
ErrWeeklyJobMinutesSeconds = fmt.Errorf("gocron: WeeklyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
ErrPanicRecovered = fmt.Errorf("gocron: panic recovered")
ErrWithClockNil = fmt.Errorf("gocron: WithClock: clock must not be nil")
Expand Down
7 changes: 6 additions & 1 deletion executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,12 @@ func (e *executor) runJob(j internalJob, jIn jobIn) {
}

startTime := time.Now()
err := e.callJobWithRecover(j)
var err error
if j.afterJobRunsWithPanic != nil {
err = e.callJobWithRecover(j)
} else {
err = callJobFuncWithParams(j.function, j.parameters...)
}
e.recordJobTiming(startTime, time.Now(), j)
if err != nil {
_ = callJobFuncWithParams(j.afterJobRunsWithError, j.id, j.name, err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/google/uuid v1.6.0
github.com/jonboulle/clockwork v0.4.0
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
go.uber.org/goleak v1.3.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
Expand Down
14 changes: 13 additions & 1 deletion job.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func (d dailyJobDefinition) setup(j *internalJob, location *time.Location, _ tim
return ErrDailyJobMinutesSeconds
}

if d.interval == 0 {
return ErrDailyJobZeroInterval
}

ds := dailyJob{
interval: d.interval,
atTimes: atTimesDate,
Expand All @@ -270,6 +274,9 @@ type weeklyJobDefinition struct {

func (w weeklyJobDefinition) setup(j *internalJob, location *time.Location, _ time.Time) error {
var ws weeklyJob
if w.interval == 0 {
return ErrWeeklyJobZeroInterval
}
ws.interval = w.interval

if w.daysOfTheWeek == nil {
Expand Down Expand Up @@ -335,6 +342,9 @@ type monthlyJobDefinition struct {

func (m monthlyJobDefinition) setup(j *internalJob, location *time.Location, _ time.Time) error {
var ms monthlyJob
if m.interval == 0 {
return ErrMonthlyJobZeroInterval
}
ms.interval = m.interval

if m.daysOfTheMonth == nil {
Expand Down Expand Up @@ -1089,12 +1099,14 @@ func (j job) RunNow() error {
defer cancel()
resp := make(chan error, 1)

t := time.NewTimer(100 * time.Millisecond)
select {
case j.runJobRequest <- runJobRequest{
id: j.id,
outChan: resp,
}:
case <-time.After(100 * time.Millisecond):
t.Stop()
case <-t.C:
return ErrJobRunNowFailed
}
var err error
Expand Down
15 changes: 15 additions & 0 deletions mocks/job.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions mocks/scheduler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,11 @@ func (s *scheduler) stopScheduler() {
}
var err error
if s.started {
t := time.NewTimer(s.exec.stopTimeout + 1*time.Second)
select {
case err = <-s.exec.done:
case <-time.After(s.exec.stopTimeout + 1*time.Second):
t.Stop()
case <-t.C:
err = ErrStopExecutorTimedOut
}
}
Expand Down Expand Up @@ -741,20 +743,27 @@ func (s *scheduler) StopJobs() error {
return nil
case s.stopCh <- struct{}{}:
}

t := time.NewTimer(s.exec.stopTimeout + 2*time.Second)
select {
case err := <-s.stopErrCh:
t.Stop()
return err
case <-time.After(s.exec.stopTimeout + 2*time.Second):
case <-t.C:
return ErrStopSchedulerTimedOut
}
}

func (s *scheduler) Shutdown() error {
s.shutdownCancel()

t := time.NewTimer(s.exec.stopTimeout + 2*time.Second)
select {
case err := <-s.stopErrCh:

t.Stop()
return err
case <-time.After(s.exec.stopTimeout + 2*time.Second):
case <-t.C:
return ErrStopSchedulerTimedOut
}
}
Expand Down
35 changes: 35 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,17 @@ func TestScheduler_NewJobErrors(t *testing.T) {
nil,
ErrDailyJobMinutesSeconds,
},
{
"daily job interval 0",
DailyJob(
0,
NewAtTimes(
NewAtTime(1, 0, 0),
),
),
nil,
ErrDailyJobZeroInterval,
},
{
"weekly job at times nil",
WeeklyJob(
Expand Down Expand Up @@ -675,6 +686,18 @@ func TestScheduler_NewJobErrors(t *testing.T) {
nil,
ErrWeeklyJobMinutesSeconds,
},
{
"weekly job interval zero",
WeeklyJob(
0,
NewWeekdays(time.Monday),
NewAtTimes(
NewAtTime(1, 0, 0),
),
),
nil,
ErrWeeklyJobZeroInterval,
},
{
"monthly job at times nil",
MonthlyJob(
Expand Down Expand Up @@ -755,6 +778,18 @@ func TestScheduler_NewJobErrors(t *testing.T) {
nil,
ErrMonthlyJobMinutesSeconds,
},
{
"monthly job interval zero",
MonthlyJob(
0,
NewDaysOfTheMonth(1),
NewAtTimes(
NewAtTime(1, 0, 0),
),
),
nil,
ErrMonthlyJobZeroInterval,
},
{
"WithName no name",
DurationJob(
Expand Down

0 comments on commit 5c337a7

Please sign in to comment.