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

How to make stop Scheduler? #172

Open
meteoricfarm opened this issue Aug 25, 2021 · 1 comment
Open

How to make stop Scheduler? #172

meteoricfarm opened this issue Aug 25, 2021 · 1 comment

Comments

@meteoricfarm
Copy link

meteoricfarm commented Aug 25, 2021

Dear Contributors,

I've tested gocron to understand its behavior.
I've got obtained a result that I cannot understand.
I reckon that "gocron" Scheduler will be stopped if I assign a value to the channel that obtained from Start() function's return value.
But, jobs still running even if "Scheduler Finished" is executed.

What do I have to do to stop the Scheduler?

func stuff() {
    defer func() {
        fmt.Printf("doing the job: 1\n")
    }()
}

func jobs(quit <-chan bool) {
    g := gocron.NewScheduler()
    g.Every(1).Second().Do(stuff)
    stopped := g.Start()

    for {
        select {
            case <-quit:
                stopped <- true
            case <-stopped:
                fmt.Println("Scheduler Finished")
                return
        }
    }
}

func TestGoCron(t *testing.T) {
    q := make(chan bool)
    go jobs(q)

    time.Sleep(5 * time.Second)

    q <- true

    time.Sleep(5 * time.Second)
    close(q)
    fmt.Println("main")
}

----------------------- Result -----------------------
doing the job: 1
doing the job: 1
doing the job: 1
doing the job: 1
Scheduler Finished
doing the job: 1
doing the job: 1
doing the job: 1
doing the job: 1
doing the job: 1
doing the job: 1
main

@JohnRoesler
Copy link
Contributor

@sathya0803 this repo isn’t maintained anymore. Please checkout https://GitHub.com/go-co-op/gocron which is an active fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants