简体   繁体   中英

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

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?

Outside of the framework that u use,

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM