简体   繁体   English

使用 gin-gorm 设置 Golang API 以每隔一段时间(每天)更新其数据库

[英]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.我正在使用 gin 和 gorm 制作银行 golang API。 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您可以考虑使用gocron来执行您的计划任务并相应地更新数据库

You can create a cron job with gocron at a certain time and run an update selecting "*":您可以在特定时间使用gocron创建一个 cron 作业并选择“*”运行更新:

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.您可以使用 Cron 执行计划任务。

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

And Gin example see: https://github.com/EDDYCJY/go-gin-example和杜松子酒的例子见: https://github.com/EDDYCJY/go-gin-example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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