-
-
Notifications
You must be signed in to change notification settings - Fork 313
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
[FEATURE] - OneTime jobs should have the option to be deleted on completion #709
Comments
I don't consider this a bug. Currently, you can run a one time job, and then if you choose to, can run it manually using I think this could be changed to a feature request to optionally remove the job when done. |
Why would I use
This is a bug, OneTime jobs should be removed from the scheduler after execution (see quartz or apscheduler for example) |
For anyone who comes later: you may want to use _, err := scheduler.NewJob(
gocron.OneTimeJob(gocron.OneTimeJobStartDateTime(runAt)),
gocron.NewTask(func() {
log.Printf("running job at %s", time.Now().UTC())
wg.Done()
}),
gocron.WithLimitedRuns(1),
) But please note that there seems to be some subtle difference between the job being fired by the scheduler and directly called using package main
import (
"fmt"
"github.com/go-co-op/gocron/v2"
"time"
)
var s gocron.Scheduler
func remainingJobCount() {
// remove this delay to see the difference
time.Sleep(time.Microsecond * 10)
fmt.Println("there are", len(s.Jobs()), "jobs in the queue")
}
func main() {
var err error
s, err = gocron.NewScheduler()
if err != nil {
fmt.Println(err)
}
s.Start()
j, err := s.NewJob(
gocron.OneTimeJob(gocron.OneTimeJobStartDateTime(time.Now().Add(time.Second))),
gocron.NewTask(remainingJobCount),
gocron.WithLimitedRuns(2),
)
if err != nil {
fmt.Println(err)
}
time.Sleep(time.Second * 2)
err = j.RunNow()
if err != nil {
fmt.Println(err)
}
time.Sleep(time.Second)
remainingJobCount()
} Will result in:
or
But the first one will always be 1. I'm not sure if this is the desired behavior, so I'm not posting this as a issue. |
Describe the bug
...
To Reproduce
Version
v2.2.9
Expected behavior
scheduler.Jobs() should return an empty slice after completing all OneTime jobs
Additional context
The text was updated successfully, but these errors were encountered: