[英]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?
有什么方法可以让我每天在数据库/表的某个时间更新所有用户(应付利息)?
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.