1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
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)
}
|