How to cancel async job that already running? #204
Answered
by
JohnRoesler
Aldiwildan77
asked this question in
Q&A
-
Hello, I have got a project to run a scheduler using this package. But I got confused about the scheduler cancellation, is there any example of how to use that? *this is my example code. I wanna cancel if the task inside this job got an error so the job itself will be cancelled. func main() {
loc, _ := time.LoadLocation(utils.TIMEZONE_LOCAL)
s := gocron.NewScheduler(loc)
s.StartAsync()
s.Every(10).Second().Do(RecoverTransactionScheduler)
}
func RecoverTransactionScheduler() {
// run task
// if error occur here, stop this job itself
} |
Beta Was this translation helpful? Give feedback.
Answered by
JohnRoesler
Jul 6, 2021
Replies: 1 comment 3 replies
-
Yes - you can stop the job by removing it. https://pkg.go.dev/github.com/go-co-op/gocron#Scheduler.Remove As noted, you could also add a context to the job function to allow stopping the job if it's long running and you've removed it. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Aldiwildan77
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @Aldiwildan77
Yes - you can stop the job by removing it. https://pkg.go.dev/github.com/go-co-op/gocron#Scheduler.Remove
As noted, you could also add a context to the job function to allow stopping the job if it's long running and you've removed it.