You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe
I'm very much a beginner at Go so please forgive me if this is a dumb question.
Somewhere in my application I'm creating a Context root. Would it make sense to have some methods on the Scheduler to allow passing in a Context and CancelFunc , so I can pass my root context to the scheduler?
If this makes sense, I can create a PR.
Describe the solution you'd like
Add the methods WithShutdownContext(context context.Context) and WithCancel(cancel context.CancelFunc) on the Scheduler, much like the existing WithLocation(..)
Describe alternatives you've considered
n/a
Additional context
n/a
The text was updated successfully, but these errors were encountered:
I assume you are looking to have the scheduler shutdown if your context cancels?
It wouldn't be as simple as passing a context/cancel with the current design. If you're looking to handle a graceful shutdown, you can do without passing a context/cancel into the scheduler.
Something like:
package main
import (
"context""fmt""log""os""os/signal""syscall""github.com/go-co-op/gocron/v2""github.com/google/uuid"
)
funcmain() {
appCtx, appCancel:=context.WithCancel(context.Background())
deferappCancel()
sig:=make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
scheduler, err:=gocron.NewScheduler()
iferr!=nil {
log.Fatalf("failed to setup scheduler: %s", err.Error())
}
_, err=scheduler.NewJob(
gocron.CronJob("* * * * * *", true),
gocron.NewTask(func() {
panic("boom!")
}),
gocron.WithEventListeners(
gocron.AfterJobRunsWithPanic(
func(jobID uuid.UUID, jobNamestring, recoverDataany) {
fmt.Println("job run panic", jobName, recoverData, jobID)
},
),
),
)
iferr!=nil {
log.Fatalf("failed to setup job: %s", err.Error())
}
scheduler.Start()
select {
case<-appCtx.Done():
case<-sig:
}
// you can configure the timeout for how long the scheduler waits during graceful shutdown// https://pkg.go.dev/github.com/go-co-op/gocron/v2#WithStopTimeout// WithStopTimeout sets the amount of time the Scheduler should wait gracefully for jobs to complete// before returning when StopJobs() or Shutdown() are called. Default: 10 * time.Seconderr=scheduler.Shutdown()
iferr!=nil {
log.Fatalf("there was an error shutting down the scheduler: %s", err.Error())
}
}
Is your feature request related to a problem? Please describe
I'm very much a beginner at Go so please forgive me if this is a dumb question.
Somewhere in my application I'm creating a Context root. Would it make sense to have some methods on the Scheduler to allow passing in a Context and CancelFunc , so I can pass my root context to the scheduler?
If this makes sense, I can create a PR.
Describe the solution you'd like
Add the methods
WithShutdownContext(context context.Context)
andWithCancel(cancel context.CancelFunc)
on the Scheduler, much like the existingWithLocation(..)
Describe alternatives you've considered
n/a
Additional context
n/a
The text was updated successfully, but these errors were encountered: