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] - Multiple times not working #322

Closed
nandra opened this issue Apr 8, 2022 · 3 comments
Closed

[BUG] - Multiple times not working #322

nandra opened this issue Apr 8, 2022 · 3 comments
Assignees
Labels
accepted We plan to work this issue bug Something isn't working

Comments

@nandra
Copy link

nandra commented Apr 8, 2022

Describe the bug

With this test code:

package main

import (
	"fmt"
	"time"

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

func main() {
	s := gocron.NewScheduler(time.UTC)
	dayOn, _ := s.Every(1).Day().At("20:31;20:34;20:36").Do(func() {
		fmt.Println("on", time.Now())
	})

	dayOff, _ := s.Every(1).Day().At("20:33;20:35;20:37").Do(func() {
		fmt.Println("off", time.Now())
	})
	s.StartAsync()

	for {
		time.Sleep(10 * time.Second)
		fmt.Println("on:", dayOn.ScheduledAtTimes(), dayOn.NextRun())
		fmt.Println("off:", dayOff.ScheduledAtTimes(), dayOff.NextRun())
	}
}

I get call only on first time occurrence not others (see log):

go run main.go
on: [20:31 20:34 20:36] 2022-04-08 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on 2022-04-08 22:31:00.000364307 +0200 CEST m=+14.149554983
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-08 20:33:00 +0000 UTC
off 2022-04-08 22:33:00.0002875 +0200 CEST m=+134.149478172
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-09 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-09 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-09 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-09 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-09 20:33:00 +0000 UTC
on: [20:31 20:34 20:36] 2022-04-09 20:31:00 +0000 UTC
off: [20:33 20:35 20:37] 2022-04-09 20:33:00 +0000 UTC

Version

latest master: b96aa93

Expected behavior

I should see printed on in all times and off in all times.

@nandra nandra added the bug Something isn't working label Apr 8, 2022
@Streppel Streppel self-assigned this Apr 20, 2022
@Streppel Streppel added the accepted We plan to work this issue label Apr 20, 2022
@ayush71994
Copy link

ayush71994 commented Jul 18, 2022

+1. Facing this same issue.
Setting multiple times every day results in only one execution.

Sample code

	_, schedErr := sched.Every(1).Day().At("2:45").At("12:15").Do(func(){
//something})

Seeing only one execution

Currently scheduling it separately seems to work as an alternative

	_, schedErr := sched.Every(1).Day().At("2:45").Do(someFunc())
	if schedErr != nil {
		panic(schedErr)
	}
	_, schedErr = sched.Every(1).Day().At("12:15").Do(someFunc())
	if schedErr != nil {
		panic(schedErr)
	}
	sched.StartAsync()

@TawsifKarim
Copy link

I started to use this package and experiencing issues with a lot of methods.. But one cool thing is, there is a method named Cron() like below

job, _ := s.Cron("37-41 * * * *").Do(func() {    //run At every minute from 37 through 41
		fmt.Println("yes im running", time.Now().Format("03:04:05"))
	})

use this https://crontab.guru/ website and set your desired timings and copy paste the desired expression inside this method. I hope you would be good to go...

@xwjdsh
Copy link

xwjdsh commented Nov 2, 2022

I dug into this issue, below is my code snippet and I also add some logs to the gocron package.

package main

import (
	"fmt"
	"time"

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

func main() {
	s := gocron.NewScheduler(time.UTC)
	dayOn, _ := s.Every(1).Day().At("18:54;18:55;18:56").Do(func() {
		fmt.Println("dayOn Task", time.Now().UTC().Format(time.RFC3339))
	})

	s.StartAsync()

	fmt.Println("dayOn Task", dayOn.ScheduledAtTimes())
	for {
		now := time.Now().UTC().Format(time.RFC3339)
		fmt.Printf("[%s] dayOn nextRun: %s\n", now, dayOn.NextRun().Format(time.RFC3339))
		time.Sleep(10 * time.Second)
	}
}

The log is below, and you can see, the task executed at 18:54 and 18:55, and then skipped 18:56, goes to the next day.

% go run main.go
lastRun:  2022-11-02T18:53:37Z
lastRunDayPlusJobAtTime:  2022-11-02T18:54:00Z
shouldRunToday true
dayOn Task [18:54 18:55 18:56]
[2022-11-02T18:53:37Z] dayOn nextRun: 2022-11-02T18:54:00Z
[2022-11-02T18:53:47Z] dayOn nextRun: 2022-11-02T18:54:00Z
[2022-11-02T18:53:57Z] dayOn nextRun: 2022-11-02T18:54:00Z
lastRun:  2022-11-02T18:54:00Z
lastRunDayPlusJobAtTime:  2022-11-02T18:55:00Z
shouldRunToday true
dayOn Task 2022-11-02T18:54:00Z
[2022-11-02T18:54:07Z] dayOn nextRun: 2022-11-02T18:55:00Z
[2022-11-02T18:54:17Z] dayOn nextRun: 2022-11-02T18:55:00Z
[2022-11-02T18:54:27Z] dayOn nextRun: 2022-11-02T18:55:00Z
[2022-11-02T18:54:37Z] dayOn nextRun: 2022-11-02T18:55:00Z
[2022-11-02T18:54:47Z] dayOn nextRun: 2022-11-02T18:55:00Z
[2022-11-02T18:54:57Z] dayOn nextRun: 2022-11-02T18:55:00Z
lastRun:  2022-11-02T18:54:00Z
lastRunDayPlusJobAtTime:  2022-11-02T18:55:00Z
shouldRunToday true
dayOn Task 2022-11-02T18:55:00Z
lastRun:  2022-11-02T18:55:00Z
lastRunDayPlusJobAtTime:  2022-11-02T18:56:00Z
111 1667415300 2022-11-03T03:55:00+09:00
111 1667415300 2022-11-03T03:55:00+09:00
lastRun:  2022-11-02T18:56:00Z
shouldRunToday false
[2022-11-02T18:55:07Z] dayOn nextRun: 2022-11-03T18:54:00Z

The reason is that the code added in the calculateDays implementation is in #159, but I read the comments from #52, the problem is solved by another way, then this code should be removed.

		// handle occasional occurrence of job running to quickly / too early such that last run was within a second of now
		lastRunUnix, nowUnix := job.LastRun().Unix(), s.now().Unix()
		if lastRunUnix == nowUnix || lastRunUnix == nowUnix-1 || lastRunUnix == nowUnix+1 {
			fmt.Println(111, lastRunUnix, time.Unix(lastRunUnix, 0).Format(time.RFC3339))
			fmt.Println(111, nowUnix, time.Unix(nowUnix, 0).Format(time.RFC3339))
			lastRun = lastRunDayPlusJobAtTime
			fmt.Println("lastRun: ", lastRun.Format(time.RFC3339))
		}

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

No branches or pull requests

5 participants