forked from Dri0m/flashpoint-submission-system
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautounfreezer.go
103 lines (88 loc) · 2.73 KB
/
autounfreezer.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package service
import (
"context"
"fmt"
"github.com/FlashpointProject/flashpoint-submission-system/constants"
"github.com/FlashpointProject/flashpoint-submission-system/utils"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"sync"
"time"
)
func (s *SiteService) RunAutounfreezer(logger *logrus.Entry, ctx context.Context, wg *sync.WaitGroup, dataPacksPath, frozenPacksPath, deletedPacksPath string, doNotUnfreezeGameList []string) {
defer wg.Done()
l := logger.WithField("serviceName", "autounfreezer")
defer l.Info("autounfreezer stopped")
ticker := time.NewTicker(time.Hour * 12)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
l.Info("context cancelled, stopping autounfreezer")
return
case <-ticker.C:
loop := func() {
ctx := context.WithValue(ctx, utils.CtxKeys.Log, l)
dbs, err := s.pgdal.NewSession(ctx)
if err != nil {
l.Error(err)
return
}
defer func() {
err = dbs.Rollback()
if err != nil {
l.Error(err)
}
}()
games, err := s.pgdal.GetFrozenGames(dbs)
if err != nil {
l.Error(err)
return
}
ageThreshold := time.Now().Add(-time.Hour * 24 * 365 * 3)
for _, game := range games {
if slices.Contains(doNotUnfreezeGameList, game.GameID) {
l.Infof("game %s with release date '%s' is on the do-not-unfreeze list and will be skipped", game.GameID, game.ReleaseDate)
continue
}
releaseTime, err := parseDate(game.ReleaseDate)
if err != nil {
l.Errorf("game %s has unexpected release date format '%s' and will be skipped", game.GameID, game.ReleaseDate)
continue
}
if releaseTime.Before(ageThreshold) {
l.Infof("game %s with release date '%s' will be unfrozen", game.GameID, game.ReleaseDate)
err = s.UnfreezeGame(ctx, game.GameID, constants.SystemID, dataPacksPath, frozenPacksPath, deletedPacksPath)
if err != nil {
l.Error(err)
return
}
}
}
}
loop()
}
}
}
// parseDate parses date string to up to day resolution.
// If the day is missing, then it is assumed it is the last day of the month.
// If the month is also missing, then it is assumed it is the last day of the year.
func parseDate(dateStr string) (time.Time, error) {
var layouts = []string{
"2006", // yyyy
"2006-01", // yyyy-mm
"2006-01-02", // yyyy-mm-dd
}
for _, layout := range layouts {
if t, err := time.Parse(layout, dateStr); err == nil {
switch layout {
case "2006": // Move to the last day of the year
t = time.Date(t.Year(), time.December, 31, 0, 0, 0, 0, time.UTC)
case "2006-01": // Move to the last day of the month
t = t.AddDate(0, 1, -1)
}
return t, nil
}
}
return time.Time{}, fmt.Errorf("invalid date format")
}