设置Golang API使用gin-gorm在固定时间间隔(每天)更新数据库。

huangapple go评论75阅读模式
英文:

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

问题

我正在使用gin和gorm制作一个银行的golang API。有没有办法在每天的特定时间更新数据库/表中的所有用户(应付利息)?

英文:

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?

答案1

得分: 1

你可以使用Cron来执行定时任务。

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

Gin示例链接:https://github.com/EDDYCJY/go-gin-example

英文:

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

答案2

得分: 0

你可以考虑在你使用的框架之外,使用gocron来执行你的定时任务,并相应地更新数据库。

英文:

Outside of the framework that u use,

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

答案3

得分: 0

你可以使用gocron在特定时间创建一个cron job,并选择运行一个更新操作,选择"*"

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

huangapple
  • 本文由 发表于 2022年8月17日 10:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/73382217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定