From 7d23acb2fee1e16d0558874ab1fc5bb4c3e2cdd4 Mon Sep 17 00:00:00 2001 From: Stephan Krynauw Date: Thu, 27 Sep 2018 14:42:17 +0200 Subject: [PATCH] move lastRun=time.Now() to before running of the function, otherwise if its a long running function, the next run includes the function's running time --- gocron.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/gocron.go b/gocron.go index e226a6a..bc7eb0c 100644 --- a/gocron.go +++ b/gocron.go @@ -98,8 +98,8 @@ func (j *Job) run() (result []reflect.Value, err error) { for k, param := range params { in[k] = reflect.ValueOf(param) } - result = f.Call(in) j.lastRun = time.Now() + result = f.Call(in) j.scheduleNextRun() return } @@ -198,6 +198,14 @@ func (j *Job) scheduleNextRun() { if j.period != 0 { // translate all the units to the Seconds j.nextRun = j.lastRun.Add(j.period * time.Second) + + //Make sure next run is after current time, otherwise this job will never run again + for { + if j.nextRun.After(time.Now()) { + break; + } + j.nextRun = j.nextRun.Add(j.period * time.Second) + } } else { switch j.unit { case "minutes":