jasonlvhit / gocron

A Golang Job Scheduling Package.
BSD 2-Clause "Simplified" License
3.44k stars 347 forks source link

Can gocron work with gin #50

Open huyntsgs opened 6 years ago

huyntsgs commented 6 years ago

I want to use gocron with gin router. But it seems that gin router and gocron block each other. If i start goncron before router.Run, the genData will be done, but can not get response from /ex/getdata. If i start router.Run before gocron.Start(), I can get response from /ex/getdata but genData job is not executed.

func main() {
    data := &Data{}
    router := gin.Default()
    router.GET("/ex/getdata", getData)

    gocron.Every(1).Second().Do(genData)
    <-gocron.Start()
    router.Run(":8888")
}
icobani commented 6 years ago

I have same problem.

huyntsgs commented 6 years ago

I backed with github.com/robfig/cron and all work fine

laczar commented 5 years ago

This one gives a nice answer im using right now. https://stackoverflow.com/questions/52719015/using-gin-gonic-and-some-scheduler-in-golang

ariefannur commented 5 years ago

i have same problem

ariefannur commented 5 years ago

I backed with github.com/robfig/cron and all work fine

could you mind to give sample implementation about it?

huyntsgs commented 5 years ago

Please refer my sample at https://github.com/huyntsgs/exercise/blob/master/main.go#L215 and https://github.com/huyntsgs/exercise/blob/master/main.go#L63

ariefannur commented 5 years ago

how about every (time) get data from db and send broadcast notif with gcm. @huyntsgs