Repository: go-co-op/gocron
Pull request #621 (2023-11-14 17:16:52)

add job name to event listners
### What does this do?


### Which issue(s) does this PR fix/relate to?
<!--- Put `Resolves #XXX` here to auto-close the issue that your PR fixes (if such) --->


### List any changes that modify/break current functionality


### Have you included tests for your changes?


### Did you document any new/modified functionality?

- [ ] Updated `example_test.go`
- [ ] Updated `README.md`

### Notes


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/go-co-op/gocron/pull/621?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-co-op) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`66caa1a`)](https://app.codecov.io/gh/go-co-op/gocron/commit/66caa1aabe0fef9558bdd51888fa9b7a8251d24b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-co-op) 96.64% compared to head [(`b686d9c`)](https://app.codecov.io/gh/go-co-op/gocron/pull/621?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-co-op) 96.19%.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##               v2     #621      +/-   ##
==========================================
- Coverage   96.64%   96.19%   -0.46%     
==========================================
  Files           5        5              
  Lines        1103     1103              
==========================================
- Hits         1066     1061       -5     
- Misses         35       40       +5     
  Partials        2        2              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/go-co-op/gocron/pull/621?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-co-op).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-co-op).


Patch:
diff --git a/executor.go b/executor.go
index 5117c40..74597d8 100644
--- a/executor.go
+++ b/executor.go
@@ -344,7 +344,7 @@ func (e *executor) runJob(j internalJob) {
 		}
 		defer func() { _ = lock.Unlock(j.ctx) }()
 	}
-	_ = callJobFuncWithParams(j.beforeJobRuns, j.id)
+	_ = callJobFuncWithParams(j.beforeJobRuns, j.id, j.name)
 
 	select {
 	case <-e.ctx.Done():
@@ -356,8 +356,8 @@ func (e *executor) runJob(j internalJob) {
 
 	err := callJobFuncWithParams(j.function, j.parameters...)
 	if err != nil {
-		_ = callJobFuncWithParams(j.afterJobRunsWithError, j.id, err)
+		_ = callJobFuncWithParams(j.afterJobRunsWithError, j.id, j.name, err)
 	} else {
-		_ = callJobFuncWithParams(j.afterJobRuns, j.id)
+		_ = callJobFuncWithParams(j.afterJobRuns, j.id, j.name)
 	}
 }
diff --git a/job.go b/job.go
index 2f7e928..7302924 100644
--- a/job.go
+++ b/job.go
@@ -34,9 +34,9 @@ type internalJob struct {
 	startTime          time.Time
 	startImmediately   bool
 	// event listeners
-	afterJobRuns          func(jobID uuid.UUID)
-	beforeJobRuns         func(jobID uuid.UUID)
-	afterJobRunsWithError func(jobID uuid.UUID, err error)
+	afterJobRuns          func(jobID uuid.UUID, jobName string)
+	beforeJobRuns         func(jobID uuid.UUID, jobName string)
+	afterJobRunsWithError func(jobID uuid.UUID, jobName string, err error)
 }
 
 // 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
 
-func AfterJobRuns(eventListenerFunc func(jobID uuid.UUID)) EventListener {
+func AfterJobRuns(eventListenerFunc func(jobID uuid.UUID, jobName string)) EventListener {
 	return func(j *internalJob) error {
 		if eventListenerFunc == nil {
 			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 {
 		if eventListenerFunc == nil {
 			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 {
 		if eventListenerFunc == nil {
 			return ErrEventListenerFuncNil


Test patch:
diff --git a/example_test.go b/example_test.go
index 8deef9b..af40a97 100644
--- a/example_test.go
+++ b/example_test.go
@@ -23,7 +23,7 @@ func ExampleAfterJobRuns() {
 		),
 		WithEventListeners(
 			AfterJobRuns(
-				func(jobID uuid.UUID) {
+				func(jobID uuid.UUID, jobName string) {
 					// do something after the job completes
 				},
 			),
@@ -44,7 +44,7 @@ func ExampleAfterJobRunsWithError() {
 		),
 		WithEventListeners(
 			AfterJobRunsWithError(
-				func(jobID uuid.UUID, err error) {
+				func(jobID uuid.UUID, jobName string, err error) {
 					// do something when the job returns an error
 				},
 			),
@@ -65,7 +65,7 @@ func ExampleBeforeJobRuns() {
 		),
 		WithEventListeners(
 			BeforeJobRuns(
-				func(jobID uuid.UUID) {
+				func(jobID uuid.UUID, jobName string) {
 					// do something immediately before the job is run
 				},
 			),
@@ -464,17 +464,17 @@ func ExampleWithEventListeners() {
 		),
 		WithEventListeners(
 			AfterJobRuns(
-				func(jobID uuid.UUID) {
+				func(jobID uuid.UUID, jobName string) {
 					// do something after the job completes
 				},
 			),
 			AfterJobRunsWithError(
-				func(jobID uuid.UUID, err error) {
+				func(jobID uuid.UUID, jobName string, err error) {
 					// do something when the job returns an error
 				},
 			),
 			BeforeJobRuns(
-				func(jobID uuid.UUID) {
+				func(jobID uuid.UUID, jobName string) {
 					// do something immediately before the job is run
 				},
 			),
diff --git a/job_test.go b/job_test.go
index 82a521c..73beccd 100644
--- a/job_test.go
+++ b/job_test.go
@@ -355,30 +355,30 @@ func TestWithEventListeners(t *testing.T) {
 		{
 			"afterJobRuns",
 			[]EventListener{
-				AfterJobRuns(func(_ uuid.UUID) {}),
+				AfterJobRuns(func(_ uuid.UUID, _ string) {}),
 			},
 			nil,
 		},
 		{
 			"afterJobRunsWithError",
 			[]EventListener{
-				AfterJobRunsWithError(func(_ uuid.UUID, _ error) {}),
+				AfterJobRunsWithError(func(_ uuid.UUID, _ string, _ error) {}),
 			},
 			nil,
 		},
 		{
 			"beforeJobRuns",
 			[]EventListener{
-				BeforeJobRuns(func(_ uuid.UUID) {}),
+				BeforeJobRuns(func(_ uuid.UUID, _ string) {}),
 			},
 			nil,
 		},
 		{
 			"multiple event listeners",
 			[]EventListener{
-				AfterJobRuns(func(_ uuid.UUID) {}),
-				AfterJobRunsWithError(func(_ uuid.UUID, _ error) {}),
-				BeforeJobRuns(func(_ uuid.UUID) {}),
+				AfterJobRuns(func(_ uuid.UUID, _ string) {}),
+				AfterJobRunsWithError(func(_ uuid.UUID, _ string, _ error) {}),
+				BeforeJobRuns(func(_ uuid.UUID, _ string) {}),
 			},
 			nil,
 		},
diff --git a/scheduler_test.go b/scheduler_test.go
index 8bbd8db..4173c9d 100644
--- a/scheduler_test.go
+++ b/scheduler_test.go
@@ -1177,7 +1177,7 @@ func TestScheduler_WithEventListeners(t *testing.T) {
 		{
 			"AfterJobRuns",
 			NewTask(func() {}),
-			AfterJobRuns(func(_ uuid.UUID) {
+			AfterJobRuns(func(_ uuid.UUID, _ string) {
 				listenerRunCh <- nil
 			}),
 			true,
@@ -1186,7 +1186,7 @@ func TestScheduler_WithEventListeners(t *testing.T) {
 		{
 			"AfterJobRunsWithError - error",
 			NewTask(func() error { return testErr }),
-			AfterJobRunsWithError(func(_ uuid.UUID, err error) {
+			AfterJobRunsWithError(func(_ uuid.UUID, _ string, err error) {
 				listenerRunCh <- err
 			}),
 			true,
@@ -1195,7 +1195,7 @@ func TestScheduler_WithEventListeners(t *testing.T) {
 		{
 			"AfterJobRunsWithError - no error",
 			NewTask(func() error { return nil }),
-			AfterJobRunsWithError(func(_ uuid.UUID, err error) {
+			AfterJobRunsWithError(func(_ uuid.UUID, _ string, err error) {
 				listenerRunCh <- err
 			}),
 			false,
@@ -1204,7 +1204,7 @@ func TestScheduler_WithEventListeners(t *testing.T) {
 		{
 			"BeforeJobRuns",
 			NewTask(func() {}),
-			BeforeJobRuns(func(_ uuid.UUID) {
+			BeforeJobRuns(func(_ uuid.UUID, _ string) {
 				listenerRunCh <- nil
 			}),
 			true,