Skip to content

Commit

Permalink
feat: add WithIdentifier (#753) (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcfreak30 authored Jul 10, 2024
1 parent 9747c90 commit 63a6458
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
ErrWithDistributedElectorNil = fmt.Errorf("gocron: WithDistributedElector: elector must not be nil")
ErrWithDistributedLockerNil = fmt.Errorf("gocron: WithDistributedLocker: locker must not be nil")
ErrWithDistributedJobLockerNil = fmt.Errorf("gocron: WithDistributedJobLocker: locker must not be nil")
ErrWithIdentifierNil = fmt.Errorf("gocron: WithIdentifier: identifier must not be nil")
ErrWithLimitConcurrentJobsZero = fmt.Errorf("gocron: WithLimitConcurrentJobs: limit must be greater than 0")
ErrWithLocationNil = fmt.Errorf("gocron: WithLocation: location must not be nil")
ErrWithLoggerNil = fmt.Errorf("gocron: WithLogger: logger must not be nil")
Expand Down
21 changes: 21 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,27 @@ func ExampleWithGlobalJobOptions() {
// [tag4 tag5 tag6]
}

func ExampleWithIdentifier() {
s, _ := NewScheduler()
defer func() { _ = s.Shutdown() }()

j, _ := s.NewJob(
DurationJob(
time.Second,
),
NewTask(
func(one string, two int) {
fmt.Printf("%s, %d", one, two)
},
"one", 2,
),
WithIdentifier(uuid.MustParse("87b95dfc-3e71-11ef-9454-0242ac120002")),
)
fmt.Println(j.ID())
// Output:
// 87b95dfc-3e71-11ef-9454-0242ac120002
}

func ExampleWithLimitConcurrentJobs() {
_, _ = NewScheduler(
WithLimitConcurrentJobs(
Expand Down
14 changes: 14 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,20 @@ func WithTags(tags ...string) JobOption {
}
}

// WithIdentifier sets the identifier for the job. The identifier
// is used to uniquely identify the job and is used for logging
// and metrics.
func WithIdentifier(id uuid.UUID) JobOption {
return func(j *internalJob) error {
if id == uuid.Nil {
return ErrWithIdentifierNil
}

j.id = id
return nil
}
}

// -----------------------------------------------
// -----------------------------------------------
// ------------- Job Event Listeners -------------
Expand Down
8 changes: 8 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,14 @@ func TestScheduler_NewJobErrors(t *testing.T) {
[]JobOption{WithDistributedJobLocker(nil)},
ErrWithDistributedJobLockerNil,
},
{
"WithIdentifier is nil",
DurationJob(
time.Second,
),
[]JobOption{WithIdentifier(uuid.Nil)},
ErrWithIdentifierNil,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 63a6458

Please sign in to comment.