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 e9e532d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/file_formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
uses: actions/checkout@v4
- name: verify example_test.go
run: |
grep "^func " example_test.go | sort -c
grep "^func [a-z-A-Z]" example_test.go | sort -c
23 changes: 21 additions & 2 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,27 @@ 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 {
e.sendOutForRescheduling(&jIn)

select {
case e.jobsOutCompleted <- j.id:
case <-e.ctx.Done():
}

return
}

e.sendOutForRescheduling(&jIn)
select {
Expand All @@ -398,7 +418,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 e9e532d

Please sign in to comment.