File tree 4 files changed +44
-0
lines changed
4 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 38
38
ErrWithDistributedElectorNil = fmt .Errorf ("gocron: WithDistributedElector: elector must not be nil" )
39
39
ErrWithDistributedLockerNil = fmt .Errorf ("gocron: WithDistributedLocker: locker must not be nil" )
40
40
ErrWithDistributedJobLockerNil = fmt .Errorf ("gocron: WithDistributedJobLocker: locker must not be nil" )
41
+ ErrWithIdentifierNil = fmt .Errorf ("gocron: WithIdentifier: identifier must not be nil" )
41
42
ErrWithLimitConcurrentJobsZero = fmt .Errorf ("gocron: WithLimitConcurrentJobs: limit must be greater than 0" )
42
43
ErrWithLocationNil = fmt .Errorf ("gocron: WithLocation: location must not be nil" )
43
44
ErrWithLoggerNil = fmt .Errorf ("gocron: WithLogger: logger must not be nil" )
Original file line number Diff line number Diff line change @@ -694,6 +694,27 @@ func ExampleWithGlobalJobOptions() {
694
694
// [tag4 tag5 tag6]
695
695
}
696
696
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
+
697
718
func ExampleWithLimitConcurrentJobs () {
698
719
_ , _ = NewScheduler (
699
720
WithLimitConcurrentJobs (
Original file line number Diff line number Diff line change @@ -610,6 +610,20 @@ func WithTags(tags ...string) JobOption {
610
610
}
611
611
}
612
612
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
+
613
627
// -----------------------------------------------
614
628
// -----------------------------------------------
615
629
// ------------- Job Event Listeners -------------
Original file line number Diff line number Diff line change @@ -774,6 +774,14 @@ func TestScheduler_NewJobErrors(t *testing.T) {
774
774
[]JobOption {WithDistributedJobLocker (nil )},
775
775
ErrWithDistributedJobLockerNil ,
776
776
},
777
+ {
778
+ "WithIdentifier is nil" ,
779
+ DurationJob (
780
+ time .Second ,
781
+ ),
782
+ []JobOption {WithIdentifier (uuid .Nil )},
783
+ ErrWithIdentifierNil ,
784
+ },
777
785
}
778
786
779
787
for _ , tt := range tests {
You can’t perform that action at this time.
0 commit comments