Home > Mobile >  Setting up Golang API to update its database on an interval (everyday) using gin-gorm
Setting up Golang API to update its database on an interval (everyday) using gin-gorm

Time:08-17

I am making a banking golang API using gin and gorm. Is there any way where I can update t all the users every day (interest payable) at a certain time on the database/table?

CodePudding user response:

Outside of the framework that u use,

you can consider using gocron to do your scheduled task and update the db accordingly

CodePudding user response:

You can create a cron job with gocron at a certain time and run an update selecting "*":

func updateAllUsers() {
    db.Model(&user).Select("*").Update(User{})
}

func runCronJobs() {
    s := gocron.NewScheduler(time.UTC)

    s.Every(1).Day().At("10:30;08:00").Do(func() {
        updateAllUsers()
    })

    s.StartBlocking()
}

CodePudding user response:

You can use Cron to do scheduled tasks.

GitHub: https://github.com/robfig/cron

And Gin example see: https://github.com/EDDYCJY/go-gin-example

  • Related