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] - Out of sync, multiple executions of same task #174

Closed
alexandre-dias-cj opened this issue May 6, 2021 · 5 comments
Closed

[BUG] - Out of sync, multiple executions of same task #174

alexandre-dias-cj opened this issue May 6, 2021 · 5 comments
Labels
bug Something isn't working

Comments

@alexandre-dias-cj
Copy link

alexandre-dias-cj commented May 6, 2021

When using a 30s interval scheduler, after a few hours it runs multiple times the same task, per second

Example snippet

var partialUpdatesTag = []string{"partial"}
var partialIndexer *gocron.Scheduler

func indexSomeData(){
	log.Println("Indexing data...")
	time.Sleep(1 * time.Second) 
}

func runPartialReindex() {
	log.Println("Runing partial...")
	stopPartial()
	indexSomeData()//dummy function won't take more than 200ms to complete
	startPartial()
}

func schedulePartialReindex() {
	log.Println("Partial reindex scheduler running each 30 seconds")
	partialIndexer = gocron.NewScheduler(time.Local)
	partialIndexer.Every(30 * time.Second).Tag(partialUpdatesTag...).SingletonMode().Do(runPartialReindex)
	partialIndexer.StartAsync()
}

func startPartial() {
	if partialIndexer == nil {
		partialIndexer = gocron.NewScheduler(time.Local)
	}
	if !partialIndexer.IsRunning() {
		partialIndexer.Clear() //make sure the old scheduler is cleared and no settings are kept
		for _, tag := range partialUpdatesTag {
			partialIndexer.RemoveByTag(tag)
		}
		partialIndexer = gocron.NewScheduler(time.Local)
		partialIndexer.Every(30 * time.Second).Tag(partialUpdatesTag...).SingletonMode().Do(runPartialReindex)
		time.Sleep(30 * time.Second) //we want to delay 30s start
		partialIndexer.StartAsync()
	}
}
func stopPartial() {
	if partialIndexer == nil {
		partialIndexer = gocron.NewScheduler(time.Local)
	}
	if partialIndexer.IsRunning() {
		partialIndexer.Stop()
		partialIndexer.WaitForSchedule().Stop() //make sure no runing tasks
	}
}

It wasn't expected that the runPartialReindex function was run more than once per 30s interval
I did try with partialIndexer.Every(30).Second() and partialIndexer.Every(30).Seconds() but the described problem is even worse
Not sure if it matters but I'm using go mod.

@alexandre-dias-cj alexandre-dias-cj added the bug Something isn't working label May 6, 2021
@Streppel
Copy link
Member

Streppel commented May 7, 2021

Thanks for the detailed report, @alexandre-dias-cj! We will take a look at it!

@JohnRoesler
Copy link
Contributor

@alexandre-dias-cj how are you running this? local machine, docker, etc.

@alexandre-dias-cj
Copy link
Author

Docker, forgot to mention it

@JohnRoesler
Copy link
Contributor

hey @alexandre-dias-cj some time is freeing up and I'm getting back to looking at these bugs. Could you help me understand a little bit more about this example, it looks to me like your jobs are essentially recursively calling the function that stops and starts the scheduler?

It's hard for me to dig into to exactly what is happening here because this isn't really using the scheduler as it was designed.

I would propose right off the bat that, that we redesign your functions to not be starting and stopping the scheduler and instead leave the scheduler running.

For example:

scheduler := gocron.NewScheduler(time.Local)
scheduler.Every(30).Seconds().Tag(partialUpdatesTag...).SingletonMode().Do(runPartialReindex)

func runPartialReindex() {
	log.Println("Runing partial...")
	indexSomeData() //dummy function won't take more than 200ms to complete
}

That's maybe simplistic and I'm probably missing something in the reasoning behind your implementation. Help me understand and we can work more on what might be happening!

@alexandre-dias-cj
Copy link
Author

Sure, we actually had 3 schedulers running, there are 2 different functions which interact with one single data repository.
That repository is incremented or decremented using one 30 scheduler but 24h on 24h we're doing a full-rebuild of that data and we don't want the partial indexer running for obvious reasons.

We did actually ended up rebuilding our service to get rid of the problem.

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

No branches or pull requests

3 participants