高朗的CRON工作

I AM USING IN CRON PKG https://github.com/jasonlvhit/gocron/blob/master/gocron.go

import (
    "fmt"
    "time"

    "github.com/claudiu/gocron"
)

func task() {
    fmt.Println("I am runnning task.", time.Now())
}
func vijay() {
    fmt.Println("I am runnning vijay.", time.Now())
}

func main() {
    go test()

    gocron.Start()
    s := gocron.NewScheduler()
    gocron.Every(5).Seconds().Do(task)
    gocron.Every(10).Seconds().Do(vijay)

    <-s.Start()

}
func test() {
    time.Sleep(20 * time.Second)
    gocron.Clear()
    fmt.Println("All task removed")
}

My problem is after removing all job, my program is still executing

i want to break the exection after removing all jobs

please help me out ,i am not able to find out how to do it , i tried to change the PKG source code also but not able to find out the way to do it

thank you all

First, you're creating a new scheduler, and waiting on it, but using the default scheduler to run your jobs.

Next, you're blocking on the channel returned by the Start() method. Close that channel to unblock the receive operation. This will also exit the main loop in the cron program if you aren't immediately exiting from main.

func main() {
    ch := gocron.Start()
    go test(ch)
    gocron.Every(5).Seconds().Do(task)
    gocron.Every(10).Seconds().Do(vijay)

    <-ch
}
func test(stop chan bool) {
    time.Sleep(20 * time.Second)
    gocron.Clear()
    fmt.Println("All task removed")
    close(stop)
}

which effectively is the same as

func main() {
    gocron.Start()
    gocron.Every(5).Seconds().Do(task)
    gocron.Every(10).Seconds().Do(vijay)

    time.Sleep(20 * time.Second)

    gocron.Clear()
    fmt.Println("All task removed")
}

If you're exiting immediately, it doesn't really matter if you call Clear() first and then stop the scheduler, you can simply exit the program.

JimB rights. But I don't know why do you use gocron methods and the s methods. This example works fine:

package main

import (
    "fmt"
    "time"

    "github.com/claudiu/gocron"
)

func task() {
    fmt.Println("I am runnning task.", time.Now())
}
func vijay() {
    fmt.Println("I am runnning vijay.", time.Now())
}

func main() {
    s := gocron.NewScheduler()
    s.Every(2).Seconds().Do(task)
    s.Every(4).Seconds().Do(vijay)

    sc := s.Start() // keep the channel
    go test(s, sc)  // wait
    <-sc            // it will happens if the channel is closed
}
func test(s *gocron.Scheduler, sc chan bool) {
    time.Sleep(8 * time.Second)
    s.Clear()
    fmt.Println("All task removed")
    close(sc) // close the channel
}