I am doing something like this:
package main
import (
"fmt"
"time"
"github.com/jasonlvhit/gocron"
)
func jobs(quit <-chan bool) {
for {
select {
case <-quit:
return
default:
//cron jobs
g := gocron.NewScheduler()
g.Every(1).Second().Do(stuff)
<-g.Start()
}
}
}
func stuff() {
fmt.Println("doing job")
}
func main() {
q := make(chan bool)
go jobs(q)
time.Sleep(3 * time.Second)
//to quit the goroutine
q <- true
close(q)
fmt.Println("main")
}
I'm trying to stop the gocrons by killing the goroutine by closing the channel but I'm not able to stop gocron jobs. I am getting output
doing job
doing job
doing job
doing job
doing job
doing job
doing job
.
.
Instead of
doing job
doing job
doing job
main
What am I doing wrong? Is there any better solution to stop gocron jobs?