Skip to content

Commit

Permalink
before script error
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Morelly committed Jan 8, 2025
1 parent abfd9e5 commit 29a9c7b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
16 changes: 14 additions & 2 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,20 @@ func (e *executor) runJob(j internalJob, jIn jobIn) {
}
defer func() { _ = lock.Unlock(j.ctx) }()
}
_ = callJobFuncWithParams(j.beforeJobRuns, j.id, j.name)

// execute before func, unless user has specified BeforeJobRunsWithError
var beforeFunc any

beforeFunc = j.beforeJobRuns

if j.beforeJobRunsWithError != nil {
beforeFunc = j.beforeJobRunsWithError
}

err := callJobFuncWithParams(beforeFunc, j.id, j.name)
if err != nil {
return
}

e.sendOutForRescheduling(&jIn)
select {
Expand All @@ -398,7 +411,6 @@ func (e *executor) runJob(j internalJob, jIn jobIn) {
}

startTime := time.Now()
var err error
if j.afterJobRunsWithPanic != nil {
err = e.callJobWithRecover(j)
} else {
Expand Down
26 changes: 20 additions & 6 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ type internalJob struct {
startImmediately bool
stopTime time.Time
// event listeners
afterJobRuns func(jobID uuid.UUID, jobName string)
beforeJobRuns func(jobID uuid.UUID, jobName string)
afterJobRunsWithError func(jobID uuid.UUID, jobName string, err error)
afterJobRunsWithPanic func(jobID uuid.UUID, jobName string, recoverData any)
afterLockError func(jobID uuid.UUID, jobName string, err error)
disabledLocker bool
afterJobRuns func(jobID uuid.UUID, jobName string)
beforeJobRuns func(jobID uuid.UUID, jobName string)
beforeJobRunsWithError func(jobID uuid.UUID, jobName string) error
afterJobRunsWithError func(jobID uuid.UUID, jobName string, err error)
afterJobRunsWithPanic func(jobID uuid.UUID, jobName string, recoverData any)
afterLockError func(jobID uuid.UUID, jobName string, err error)
disabledLocker bool

locker Locker
}
Expand Down Expand Up @@ -724,6 +725,19 @@ func BeforeJobRuns(eventListenerFunc func(jobID uuid.UUID, jobName string)) Even
}
}

// BeforeJobRunsError is used to listen for when a job is about to run and
// then run the provided function and returns an error if any.
// This function overwrites BeforeJobRuns.
func BeforeJobRunsError(eventListenerFunc func(jobID uuid.UUID, jobName string) error) EventListener {
return func(j *internalJob) error {
if eventListenerFunc == nil {
return ErrEventListenerFuncNil
}
j.beforeJobRunsWithError = eventListenerFunc
return nil
}
}

// AfterJobRuns is used to listen for when a job has run
// without an error, and then run the provided function.
func AfterJobRuns(eventListenerFunc func(jobID uuid.UUID, jobName string)) EventListener {
Expand Down
7 changes: 7 additions & 0 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,13 @@ func TestWithEventListeners(t *testing.T) {
},
ErrEventListenerFuncNil,
},
{
"nil before job runs error listener",
[]EventListener{
BeforeJobRunsError(nil),
},
ErrEventListenerFuncNil,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 29a9c7b

Please sign in to comment.