Skip to content

Commit cc66c78

Browse files
feat(eventListener): add BeforeJobRunsSkipIfBeforeFuncErrors() (#813)
1 parent abfd9e5 commit cc66c78

File tree

6 files changed

+65
-9
lines changed

6 files changed

+65
-9
lines changed

.github/workflows/file_formatting.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ jobs:
1616
uses: actions/checkout@v4
1717
- name: verify example_test.go
1818
run: |
19-
grep "^func " example_test.go | sort -c
19+
grep "^func [a-z-A-Z]" example_test.go | sort -c

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fmt:
66
@go list -f {{.Dir}} ./... | xargs -I{} gofmt -w -s {}
77

88
lint:
9-
@grep "^func " example_test.go | sort -c
9+
@grep "^func [a-zA-Z]" example_test.go | sort -c
1010
@golangci-lint run
1111

1212
test:

example_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ func ExampleBeforeJobRuns() {
104104
)
105105
}
106106

107+
func ExampleBeforeJobRunsSkipIfBeforeFuncErrors() {
108+
s, _ := gocron.NewScheduler()
109+
defer func() { _ = s.Shutdown() }()
110+
111+
_, _ = s.NewJob(
112+
gocron.DurationJob(
113+
time.Second,
114+
),
115+
gocron.NewTask(
116+
func() {
117+
fmt.Println("Will never run, because before job func errors")
118+
},
119+
),
120+
gocron.WithEventListeners(
121+
gocron.BeforeJobRunsSkipIfBeforeFuncErrors(
122+
func(jobID uuid.UUID, jobName string) error {
123+
return fmt.Errorf("error")
124+
},
125+
),
126+
),
127+
)
128+
}
129+
107130
func ExampleCronJob() {
108131
s, _ := gocron.NewScheduler()
109132
defer func() { _ = s.Shutdown() }()

executor.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -389,16 +389,28 @@ func (e *executor) runJob(j internalJob, jIn jobIn) {
389389
}
390390
defer func() { _ = lock.Unlock(j.ctx) }()
391391
}
392+
392393
_ = callJobFuncWithParams(j.beforeJobRuns, j.id, j.name)
393394

395+
err := callJobFuncWithParams(j.beforeJobRunsSkipIfBeforeFuncErrors, j.id, j.name)
396+
if err != nil {
397+
e.sendOutForRescheduling(&jIn)
398+
399+
select {
400+
case e.jobsOutCompleted <- j.id:
401+
case <-e.ctx.Done():
402+
}
403+
404+
return
405+
}
406+
394407
e.sendOutForRescheduling(&jIn)
395408
select {
396409
case e.jobsOutCompleted <- j.id:
397410
case <-e.ctx.Done():
398411
}
399412

400413
startTime := time.Now()
401-
var err error
402414
if j.afterJobRunsWithPanic != nil {
403415
err = e.callJobWithRecover(j)
404416
} else {

job.go

+20-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ type internalJob struct {
4040
startImmediately bool
4141
stopTime time.Time
4242
// event listeners
43-
afterJobRuns func(jobID uuid.UUID, jobName string)
44-
beforeJobRuns func(jobID uuid.UUID, jobName string)
45-
afterJobRunsWithError func(jobID uuid.UUID, jobName string, err error)
46-
afterJobRunsWithPanic func(jobID uuid.UUID, jobName string, recoverData any)
47-
afterLockError func(jobID uuid.UUID, jobName string, err error)
48-
disabledLocker bool
43+
afterJobRuns func(jobID uuid.UUID, jobName string)
44+
beforeJobRuns func(jobID uuid.UUID, jobName string)
45+
beforeJobRunsSkipIfBeforeFuncErrors func(jobID uuid.UUID, jobName string) error
46+
afterJobRunsWithError func(jobID uuid.UUID, jobName string, err error)
47+
afterJobRunsWithPanic func(jobID uuid.UUID, jobName string, recoverData any)
48+
afterLockError func(jobID uuid.UUID, jobName string, err error)
49+
disabledLocker bool
4950

5051
locker Locker
5152
}
@@ -724,6 +725,19 @@ func BeforeJobRuns(eventListenerFunc func(jobID uuid.UUID, jobName string)) Even
724725
}
725726
}
726727

728+
// BeforeJobRunsSkipIfBeforeFuncErrors is used to listen for when a job is about to run and
729+
// then runs the provided function. If the provided function returns an error, the job will be
730+
// rescheduled and the current run will be skipped.
731+
func BeforeJobRunsSkipIfBeforeFuncErrors(eventListenerFunc func(jobID uuid.UUID, jobName string) error) EventListener {
732+
return func(j *internalJob) error {
733+
if eventListenerFunc == nil {
734+
return ErrEventListenerFuncNil
735+
}
736+
j.beforeJobRunsSkipIfBeforeFuncErrors = eventListenerFunc
737+
return nil
738+
}
739+
}
740+
727741
// AfterJobRuns is used to listen for when a job has run
728742
// without an error, and then run the provided function.
729743
func AfterJobRuns(eventListenerFunc func(jobID uuid.UUID, jobName string)) EventListener {

job_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,13 @@ func TestWithEventListeners(t *testing.T) {
482482
},
483483
ErrEventListenerFuncNil,
484484
},
485+
{
486+
"nil before job runs error listener",
487+
[]EventListener{
488+
BeforeJobRunsSkipIfBeforeFuncErrors(nil),
489+
},
490+
ErrEventListenerFuncNil,
491+
},
485492
}
486493

487494
for _, tt := range tests {

0 commit comments

Comments
 (0)