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] - Scheduler.Clear() not real remove all jobs #133

Closed
36huo opened this issue Mar 4, 2021 · 2 comments · Fixed by #134
Closed

[BUG] - Scheduler.Clear() not real remove all jobs #133

36huo opened this issue Mar 4, 2021 · 2 comments · Fixed by #134
Labels
bug Something isn't working

Comments

@36huo
Copy link

36huo commented Mar 4, 2021

Describe the bug

Scheduler.Clear() not real remove all jobs

To Reproduce

Steps to reproduce the behavior:

  1. use Scheduler.Clear()
  2. add new job
  3. start immediately
  4. the old job run once again

Code

package main

import (
	"fmt"
	"time"
	"log"
	"github.com/go-co-op/gocron"
)

var task = func(a string) {
	log.Println(a)
}

func main() {
	s := gocron.NewScheduler(time.UTC)
	_, _ = s.Every(5).Second().Do(task,"job1")
	_, _ = s.Every(5).Second().Do(task,"job2")
	fmt.Printf("there is %d jobs.\n",len(s.Jobs()))
	s.StartAsync()
	time.Sleep(2*time.Second)
	s.Stop()
	s.Clear()
	fmt.Printf("After clear,there is %d jobs.\n",len(s.Jobs()))
	log.Println("cleared")
	_, _ = s.Every(1).Second().Do(task,"job3")
	fmt.Printf("After add new job,there is %d jobs.\n",len(s.Jobs()))
	s.StartAsync()
	time.Sleep(10*time.Second)
}

Output

there is 2 jobs.
2021/03/04 23:42:05 job2
2021/03/04 23:42:05 job1
After clear,there is 0 jobs.
2021/03/04 23:42:07 cleared
After add new job,there is 1 jobs.
2021/03/04 23:42:07 job3
2021/03/04 23:42:08 job3
2021/03/04 23:42:09 job3
2021/03/04 23:42:10 job1
2021/03/04 23:42:10 job2
2021/03/04 23:42:10 job3
2021/03/04 23:42:11 job3
2021/03/04 23:42:12 job3
2021/03/04 23:42:13 job3
2021/03/04 23:42:14 job3
2021/03/04 23:42:15 job3
2021/03/04 23:42:16 job3

If remove all jobs manual ,it is right.

    for _, v := range s.Jobs(){
        s.RemoveByReference(v)
    }

And suggest add StartAtTime for job

func (j *Job) StartAtTime(t time.Time) {
        j.Lock()
        defer j.Unlock()
        j.startAtTime = t
	j.startsImmediately = false
}
@36huo 36huo added the bug Something isn't working label Mar 4, 2021
@JohnRoesler
Copy link
Contributor

Thank you for pointing out the bug! The clear function neglects to actually stop the job's from running.

@JohnRoesler
Copy link
Contributor

@36huo mind opening a feature request to make start at accept time.Time?

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.

2 participants