Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add WithIdentifier #754

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
JohnRoesler marked this conversation as resolved.
Show resolved Hide resolved
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