2

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?

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
mondayguy
  • 973
  • 2
  • 12
  • 34

2 Answers2

7

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()
}
Raggaer
  • 3,244
  • 8
  • 36
  • 67
5

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.

Mad Wombat
  • 14,490
  • 14
  • 73
  • 109