go-co-op / gocron

Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
MIT License
5.7k stars 308 forks source link

[BUG] - gocron: job not found #656

Closed m-s-sh closed 10 months ago

m-s-sh commented 10 months ago

Describe the bug

When I add a new job (let's say j) and after that calling j.NextRun(), it fails unpredictable with ErrJobNotFound.

To Reproduce

Steps to reproduce the behavior:

func TestNextRun(t *testing.T) {
    clock := clockwork.NewFakeClock()
    sch, err := gocron.NewScheduler(gocron.WithClock(clock))
    require.NoError(t, err)
    for i := 0; i < 10; i++ {
        var c int
        j, err := sch.NewJob(
            gocron.DurationJob(time.Second),
            gocron.NewTask(func() error {
                c++
                t.Log("c", c)
                return nil
            }),
            gocron.WithName("calcJob"),
            gocron.WithSingletonMode(gocron.LimitModeReschedule),
        )
        require.NoError(t, err)
        sch.Start()
        r, err := j.NextRun()
        require.NoError(t, err, "i=%d", i)
        t.Log("nextRun", r)
    }
}

Version

v2.1.2

Expected behavior

I expect not to fail with ErrJobNotFound, when I added job few lines above.

JohnRoesler commented 10 months ago

@mihailstefanov thanks for reporting this! An issue with not properly waiting for the new job to be instantiated before returning. I'll have a fix up soon.