Skip to content

Commit

Permalink
clarify context docs
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler committed Jan 23, 2025
1 parent ce204f9 commit 8df2bbf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
10 changes: 6 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,9 @@ func ExampleNewTask() {
gocron.DurationJob(time.Second),
gocron.NewTask(
func(ctx context.Context) {
// gocron will pass in a context to the job and will cancel on shutdown.
// this allows you to listen for and handle cancellation within your job.
// gocron will pass in a context (either the default Job context, or one provided via WithContext)
// to the job and will cancel the context on shutdown.
// This allows you to listen for and handle cancellation within your job.
},
),
)
Expand Down Expand Up @@ -626,8 +627,9 @@ func ExampleWithContext() {
),
gocron.NewTask(
func(ctx context.Context) {
// gocron will pass in a context to the job and will cancel on shutdown.
// this allows you to listen for and handle cancellation within your job.
// gocron will pass in a context (either the default Job context, or one provided via WithContext)
// to the job and will cancel the context on shutdown.
// This allows you to listen for and handle cancellation within your job.
},
),
gocron.WithContext(ctx),
Expand Down
6 changes: 4 additions & 2 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ type Task func() task

// NewTask provides the job's task function and parameters.
// If you set the first argument of your Task func to be a context.Context,
// gocron will pass in a context to the job and will cancel on shutdown.
// gocron will pass in a context (either the default Job context, or one provided via WithContext)
// to the job and will cancel the context on shutdown.
// This allows you to listen for and handle cancellation within your job.
func NewTask(function any, parameters ...any) Task {
return func() task {
Expand Down Expand Up @@ -709,7 +710,8 @@ func WithIdentifier(id uuid.UUID) JobOption {

// WithContext sets the parent context for the job
// If you set the first argument of your Task func to be a context.Context,
// gocron will pass in a context to the job and will cancel on shutdown.
// gocron will pass in a context (either the default Job context, or one provided via WithContext)
// to the job and will cancel the context on shutdown.
// This allows you to listen for and handle cancellation within your job.
func WithContext(ctx context.Context) JobOption {
return func(j *internalJob, _ time.Time) error {
Expand Down
3 changes: 2 additions & 1 deletion scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type Scheduler interface {
// definition when the Scheduler is started. If the Scheduler is already running
// the job will be scheduled when the Scheduler is started.
// If you set the first argument of your Task func to be a context.Context,
// gocron will pass in a context to the job and will cancel on shutdown.
// gocron will pass in a context (either the default Job context, or one provided via WithContext)
// to the job and will cancel the context on shutdown.
// This allows you to listen for and handle cancellation within your job.
NewJob(JobDefinition, Task, ...JobOption) (Job, error)
// RemoveByTags removes all jobs that have at least one of the provided tags.
Expand Down

0 comments on commit 8df2bbf

Please sign in to comment.