remove allservers cache

This commit is contained in:
Dawid Wysokiński 2020-08-07 10:21:19 +02:00 committed by Kichiyaki
parent efc51d5060
commit 692de15a51
3 changed files with 8 additions and 35 deletions

View File

@ -9,7 +9,6 @@ import (
"sync"
"time"
"github.com/tribalwarshelp/shared/cache/allservers"
"github.com/tribalwarshelp/shared/models"
phpserialize "github.com/Kichiyaki/go-php-serialize"
@ -25,17 +24,15 @@ const (
)
type handler struct {
db *pg.DB
allServersCache allservers.Cache
db *pg.DB
}
type Config struct {
DB *pg.DB
AllServersCache allservers.Cache
DB *pg.DB
}
func Attach(c *cron.Cron, cfg Config) error {
h := &handler{cfg.DB, cfg.AllServersCache}
h := &handler{cfg.DB}
if err := h.init(); err != nil {
return err
}
@ -43,7 +40,7 @@ func Attach(c *cron.Cron, cfg Config) error {
if _, err := c.AddFunc("0 * * * *", h.updateServersData); err != nil {
return err
}
if _, err := c.AddFunc("30 0 * * *", h.updateServerHistories); err != nil {
if _, err := c.AddFunc("30 0 * * *", h.updateServerHistory); err != nil {
return err
}
if _, err := c.AddFunc("30 1 * * *", h.vacuumDatabase); err != nil {
@ -55,7 +52,7 @@ func Attach(c *cron.Cron, cfg Config) error {
go func() {
h.updateServersData()
h.vacuumDatabase()
h.updateServerHistories()
h.updateServerHistory()
h.updateStats()
}()
@ -209,7 +206,6 @@ func (h *handler) getServers() ([]*models.Server, map[string]string, error) {
return nil, nil, err
}
go h.allServersCache.Clear()
log.Print("Servers loaded!")
return servers, urls, nil
@ -243,7 +239,7 @@ func (h *handler) updateServersData() {
}
}
func (h *handler) updateServerHistories() {
func (h *handler) updateServerHistory() {
servers := []*models.Server{}
now := time.Now()
t1 := time.Date(now.Year(), now.Month(), now.Day(), 0, 30, 0, 0, time.UTC)
@ -252,7 +248,7 @@ func (h *handler) updateServerHistories() {
Where("status = ? AND (history_updated_at < ? OR history_updated_at IS NULL)", models.ServerStatusOpen, t1).
Select()
if err != nil {
log.Println(errors.Wrap(err, "updateServerHistories"))
log.Println(errors.Wrap(err, "updateServerHistory"))
return
}

1
go.mod
View File

@ -5,7 +5,6 @@ go 1.14
require (
github.com/Kichiyaki/go-php-serialize v0.0.0-20200601110855-47b6982acf83
github.com/go-pg/pg/v10 v10.0.0-beta.2
github.com/go-redis/redis/v8 v8.0.0-beta.7
github.com/joho/godotenv v1.3.0
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1

24
main.go
View File

@ -1,17 +1,12 @@
package main
import (
"context"
"log"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/cache/allservers"
"github.com/tribalwarshelp/shared/mode"
_cron "github.com/tribalwarshelp/cron/cron"
@ -43,28 +38,11 @@ func main() {
}
}()
redisClient := redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_HOST") + ":" + os.Getenv("REDIS_PORT"),
Password: os.Getenv("REDIS_PASSWORD"),
})
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
if err := redisClient.Ping(ctx).Err(); err != nil {
log.Fatal(errors.Wrap(err, "cannot connect to redis"))
}
cancel()
defer func() {
if err := redisClient.Close(); err != nil {
log.Fatal(err)
}
}()
allServersCache := allservers.New(redisClient)
c := cron.New(cron.WithChain(
cron.SkipIfStillRunning(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))),
))
if err := _cron.Attach(c, _cron.Config{
DB: db,
AllServersCache: allServersCache,
DB: db,
}); err != nil {
log.Fatal(err)
}