dswfyq6201
2018-10-09 10:38
浏览 30
已采纳

在Golang中使用Gin Gonic和一些调度程序

I have some REST service in Gin Gonic, and I need to do some job every 5 seconds using some scheduler. I've tried rakanalh and gocron but it seems that every code written after initializing crons or gin routes will not execute.

func main() {
    settings.AppSettings = settings.ReadSettings()

    db.InitOracleDataBase()
    OracleEnv, OracleSrv, OracleSes := db.GetOracleDB()
    defer OracleEnv.Close()
    defer OracleSrv.Close()
    defer OracleSes.Close()

    routes.Init()


    gocron.Every(5).Seconds().Do(prOk)
    <-gocron.Start()


}

prOk just prints OK, and it never will be executed, until I comment routes.init(). How it's possible to run both cron and gin routes concurrently?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douguluan5102 2018-10-09 18:36
    已采纳

    You need to execute the gocron inside a goroutine, since the main goroutine will block (I guess routes.Init() starts an HTTP server too?)

    func main() {
        settings.AppSettings = settings.ReadSettings()
    
        db.InitOracleDataBase()
        OracleEnv, OracleSrv, OracleSes := db.GetOracleDB()
        defer OracleEnv.Close()
        defer OracleSrv.Close()
        defer OracleSes.Close()
    
        go func() {
            gocron.Every(5).Seconds().Do(prOk)
            <-gocron.Start()
        }()
    
        routes.Init()
    }
    
    点赞 评论
  • dqndc26628 2018-10-09 17:13

    I am doing something similar, where I cache some API results and refresh cache every few seconds. I do not use anything beyond the standard library though. Here is some sample code.

    func UpdateCache() {
        var lock sync.Mutex
        timer1 := time.NewTicker(time.Second * 10) 
        defer timer1.Stop()
        timer2 := time.NewTicker(time.Second * 5) 
        defer timer2.Stop()
        for {
            /* run forever */
            select {
            case <-timer1.C:
                go func() {
                    lock.Lock()
                    defer lock.Unlock()
                    /* do things I need done every 10 seconds */
                }()
            case <-timer2.C:
                go func() {
                    lock.Lock()
                    defer lock.Unlock()
                    /* do things I need done every 5 seconds */
                }()
            }
        }
    }
    

    And in the main() I have

    go UpdateCache()
    router := gin.Default()
    /* setup all the routes */
    listen := os.Getenv("SERVICE_ADDR")
    router.Run(listen)
    

    I think the problem you are running into is that you start running your routes in routes.Init() and it blocks and never gets to your cron setup.

    点赞 评论

相关推荐 更多相似问题