Add scheduler module to handle daily user verification updates
This commit is contained in:
parent
d1e14db639
commit
4119431e61
|
|
@ -9,9 +9,12 @@ import (
|
|||
"github.com/Caknoooo/go-gin-clean-starter/modules/product"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/tenant"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/scheduler"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/providers"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/script"
|
||||
"github.com/samber/do"
|
||||
"gorm.io/gorm"
|
||||
|
||||
// "github.com/common-nighthawk/go-figure"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
|
@ -61,6 +64,8 @@ func main() {
|
|||
}
|
||||
|
||||
server := gin.Default()
|
||||
db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB)
|
||||
scheduler.Start(db)
|
||||
|
||||
// Atur trusted proxies, misal hanya localhost dan IP proxy tertentu
|
||||
server.SetTrustedProxies([]string{"127.0.0.1"})
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
package scheduler
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
"github.com/robfig/cron/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Start(db *gorm.DB) {
|
||||
c := cron.New()
|
||||
// Setiap hari jam 00:00
|
||||
_, err := c.AddFunc("0 0 * * *", func() {
|
||||
if err := db.Model(&entities.User{}).Where("is_verified = ?", true).Update("is_verified", false).Error; err != nil {
|
||||
log.Println("Failed to update user verification:", err)
|
||||
} else {
|
||||
log.Println("All users set is_verified to false at", time.Now())
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
log.Println("Failed to schedule cron job:", err)
|
||||
}
|
||||
c.Start()
|
||||
}
|
||||
Loading…
Reference in New Issue