Skip to content

Commit

Permalink
Reload configs every 5 minutes to make sure we pick up updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonhasacat committed Feb 8, 2024
1 parent ce98c13 commit 7837db3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/bot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Run() {
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
go IntervalRefreshAll(s)
go IntervalReloadConfigs()
go func() {
for {
QueueListen(s)
Expand Down
19 changes: 14 additions & 5 deletions internal/bot/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"log"
"os"
"time"
)

type ServerConfig struct {
Expand Down Expand Up @@ -40,15 +41,15 @@ type ConditionConfig struct {
}

func LoadAllServerConfigOrPanic(configPath string) map[string]ServerConfig {
configs, err := LoadAllServerConfig(configPath)
cfgs, err := LoadAllServerConfig(configPath)
if err != nil {
log.Printf(err.Error())
}
return configs
return cfgs
}

func LoadAllServerConfig(configPath string) (map[string]ServerConfig, error) {
configs := make(map[string]ServerConfig, 0)
cfgs := make(map[string]ServerConfig, 0)
files, err := os.ReadDir(configPath)
if err != nil {
return nil, errors.New("failed to load server configs")
Expand All @@ -60,10 +61,10 @@ func LoadAllServerConfig(configPath string) (map[string]ServerConfig, error) {
log.Printf(err.Error())
return nil, nil
}
configs[cfg.ID] = *cfg
cfgs[cfg.ID] = *cfg
}
}
return configs, nil
return cfgs, nil
}

func LoadServerConfig(configPath string) (*ServerConfig, error) {
Expand All @@ -84,6 +85,14 @@ func LoadServerConfig(configPath string) (*ServerConfig, error) {

var configs = LoadAllServerConfigOrPanic(config.CONFIG_PATH)

func IntervalReloadConfigs() {
for {
log.Print("Reloading server configs")
configs = LoadAllServerConfigOrPanic(config.CONFIG_PATH)
time.Sleep(5 * time.Hour)
}
}

func GetServerConfig(id string) *ServerConfig {
cfg, ok := configs[id]
if !ok {
Expand Down

0 comments on commit 7837db3

Please sign in to comment.