remove allservers cache
This commit is contained in:
parent
efc51d5060
commit
692de15a51
|
@ -9,7 +9,6 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tribalwarshelp/shared/cache/allservers"
|
|
||||||
"github.com/tribalwarshelp/shared/models"
|
"github.com/tribalwarshelp/shared/models"
|
||||||
|
|
||||||
phpserialize "github.com/Kichiyaki/go-php-serialize"
|
phpserialize "github.com/Kichiyaki/go-php-serialize"
|
||||||
|
@ -25,17 +24,15 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
db *pg.DB
|
db *pg.DB
|
||||||
allServersCache allservers.Cache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DB *pg.DB
|
DB *pg.DB
|
||||||
AllServersCache allservers.Cache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Attach(c *cron.Cron, cfg Config) error {
|
func Attach(c *cron.Cron, cfg Config) error {
|
||||||
h := &handler{cfg.DB, cfg.AllServersCache}
|
h := &handler{cfg.DB}
|
||||||
if err := h.init(); err != nil {
|
if err := h.init(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -43,7 +40,7 @@ func Attach(c *cron.Cron, cfg Config) error {
|
||||||
if _, err := c.AddFunc("0 * * * *", h.updateServersData); err != nil {
|
if _, err := c.AddFunc("0 * * * *", h.updateServersData); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := c.AddFunc("30 0 * * *", h.updateServerHistories); err != nil {
|
if _, err := c.AddFunc("30 0 * * *", h.updateServerHistory); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := c.AddFunc("30 1 * * *", h.vacuumDatabase); err != nil {
|
if _, err := c.AddFunc("30 1 * * *", h.vacuumDatabase); err != nil {
|
||||||
|
@ -55,7 +52,7 @@ func Attach(c *cron.Cron, cfg Config) error {
|
||||||
go func() {
|
go func() {
|
||||||
h.updateServersData()
|
h.updateServersData()
|
||||||
h.vacuumDatabase()
|
h.vacuumDatabase()
|
||||||
h.updateServerHistories()
|
h.updateServerHistory()
|
||||||
h.updateStats()
|
h.updateStats()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -209,7 +206,6 @@ func (h *handler) getServers() ([]*models.Server, map[string]string, error) {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
go h.allServersCache.Clear()
|
|
||||||
log.Print("Servers loaded!")
|
log.Print("Servers loaded!")
|
||||||
|
|
||||||
return servers, urls, nil
|
return servers, urls, nil
|
||||||
|
@ -243,7 +239,7 @@ func (h *handler) updateServersData() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *handler) updateServerHistories() {
|
func (h *handler) updateServerHistory() {
|
||||||
servers := []*models.Server{}
|
servers := []*models.Server{}
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
t1 := time.Date(now.Year(), now.Month(), now.Day(), 0, 30, 0, 0, time.UTC)
|
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).
|
Where("status = ? AND (history_updated_at < ? OR history_updated_at IS NULL)", models.ServerStatusOpen, t1).
|
||||||
Select()
|
Select()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(errors.Wrap(err, "updateServerHistories"))
|
log.Println(errors.Wrap(err, "updateServerHistory"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -5,7 +5,6 @@ go 1.14
|
||||||
require (
|
require (
|
||||||
github.com/Kichiyaki/go-php-serialize v0.0.0-20200601110855-47b6982acf83
|
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-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/joho/godotenv v1.3.0
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
|
|
24
main.go
24
main.go
|
@ -1,17 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime"
|
"runtime"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/tribalwarshelp/shared/cache/allservers"
|
|
||||||
"github.com/tribalwarshelp/shared/mode"
|
"github.com/tribalwarshelp/shared/mode"
|
||||||
|
|
||||||
_cron "github.com/tribalwarshelp/cron/cron"
|
_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(
|
c := cron.New(cron.WithChain(
|
||||||
cron.SkipIfStillRunning(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))),
|
cron.SkipIfStillRunning(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))),
|
||||||
))
|
))
|
||||||
if err := _cron.Attach(c, _cron.Config{
|
if err := _cron.Attach(c, _cron.Config{
|
||||||
DB: db,
|
DB: db,
|
||||||
AllServersCache: allServersCache,
|
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user