Skip to content
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

[BUG] - Restarting a stopped scheduler will run a job multiple times #367

Closed
pma9 opened this issue Aug 3, 2022 · 1 comment · Fixed by #368
Closed

[BUG] - Restarting a stopped scheduler will run a job multiple times #367

pma9 opened this issue Aug 3, 2022 · 1 comment · Fixed by #368
Labels
bug Something isn't working

Comments

@pma9
Copy link

pma9 commented Aug 3, 2022

Describe the bug

If you start a scheduler with a job that says run every second, everything will work fine. If you stop the scheduler and then start the scheduler again without removing or adding jobs, the existing job will run twice. If you stop and restart the scheduler again, it will run the job three times every second and so on and so forth.

To Reproduce

package main

import (
	"fmt"
	"time"

	"github.com/go-co-op/gocron"
)

var task = func() {
	fmt.Println(time.Now())
}

func main() {
	var s *gocron.Scheduler = gocron.NewScheduler(time.UTC)

	fmt.Println("Adding job to cron and starting for 5 seconds")
	job, _ := s.Every(1).Seconds().Do(task)
	s.StartAsync()
	time.Sleep(5 * time.Second)

	fmt.Println("Stopping and sleeping for 5 seconds")
	s.Stop()
	fmt.Printf("Job run count: %v\n", job.RunCount())
	time.Sleep(5 * time.Second)

	fmt.Println("Starting and sleeping for 10 seconds")
	s.StartAsync()
	time.Sleep(10 * time.Second)
	fmt.Printf("Job run count: %v\n", job.RunCount())
}

Output:

Adding job to cron and starting for 5 seconds
2022-08-03 10:35:21.4868 -0700 PDT m=+0.000390376
2022-08-03 10:35:22.487998 -0700 PDT m=+1.001595084
2022-08-03 10:35:23.489039 -0700 PDT m=+2.002643126
2022-08-03 10:35:24.490316 -0700 PDT m=+3.003927001
2022-08-03 10:35:25.491499 -0700 PDT m=+4.005116793
Stopping and sleeping for 5 seconds
Job run count: 5
Starting and sleeping for 10 seconds
2022-08-03 10:35:31.499926 -0700 PDT m=+10.013584418
2022-08-03 10:35:32.490224 -0700 PDT m=+11.003889626
2022-08-03 10:35:32.500545 -0700 PDT m=+11.014210459
2022-08-03 10:35:33.491454 -0700 PDT m=+12.005126168
2022-08-03 10:35:33.500982 -0700 PDT m=+12.014654918
2022-08-03 10:35:34.492875 -0700 PDT m=+13.006553876
2022-08-03 10:35:34.502021 -0700 PDT m=+13.015699959
2022-08-03 10:35:35.493796 -0700 PDT m=+14.007481626
2022-08-03 10:35:35.5031 -0700 PDT m=+14.016785709
2022-08-03 10:35:36.495603 -0700 PDT m=+15.009295584
2022-08-03 10:35:36.503454 -0700 PDT m=+15.017147209
2022-08-03 10:35:37.496707 -0700 PDT m=+16.010406584
2022-08-03 10:35:37.505217 -0700 PDT m=+16.018916418
2022-08-03 10:35:38.49778 -0700 PDT m=+17.011486209
2022-08-03 10:35:38.506152 -0700 PDT m=+17.019858626
2022-08-03 10:35:39.498899 -0700 PDT m=+18.012612668
2022-08-03 10:35:39.507125 -0700 PDT m=+18.020838293
2022-08-03 10:35:40.500325 -0700 PDT m=+19.014045168
2022-08-03 10:35:40.507488 -0700 PDT m=+19.021208376
Job run count: 24

Version

Tested on v1.6.2 and the latest v1.16.1

Expected behavior

Expected to have a job run count about 14-16 after running the above test script. Stopping and then restarting a scheduler should continue to only run a specific job once per set interval.

@pma9 pma9 added the bug Something isn't working label Aug 3, 2022
pma9 added a commit to pma9/gocron that referenced this issue Aug 3, 2022
@ftamhar
Copy link

ftamhar commented Nov 29, 2022

this problem reappeared starting in version 1.17.1

Btw, I think we need func like SoftStop and HardStop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants