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] - Runs task twice #52

Closed
pandurang90 opened this issue Aug 10, 2020 · 35 comments · Fixed by #54
Closed

[BUG] - Runs task twice #52

pandurang90 opened this issue Aug 10, 2020 · 35 comments · Fixed by #54
Assignees
Labels
bug Something isn't working

Comments

@pandurang90
Copy link

Hi everyone, I am trying to integrate gocron in my service. But when I tried to run task it is running it twice.

package services

import (
	"fmt"
	"time"

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

func task() {
	fmt.Println("I am running task.")
}
func main() {
	s1 := gocron.NewScheduler(time.UTC)
	s1.Every(1).Monday().Do(task)
	s1.StartAsync()
        select{}
}
Output:
I am running task.
I am running task.

Is there something that I am missing or is it a bug?

Thanks you very much.

@pandurang90 pandurang90 added the bug Something isn't working label Aug 10, 2020
@pandurang90
Copy link
Author

@Streppel could you help me here?

@Streppel
Copy link
Member

Absolutely, thanks for bringing it out. Will take a look ASAP!

@Streppel Streppel self-assigned this Aug 12, 2020
@Streppel Streppel added 1 - working on it and removed bug Something isn't working labels Aug 12, 2020
@Streppel
Copy link
Member

@pandurang90 just to keep you updated I did some investigations but had to take a field trip this week but I'm back at it now

@ashim-mahara
Copy link

Getting the same issue.

@tomemmerson
Copy link

Also getting this

@tomemmerson
Copy link

tomemmerson commented Aug 22, 2020

Looks to be an issue just with the specific days of the week s1.Every(1).Monday/Tuesday/Wednesday...() .
s1.Every(1).Day() works fine, we are using this as a workaround then checking what day it is in the task if anyone else needs this quick :)

@adriendomoison
Copy link

@Streppel Any release date? :) Just want to know if I need to switch to another method temporarily or if the fix will be released this week. Thanks!

@Streppel
Copy link
Member

Streppel commented Sep 4, 2020

@adriendomoison by this weekend or next week for sure! This week I was pretty busy myself but now we're just finishing up with some month scheduling issues that were happening as well, and we'd be good to go.

@Streppel Streppel added 3 - done bug Something isn't working and removed 1 - working on it labels Sep 8, 2020
@Streppel
Copy link
Member

Streppel commented Sep 8, 2020

We merged a PR that addressed this. Everything looks good from my tests; If you guys could give it a try and report back the result here, that would be greatly appreciated. If needed, we can reopen this.

@jrobin42
Copy link

jrobin42 commented Apr 9, 2021

Hello, this problem still occurs in my program.
The job looks launched twice in parallel, it's quite rare (but it still occurs sometimes) here is a sample of my code :

package messaging

import (
	"fmt"
	"time"

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

func task() {
	fmt.Println("Send message")
}

func scheduleTask() {
	loc, _ := time.LoadLocation("Europe/Paris")
	scheduler := gocron.NewScheduler(loc)
	scheduler.Every(1).Day().At("09:00:00").Do(task())
	scheduler.StartAsync()
}

func main() {
	 scheduleTask()
}

Sometimes, this is my output :

Send message
Send message

I think I found a way by updating the call to my scheduler :

scheduler.Every(1).Day().At("09:00:00").SingletonMode().Do(task())

but it does not resolve the real problem.

Thank you !

@Streppel
Copy link
Member

Streppel commented Apr 9, 2021

@jrobin42 so it happens only sometimes, not always?

@JohnRoesler JohnRoesler reopened this Apr 11, 2021
@jrobin42
Copy link

yes, I did not found a pattern to reproduce this bug, it only happens sometimes

@JohnRoesler
Copy link
Contributor

v1.4.0 is released with a fix. Please let us know if you continue to see this issue occurring.

@KnBrBz
Copy link

KnBrBz commented Apr 26, 2021

Hi, reproduced in such a way

cron := "0 0 * * *"
loc, err := time.LoadLocation("Local")
if err != nil {
	err = errors.Wrap(err, funcTitle)
	return
}
sheduler := gocron.NewScheduler(loc)
if _, err = sheduler.Cron(cron).Do(r.SetNextWriter); err != nil {
	err = errors.Wrap(err, funcTitle)
	return
}
sheduler.StartAsync()

Using for log rotation, to log files were created, instead of one.

First 20210426_235959.log

[INFO]: 2021-04-26T23:59:59.955 - See prev log in file logs\20210426_175434.log
[INFO]: 2021-04-27T00:00:00.005 - See next log in file logs\20210427_000000.log

Second 20210427_000000.log

[INFO]: 2021-04-27T00:00:00.005 - See prev log in file logs\20210426_235959.log

Builded and runned on Win10.
Using v1.5.0

Case '6 0 * * *' worked correct.

@Streppel
Copy link
Member

Thanks for the info @KnBrBz. We'll investigate more. This bug specifically happened using "0 0 * * *" cron notation, right?

@Streppel Streppel reopened this Apr 27, 2021
@KnBrBz
Copy link

KnBrBz commented Apr 27, 2021

Yes, for now will use fix suggested by jrobin42.

Using notation 0 0 * * * in another project for month, but application is working in docker. Sheduler in it creates two job: 0 0 * * * and 0 1 * * *. No such problems so far.

An update: reproduced once more with notation 0 3 * * *, using singletone mode.

@asmaloney
Copy link

asmaloney commented Jun 23, 2021

Edit: User error - see below.

I'm also having this problem when I do the following:

s := gocron.NewScheduler(time.Local)
s.StartAsync()
s.Every(1).Minute().Do(somefunc)

somefunc is called twice each time.

@JohnRoesler
Copy link
Contributor

@asmaloney could you share a bigger example of the code? Where are you running this? When I run it outputs once per minute on a Mac

@asmaloney
Copy link

asmaloney commented Jun 24, 2021

Edit: User error - see below.

Hmmm... yeah, this might be tough. I'm using macOS as well, but running this in a docker container based on golang:1.16-alpine.

I've only just started to use gocron, so there isn't any other gocron code executing.

Here's the code with the function:

func initThis() {
	s := gocron.NewScheduler(time.Local)
	s.StartAsync()
	s.Every(1).Minute().Do(someFunc)
}

func someFunc() {
	fmt.Printf("someFunc\n")
}

It is running once a minute, but "someFunc" is output twice each time.

Not sure if that helps though...

Edit: And the version from my go.mod: github.com/go-co-op/gocron v1.6.2

@asmaloney
Copy link

My apologies for the noise!

Turns out the thing that was calling initThis was being called twice by accident, so I was adding it twice...

User error. 😊

@JohnRoesler
Copy link
Contributor

@asmaloney all good! I've seen and done that before myself 😞 😂

@artemgavrilov
Copy link

#132 (comment)

Hi, faced the same bug in my environment (MacOS + docker). After some debugging, I found that time.AfterFunc on this line triggers few milliseconds before the expected time. It seems that the root of the problem in golang:

@JohnRoesler
Copy link
Contributor

@artemgavrilov thanks for digging and finding those issues! I’m going to read up because this is a really interesting bug.

@tamoyal
Copy link

tamoyal commented Aug 23, 2021

I am also getting this issue. Something like this:

func schedule() {
  s := gocron.NewScheduler(time.UTC)
  // Every 5th minute
  s.Cron("*/5 * * * *").WaitForSchedule().Tag("stuff").Do(func() {
    doSomething()
  })
  s.StartAsync()
}

func doSomething() {
  log.Println("Doing something...")
}

Prints "Doing something" twice. I have confirmed the code to schedule only runs once.

@JohnRoesler
Copy link
Contributor

I added a Dockerfile to golang/go#45582 that reproduces the issue - let's see if we can get some traction on that issue as this appears to be related to Go itself and not our library.

@JohnRoesler
Copy link
Contributor

JohnRoesler commented Aug 25, 2021

I spent some time digging into golang/go#27090 and TL;DR, there are two calls to the clock_gettime to get monotonic and wall clock times which can cause a slight difference in the two times, more so likely in docker. That being said it sounds like it may not be fixed anytime soon or possibly at all.

My thought then is that rather than relying solely on on time.AfterFunc we put in a check to compare the expected next time.Time the job will be run and if time.AfterFunc returns "early" with regard to the wall clock time, we'll be able to catch it.

I put together a branch to test this out on. If anyone can test out their code with this branch and see if you still encounter the issue, that would be extremely helpful!

https://github.com/go-co-op/gocron/tree/afterfunc-catch

@FlowingSPDG
Copy link

Hello! We had same issue where our task runs twice(e.g. 11:59 and 12:00) in docker(golang:1.16 with MacOS).
After testing afterfunc-catch branch, it seems to be fixed!
Thank you for your great work, this is very helpful!

@artemgavrilov
Copy link

Hi, this fix solves the problem for us! Thanks!

@JohnRoesler
Copy link
Contributor

This has been released with v1.7.1

@abdurrahman-osman
Copy link

Hi I have same issue with version v1.18, I am scheduling the job 1 time, but it runs twice.

@KnBrBz
Copy link

KnBrBz commented Dec 19, 2022

Hi I have same issue with version v1.18, I am scheduling the job 1 time, but it runs twice.

Can you provide the code?

@dhovang
Copy link

dhovang commented Feb 24, 2023

I also have this problem, but only sporadically. Like so:

s.Every("5m").Do(func() { ... })
s.StartAsync()

About one time out of four, so every 20 min approx, it will run twice. Other times it will run only once.

@JohnRoesler
Copy link
Contributor

@dhovang can you share more of your code example? I can't recreate the issue on my side.

@JohnRoesler JohnRoesler reopened this Mar 2, 2023
@JohnRoesler
Copy link
Contributor

Closing, please re-open if you have an example to share that's generating this issue. I am unable to reproduce.

@leafney
Copy link

leafney commented Oct 27, 2023

Using the latest version currently, I encountered a situation where the execution occurs twice. After multiple tests, I found that the occurrence of execution twice is sporadic, and I am not sure under what circumstances it occurs.

Starting server at 0.0.0.0:8881...
{"@timestamp":"2023-10-27T16:43:41.435+08:00","caller":"handler/routes.go:25","content":"--running--","level":"info"}

{"@timestamp":"2023-10-27T16:45:00.000+08:00","caller":"handler/routes.go:25","content":"--running--","level":"info"}
{"@timestamp":"2023-10-27T16:45:00.000+08:00","caller":"handler/routes.go:25","content":"--running--","level":"info"}

{"@timestamp":"2023-10-27T16:48:00.000+08:00","caller":"handler/routes.go:25","content":"--running--","level":"info"}
{"@timestamp":"2023-10-27T16:48:00.000+08:00","caller":"handler/routes.go:25","content":"--running--","level":"info"}

my code:

      s.
	Cron("*/3 * * * *").
	SingletonMode().
	WaitForSchedule().
	Do(func() {
		logx.Info("--running--")
	})

However, when I switched to a lower version for testing, I found that the execution twice situation did not occur.

require(
        //github.com/go-co-op/gocron v1.35.2 // indirect
        github.com/go-co-op/gocron v1.16.3
)

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.