add job name to event listners (#621)

* add job name to event listners

* missed the examples

* missed executor
This commit is contained in:
John Roesler
2023-11-14 11:39:27 -06:00
committed by GitHub
parent 66caa1aabe
commit 8c628fae2d
5 changed files with 25 additions and 25 deletions
+6 -6
View File
@@ -23,7 +23,7 @@ func ExampleAfterJobRuns() {
), ),
WithEventListeners( WithEventListeners(
AfterJobRuns( AfterJobRuns(
func(jobID uuid.UUID) { func(jobID uuid.UUID, jobName string) {
// do something after the job completes // do something after the job completes
}, },
), ),
@@ -44,7 +44,7 @@ func ExampleAfterJobRunsWithError() {
), ),
WithEventListeners( WithEventListeners(
AfterJobRunsWithError( AfterJobRunsWithError(
func(jobID uuid.UUID, err error) { func(jobID uuid.UUID, jobName string, err error) {
// do something when the job returns an error // do something when the job returns an error
}, },
), ),
@@ -65,7 +65,7 @@ func ExampleBeforeJobRuns() {
), ),
WithEventListeners( WithEventListeners(
BeforeJobRuns( BeforeJobRuns(
func(jobID uuid.UUID) { func(jobID uuid.UUID, jobName string) {
// do something immediately before the job is run // do something immediately before the job is run
}, },
), ),
@@ -464,17 +464,17 @@ func ExampleWithEventListeners() {
), ),
WithEventListeners( WithEventListeners(
AfterJobRuns( AfterJobRuns(
func(jobID uuid.UUID) { func(jobID uuid.UUID, jobName string) {
// do something after the job completes // do something after the job completes
}, },
), ),
AfterJobRunsWithError( AfterJobRunsWithError(
func(jobID uuid.UUID, err error) { func(jobID uuid.UUID, jobName string, err error) {
// do something when the job returns an error // do something when the job returns an error
}, },
), ),
BeforeJobRuns( BeforeJobRuns(
func(jobID uuid.UUID) { func(jobID uuid.UUID, jobName string) {
// do something immediately before the job is run // do something immediately before the job is run
}, },
), ),
+3 -3
View File
@@ -344,7 +344,7 @@ func (e *executor) runJob(j internalJob) {
} }
defer func() { _ = lock.Unlock(j.ctx) }() defer func() { _ = lock.Unlock(j.ctx) }()
} }
_ = callJobFuncWithParams(j.beforeJobRuns, j.id) _ = callJobFuncWithParams(j.beforeJobRuns, j.id, j.name)
select { select {
case <-e.ctx.Done(): case <-e.ctx.Done():
@@ -356,8 +356,8 @@ func (e *executor) runJob(j internalJob) {
err := callJobFuncWithParams(j.function, j.parameters...) err := callJobFuncWithParams(j.function, j.parameters...)
if err != nil { if err != nil {
_ = callJobFuncWithParams(j.afterJobRunsWithError, j.id, err) _ = callJobFuncWithParams(j.afterJobRunsWithError, j.id, j.name, err)
} else { } else {
_ = callJobFuncWithParams(j.afterJobRuns, j.id) _ = callJobFuncWithParams(j.afterJobRuns, j.id, j.name)
} }
} }
+6 -6
View File
@@ -34,9 +34,9 @@ type internalJob struct {
startTime time.Time startTime time.Time
startImmediately bool startImmediately bool
// event listeners // event listeners
afterJobRuns func(jobID uuid.UUID) afterJobRuns func(jobID uuid.UUID, jobName string)
beforeJobRuns func(jobID uuid.UUID) beforeJobRuns func(jobID uuid.UUID, jobName string)
afterJobRunsWithError func(jobID uuid.UUID, err error) afterJobRunsWithError func(jobID uuid.UUID, jobName string, err error)
} }
// stop is used to stop the job's timer and cancel the context // stop is used to stop the job's timer and cancel the context
@@ -507,7 +507,7 @@ func WithTags(tags ...string) JobOption {
type EventListener func(*internalJob) error type EventListener func(*internalJob) error
func AfterJobRuns(eventListenerFunc func(jobID uuid.UUID)) EventListener { func AfterJobRuns(eventListenerFunc func(jobID uuid.UUID, jobName string)) EventListener {
return func(j *internalJob) error { return func(j *internalJob) error {
if eventListenerFunc == nil { if eventListenerFunc == nil {
return ErrEventListenerFuncNil return ErrEventListenerFuncNil
@@ -517,7 +517,7 @@ func AfterJobRuns(eventListenerFunc func(jobID uuid.UUID)) EventListener {
} }
} }
func AfterJobRunsWithError(eventListenerFunc func(jobID uuid.UUID, err error)) EventListener { func AfterJobRunsWithError(eventListenerFunc func(jobID uuid.UUID, jobName string, err error)) EventListener {
return func(j *internalJob) error { return func(j *internalJob) error {
if eventListenerFunc == nil { if eventListenerFunc == nil {
return ErrEventListenerFuncNil return ErrEventListenerFuncNil
@@ -527,7 +527,7 @@ func AfterJobRunsWithError(eventListenerFunc func(jobID uuid.UUID, err error)) E
} }
} }
func BeforeJobRuns(eventListenerFunc func(jobID uuid.UUID)) EventListener { func BeforeJobRuns(eventListenerFunc func(jobID uuid.UUID, jobName string)) EventListener {
return func(j *internalJob) error { return func(j *internalJob) error {
if eventListenerFunc == nil { if eventListenerFunc == nil {
return ErrEventListenerFuncNil return ErrEventListenerFuncNil
+6 -6
View File
@@ -355,30 +355,30 @@ func TestWithEventListeners(t *testing.T) {
{ {
"afterJobRuns", "afterJobRuns",
[]EventListener{ []EventListener{
AfterJobRuns(func(_ uuid.UUID) {}), AfterJobRuns(func(_ uuid.UUID, _ string) {}),
}, },
nil, nil,
}, },
{ {
"afterJobRunsWithError", "afterJobRunsWithError",
[]EventListener{ []EventListener{
AfterJobRunsWithError(func(_ uuid.UUID, _ error) {}), AfterJobRunsWithError(func(_ uuid.UUID, _ string, _ error) {}),
}, },
nil, nil,
}, },
{ {
"beforeJobRuns", "beforeJobRuns",
[]EventListener{ []EventListener{
BeforeJobRuns(func(_ uuid.UUID) {}), BeforeJobRuns(func(_ uuid.UUID, _ string) {}),
}, },
nil, nil,
}, },
{ {
"multiple event listeners", "multiple event listeners",
[]EventListener{ []EventListener{
AfterJobRuns(func(_ uuid.UUID) {}), AfterJobRuns(func(_ uuid.UUID, _ string) {}),
AfterJobRunsWithError(func(_ uuid.UUID, _ error) {}), AfterJobRunsWithError(func(_ uuid.UUID, _ string, _ error) {}),
BeforeJobRuns(func(_ uuid.UUID) {}), BeforeJobRuns(func(_ uuid.UUID, _ string) {}),
}, },
nil, nil,
}, },
+4 -4
View File
@@ -1177,7 +1177,7 @@ func TestScheduler_WithEventListeners(t *testing.T) {
{ {
"AfterJobRuns", "AfterJobRuns",
NewTask(func() {}), NewTask(func() {}),
AfterJobRuns(func(_ uuid.UUID) { AfterJobRuns(func(_ uuid.UUID, _ string) {
listenerRunCh <- nil listenerRunCh <- nil
}), }),
true, true,
@@ -1186,7 +1186,7 @@ func TestScheduler_WithEventListeners(t *testing.T) {
{ {
"AfterJobRunsWithError - error", "AfterJobRunsWithError - error",
NewTask(func() error { return testErr }), NewTask(func() error { return testErr }),
AfterJobRunsWithError(func(_ uuid.UUID, err error) { AfterJobRunsWithError(func(_ uuid.UUID, _ string, err error) {
listenerRunCh <- err listenerRunCh <- err
}), }),
true, true,
@@ -1195,7 +1195,7 @@ func TestScheduler_WithEventListeners(t *testing.T) {
{ {
"AfterJobRunsWithError - no error", "AfterJobRunsWithError - no error",
NewTask(func() error { return nil }), NewTask(func() error { return nil }),
AfterJobRunsWithError(func(_ uuid.UUID, err error) { AfterJobRunsWithError(func(_ uuid.UUID, _ string, err error) {
listenerRunCh <- err listenerRunCh <- err
}), }),
false, false,
@@ -1204,7 +1204,7 @@ func TestScheduler_WithEventListeners(t *testing.T) {
{ {
"BeforeJobRuns", "BeforeJobRuns",
NewTask(func() {}), NewTask(func() {}),
BeforeJobRuns(func(_ uuid.UUID) { BeforeJobRuns(func(_ uuid.UUID, _ string) {
listenerRunCh <- nil listenerRunCh <- nil
}), }),
true, true,