package crons import ( "apjournal/internal/database/repos" "context" "os" "strconv" "time" "log/slog" ) var ( log = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})) ) const ( CheckBurnTimeKey = "check_burn_time_key" ) type Cron struct { ctx context.Context repo repos.FullRepo } func NewCron( ctx context.Context, repo repos.FullRepo, ) *Cron { return &Cron{ ctx: ctx, repo: repo, } } func (c *Cron) UpdateDefaultsFloat64(defaults map[string]float64) (map[string]float64, error) { dm, err := c.repo.DBGetDefaultsMap() if err != nil { return defaults, err } for k, _ := range defaults { sosValue, ok := dm[k] if ok { value, err := strconv.ParseFloat(sosValue, 64) if err != nil { // log err continue } defaults[k] = value } } return defaults, nil } func (c *Cron) StartCronJobs() { // check system_options for time defaults := map[string]float64{ CheckBurnTimeKey: 5.0, } defaults, err := c.UpdateDefaultsFloat64(defaults) if err != nil { panic(err) } go c.BurnTicker( time.Minute * time.Duration(defaults[CheckBurnTimeKey])) } func (c *Cron) BurnTicker(interval time.Duration) { funcname := "BurnTicker" ticker := time.NewTicker(interval) for { select { case <-c.ctx.Done(): log.Info("cron stopped by ctx.Done", "func", funcname) return case <-ticker.C: c.BurnTimeUpdate() } } } func (c *Cron) BurnTimeUpdate() { // funcname := "BurnTimeUpdate" // get all user scores // if burn time < now // halv the score // move the burn time to 24h from now (using same hours, minutes, seconds) }