Skip to content

Commit

Permalink
Merge pull request #334 from Zibbp/viper-load-retry
Browse files Browse the repository at this point in the history
fix(config): retry viper load
  • Loading branch information
Zibbp authored Dec 27, 2023
2 parents 35cad5d + 22d7389 commit 2a9ebe8
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.aarch64
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ RUN mkdir -p /usr/share/fonts/ && chmod a+rX /usr/share/fonts/
RUN mv /tmp/Inter-Regular.otf /usr/share/fonts/Inter.otf && fc-cache -f -v

# Install fallback fonts for chat rendering
RUN apt-get install xfonts-terminus fonts-inconsolata fonts-dejavu fonts-dejavu-extra fonts-noto fonts-noto-cjk fonts-font-awesome fonts-noto-extra fonts-noto-core -y
RUN apt-get install xfonts-terminus fonts-inconsolata fonts-dejavu fonts-dejavu-extra fonts-noto fonts-noto-cjk fonts-font-awesome fonts-noto-extra fonts-noto-core procps -y

RUN chmod 644 /usr/share/fonts/* && chmod -R a+rX /usr/share/fonts

Expand Down
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var (

func Run() error {

config.NewConfig()
config.NewConfig(true)

configDebug := viper.GetBool("debug")
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
Expand Down
2 changes: 1 addition & 1 deletion cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func main() {

// initializte main program config
// this needs to be removed in the future to decouple the worker from the server
serverConfig.NewConfig()
serverConfig.NewConfig(false)

logger := zerolog.New(os.Stdout).With().Timestamp().Logger()

Expand Down
4 changes: 4 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export FONTCONFIG_CACHE=/var/cache/fontconfig

su-exec abc /opt/app/ganymede-api &
api_pid=$!

# delay 5 seconds to wait for api to start
sleep 5

su-exec abc /opt/app/ganymede-worker &
worker_pid=$!

Expand Down
56 changes: 42 additions & 14 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
Expand All @@ -27,10 +28,8 @@ type Conf struct {
Debug bool `json:"debug"`
LiveCheckInterval int `json:"live_check_interval_seconds"`
VideoCheckInterval int `json:"video_check_interval_minutes"`
ActiveQueueItems int `json:"active_queue_items"`
OAuthEnabled bool `json:"oauth_enabled"`
RegistrationEnabled bool `json:"registration_enabled"`
DBSeeded bool `json:"db_seeded"`
Parameters struct {
TwitchToken string `json:"twitch_token"`
VideoConvert string `json:"video_convert"`
Expand Down Expand Up @@ -75,7 +74,7 @@ type ProxyListItem struct {
Header string `json:"header"`
}

func NewConfig() {
func NewConfig(refresh bool) {
configLocation := "/data"
configName := "config"
configType := "json"
Expand All @@ -88,10 +87,8 @@ func NewConfig() {
viper.SetDefault("debug", false)
viper.SetDefault("live_check_interval_seconds", 300)
viper.SetDefault("video_check_interval_minutes", 180)
viper.SetDefault("active_queue_items", 2)
viper.SetDefault("oauth_enabled", false)
viper.SetDefault("registration_enabled", true)
viper.SetDefault("db_seeded", false)
viper.SetDefault("parameters.video_convert", "-c:v copy -c:a copy")
viper.SetDefault("parameters.chat_render", "-h 1440 -w 340 --framerate 30 --font Inter --font-size 13")
viper.SetDefault("parameters.streamlink_live", "--twitch-low-latency,--twitch-disable-hosting")
Expand Down Expand Up @@ -134,19 +131,43 @@ func NewConfig() {

if _, err := os.Stat(configPath); os.IsNotExist(err) {
log.Info().Msgf("config file not found at %s, creating new one", configPath)
err := viper.SafeWriteConfigAs(configPath)
if err != nil {
log.Panic().Err(err).Msg("error creating config file")
retries := 10
for i := 0; i < retries; i++ {
err := viper.SafeWriteConfigAs(configPath)
if err == nil {
log.Info().Msgf("config file created")
break
}
log.Error().Err(err).Msgf("error creating config file (attempt %d/%d)", i+1, retries)
if i < retries-1 {
log.Info().Msgf("retrying in 1 second")
time.Sleep(1 * time.Second)
} else {
log.Panic().Err(err).Msg("error creating config file")
}
}
} else {
log.Info().Msgf("config file found at %s, loading", configPath)
err := viper.ReadInConfig()
retries := 10
for i := 0; i < retries; i++ {
err := viper.ReadInConfig()
if err == nil {
log.Info().Msgf("config file loaded: %s", viper.ConfigFileUsed())
break
}
log.Error().Err(err).Msgf("error loading config (attempt %d/%d)", i+1, retries)
if i < retries-1 {
log.Info().Msgf("retrying in 1 second")
time.Sleep(1 * time.Second)
} else {
log.Panic().Err(err).Msg("error loading config")
}
}
// Rewrite config file to apply new variables and remove old values
refreshConfig(configPath)
log.Debug().Msgf("config file loaded: %s", viper.ConfigFileUsed())
if err != nil {
log.Panic().Err(err).Msg("error reading config file")
if refresh {
refreshConfig(configPath)
}
log.Debug().Msgf("config file loaded: %s", viper.ConfigFileUsed())
}
}

Expand All @@ -163,7 +184,6 @@ func (s *Service) GetConfig(c echo.Context) (*Conf, error) {

return &Conf{
RegistrationEnabled: viper.GetBool("registration_enabled"),
DBSeeded: viper.GetBool("db_seeded"),
Archive: struct {
SaveAsHls bool `json:"save_as_hls"`
}(struct {
Expand Down Expand Up @@ -377,6 +397,14 @@ func refreshConfig(configPath string) {
if !viper.IsSet("video_check_interval_minutes") {
viper.Set("video_check_interval_minutes", 180)
}
err = unset("db_seeded")
if err != nil {
log.Error().Err(err).Msg("error unsetting config value")
}
err = unset("active_queue_items")
if err != nil {
log.Error().Err(err).Msg("error unsetting config value")
}

}

Expand Down
33 changes: 17 additions & 16 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

_ "github.com/lib/pq"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/zibbp/ganymede/ent"
"github.com/zibbp/ganymede/internal/utils"
"golang.org/x/crypto/bcrypt"
Expand Down Expand Up @@ -44,16 +43,17 @@ func InitializeDatabase(worker bool) {
if err := client.Schema.Create(context.Background()); err != nil {
log.Fatal().Err(err).Msg("error running auto migration")
}
isSeeded := viper.Get("db_seeded").(bool)
if !isSeeded {
// check if any users exist
users, err := client.User.Query().All(context.Background())
if err != nil {
log.Panic().Err(err).Msg("error querying users")
}
// if no users exist, seed database
if len(users) == 0 {
// seed database
log.Debug().Msg("seeding database")
if err := seedDatabase(client); err != nil {
log.Fatal().Err(err).Msg("error seeding database")
}
viper.Set("db_seeded", true)
err := viper.WriteConfig()
if err != nil {
log.Fatal().Err(err).Msg("error writing config")
log.Panic().Err(err).Msg("error seeding database")
}
}
}
Expand Down Expand Up @@ -89,17 +89,18 @@ func NewDatabase() (*Database, error) {
return nil, err
}

isSeeded := viper.Get("db_seeded").(bool)
if !isSeeded {
// check if any users exist
users, err := client.User.Query().All(context.Background())
if err != nil {
return nil, err
}
// if no users exist, seed database
if len(users) == 0 {
// seed database
log.Debug().Msg("seeding database")
if err := seedDatabase(client); err != nil {
return nil, err
}
viper.Set("db_seeded", true)
err := viper.WriteConfig()
if err != nil {
return nil, fmt.Errorf("error writing config: %v", err)
}
}

return &Database{Client: client}, nil
Expand Down

0 comments on commit 2a9ebe8

Please sign in to comment.