gocron icon indicating copy to clipboard operation
gocron copied to clipboard

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

Open rigens opened this issue 10 months ago • 2 comments

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

rigens avatar Apr 16 '24 11:04 rigens

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.

JohnRoesler avatar May 06 '24 15:05 JohnRoesler

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)

rigens avatar May 10 '24 05:05 rigens