2
votes

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

2 Answers

5
votes

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.

5
votes

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()
}