go-co-op / gocron

Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
MIT License
5.45k stars 303 forks source link

[FEATURE] - OneTime jobs should have the option to be deleted on completion #709

Open rigens opened 5 months ago

rigens commented 5 months ago

Describe the bug

...

To Reproduce

func main() {
    scheduler, err := gocron.NewScheduler(gocron.WithLocation(time.UTC))
    if err != nil {
        log.Fatal(err)
    }

    total := 100

    var wg sync.WaitGroup
    wg.Add(total)

    for i := 0; i < total; i++ {
        runAt := time.Now().UTC().Add(5 * time.Second)
        _, err := scheduler.NewJob(
            gocron.OneTimeJob(gocron.OneTimeJobStartDateTime(runAt)),
            gocron.NewTask(func() {
                log.Printf("running job at %s", time.Now().UTC())
                wg.Done()
            }),
        )
        if err != nil {
            log.Fatal(err)
        }
    }

    scheduler.Start()
    wg.Wait()

    log.Printf("total jobs: %d", len(scheduler.Jobs()))
}

Version

v2.2.9

Expected behavior

scheduler.Jobs() should return an empty slice after completing all OneTime jobs

Additional context

JohnRoesler commented 4 months ago

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 RunNow().

I think this could be changed to a feature request to optionally remove the job when done.

rigens commented 4 months ago

Currently, you can run a one time job, and then if you choose to, can run it manually using RunNow()

Why would I use RunNow() if I can call the corresponding function directly

I don't consider this a bug.

This is a bug, OneTime jobs should be removed from the scheduler after execution (see quartz or apscheduler for example)