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 a7cd4d1
Show file tree
Hide file tree
Showing 2 changed files with 33 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
25 changes: 19 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,18 @@ 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.
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

0 comments on commit a7cd4d1

Please sign in to comment.