Skip to content

Commit 63a6458

Browse files
authored
feat: add WithIdentifier (#753) (#754)
1 parent 9747c90 commit 63a6458

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

errors.go

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var (
3838
ErrWithDistributedElectorNil = fmt.Errorf("gocron: WithDistributedElector: elector must not be nil")
3939
ErrWithDistributedLockerNil = fmt.Errorf("gocron: WithDistributedLocker: locker must not be nil")
4040
ErrWithDistributedJobLockerNil = fmt.Errorf("gocron: WithDistributedJobLocker: locker must not be nil")
41+
ErrWithIdentifierNil = fmt.Errorf("gocron: WithIdentifier: identifier must not be nil")
4142
ErrWithLimitConcurrentJobsZero = fmt.Errorf("gocron: WithLimitConcurrentJobs: limit must be greater than 0")
4243
ErrWithLocationNil = fmt.Errorf("gocron: WithLocation: location must not be nil")
4344
ErrWithLoggerNil = fmt.Errorf("gocron: WithLogger: logger must not be nil")

example_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,27 @@ func ExampleWithGlobalJobOptions() {
694694
// [tag4 tag5 tag6]
695695
}
696696

697+
func ExampleWithIdentifier() {
698+
s, _ := NewScheduler()
699+
defer func() { _ = s.Shutdown() }()
700+
701+
j, _ := s.NewJob(
702+
DurationJob(
703+
time.Second,
704+
),
705+
NewTask(
706+
func(one string, two int) {
707+
fmt.Printf("%s, %d", one, two)
708+
},
709+
"one", 2,
710+
),
711+
WithIdentifier(uuid.MustParse("87b95dfc-3e71-11ef-9454-0242ac120002")),
712+
)
713+
fmt.Println(j.ID())
714+
// Output:
715+
// 87b95dfc-3e71-11ef-9454-0242ac120002
716+
}
717+
697718
func ExampleWithLimitConcurrentJobs() {
698719
_, _ = NewScheduler(
699720
WithLimitConcurrentJobs(

job.go

+14
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,20 @@ func WithTags(tags ...string) JobOption {
610610
}
611611
}
612612

613+
// WithIdentifier sets the identifier for the job. The identifier
614+
// is used to uniquely identify the job and is used for logging
615+
// and metrics.
616+
func WithIdentifier(id uuid.UUID) JobOption {
617+
return func(j *internalJob) error {
618+
if id == uuid.Nil {
619+
return ErrWithIdentifierNil
620+
}
621+
622+
j.id = id
623+
return nil
624+
}
625+
}
626+
613627
// -----------------------------------------------
614628
// -----------------------------------------------
615629
// ------------- Job Event Listeners -------------

scheduler_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,14 @@ func TestScheduler_NewJobErrors(t *testing.T) {
774774
[]JobOption{WithDistributedJobLocker(nil)},
775775
ErrWithDistributedJobLockerNil,
776776
},
777+
{
778+
"WithIdentifier is nil",
779+
DurationJob(
780+
time.Second,
781+
),
782+
[]JobOption{WithIdentifier(uuid.Nil)},
783+
ErrWithIdentifierNil,
784+
},
777785
}
778786

779787
for _, tt := range tests {

0 commit comments

Comments
 (0)