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] - job.LastRun() is in the future #424

Closed
fgm opened this issue Feb 22, 2023 · 4 comments
Closed

[BUG] - job.LastRun() is in the future #424

fgm opened this issue Feb 22, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@fgm
Copy link

fgm commented Feb 22, 2023

Describe the bug

job.LastRun() is in the future.

To Reproduce

Steps to reproduce the behavior:

  1. create job running every "something"
  2. StartAsync
  3. Update it to now run every "something else"
  4. get time.Now(), job.LastRun() : the latter is now "something" in the future in comparison with the former instead of being in the past as it should.

Version

  • 1.18.0 : has the problem
  • 1.17.1: has the problem
  • 1.17.0: does not have the problem

Expected behavior

job.LastRun represents the last time a job was run, which cannot be in the future.

Additional context

This problem was not present in 1.17.0, but appears in 1.17.1. It appears to have been introduced by the #382 fix.

package demo

import (
	"log"
	"testing"
	"time"

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

func TestIssue424(t *testing.T) {
	s := gocron.NewScheduler(time.Local)

	// Start job every second
	job, _ := s.Every(time.Second).Do(func() {})
	s.StartAsync()
	if !s.IsRunning() {
		t.Fatal("cron is not running")
	}
	defer s.Stop()

	if s.Len() != 1 {
		t.Errorf("expected 1 job, found %d", s.Len())
	}

	// Update it to run every 5 seconds.
	fNew := 5 * time.Second
	if _, err := s.Job(job).Every(fNew).Update(); err != nil {
		t.Fatal(err)
	}
	if !s.IsRunning() {
		t.Fatal("cron is no longer running")
	}
	if s.Len() < 1 {
		t.Fatalf("No jobs found")
	}

	job = s.Jobs()[0]
	now, last, actualNext := time.Now().Round(time.Millisecond), job.LastRun().Round(time.Millisecond), job.NextRun().Round(time.Millisecond)
	expectedNext := last.Add(fNew)
	t.Logf("\nnow:      %v\nlast run: %v\nnext run: %v\nexpected: %v", now, last, actualNext, expectedNext)
	if actualNext != expectedNext {
		t.Errorf("Given: %v\nwant: %v\ngot:  %v", fNew, actualNext, expectedNext)
	}
}
@fgm fgm added the bug Something isn't working label Feb 22, 2023
@fgm fgm changed the title [BUG] - Please add descriptive title [BUG] - job.LastRun() is in the future Mar 14, 2023
@mikayzo
Copy link

mikayzo commented Apr 11, 2023

experiencing same issue

@JohnRoesler
Copy link
Contributor

I got a fix in. Mind confirming if it solves the issue you're seeing? https://github.com/go-co-op/gocron/releases/tag/v.1.19.1

@fgm
Copy link
Author

fgm commented Apr 11, 2023

@JohnRoesler Thanks, my test above passes now.

However, you tagged v.1.19.1 instead of v1.19.1 which is problematic for semver. You'll probably want to retag as v1.19.1.

@JohnRoesler
Copy link
Contributor

@fgm thanks for pointing that out. It's fixed now https://github.com/go-co-op/gocron/releases/tag/v1.19.1

If only github releases would ask you, "are you sure you really want to set that release version???"

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