1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-19 04:50:02 +02:00

Merge branch '32-request-for-more-startup-flags'

This commit is contained in:
Henrik Hautakoski 2024-02-18 20:03:54 +01:00
commit 2382f72e5a
8 changed files with 406 additions and 257 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"github.com/eosswedenorg/thalos/internal/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@ -28,13 +29,14 @@ func init() {
rootCmd.SetVersionTemplate(`{{with .Name}}{{printf "%s " .}}{{end}}{{printf "v%s" .Version}}` + "\n")
flags := pflag.FlagSet{}
flags.StringP("config", "c", "./config.yml", "Config file to read")
flags.StringP("level", "L", "info", "Log level to use")
flags.StringP("log", "l", "", "Path to log file (default: print to stdout/stderr)")
flags.StringP("pid", "p", "", "Where to write process id")
flags.BoolP("no-state-cache", "n", false, "Force the application to take start block from config/api")
rootCmd.PersistentFlags().AddFlagSet(&flags)
rootCmd.PersistentFlags().AddFlagSet(config.GetFlags())
}
func main() {

View file

@ -7,6 +7,7 @@ import (
"io"
"os"
"os/signal"
"path"
"syscall"
"time"
@ -153,7 +154,7 @@ func initAbiManager(api *eos.API, store cache.Store, chain_id string) *abi.AbiMa
return abi.NewAbiManager(cache, api)
}
func stateLoader(conf config.Config, chainInfo func() *eos.InfoResp, cache *cache.Cache, current_block_no_cache bool) StateLoader {
func stateLoader(conf *config.Config, start_block_flag *pflag.Flag, chainInfo func() *eos.InfoResp, cache *cache.Cache, current_block_no_cache bool) StateLoader {
return func(state *State) {
var source string
@ -167,7 +168,13 @@ func stateLoader(conf config.Config, chainInfo func() *eos.InfoResp, cache *cach
if current_block_no_cache || err != nil {
// Set from config if we have a sane value.
if conf.Ship.StartBlockNum != shipclient.NULL_BLOCK_NUMBER {
source = "config"
if start_block_flag != nil && start_block_flag.Changed {
source = "cli"
} else {
source = "config"
}
state.CurrentBlock = conf.Ship.StartBlockNum
} else {
// Otherwise, set from api.
@ -198,23 +205,34 @@ func stateSaver(cache *cache.Cache) StateSaver {
}
}
func ReadConfig(cfg *config.Config, flags *pflag.FlagSet) error {
func GetConfig(flags *pflag.FlagSet) (*config.Config, error) {
filename, err := flags.GetString("config")
if err != nil {
return err
return nil, err
}
// Read file first.
if err := cfg.ReadFile(filename); err != nil {
return err
cfg, err := config.NewBuilder().
SetConfigFile(filename).
SetFlags(flags).
Build()
if err != nil {
return nil, err
}
// Then override any cli flags
if err := cfg.ReadCliFlags(flags); err != nil {
return err
logFile, _ := flags.GetString("log")
if len(logFile) > 0 {
cfg.Log.Directory = path.Dir(logFile)
cfg.Log.Filename = path.Base(logFile)
}
return nil
// If start-block is provided, we should set no-state-cache to true.
if startBlock := flags.Lookup("start-block"); startBlock != nil && startBlock.Changed {
if err := flags.Set("no-state-cache", "true"); err != nil {
return cfg, nil
}
}
return cfg, nil
}
// "Clever" way to make sure we only call the api once.
@ -244,8 +262,6 @@ func chainInfoOnce(api *eos.API) func() *eos.InfoResp {
func serverCmd(cmd *cobra.Command, args []string) {
var err error
skip_currentblock_cache, _ := cmd.Flags().GetBool("no-state-cache")
// Write PID file
pidFile, err := cmd.Flags().GetString("pid")
if err != nil {
@ -257,12 +273,14 @@ func serverCmd(cmd *cobra.Command, args []string) {
}
// Parse config
conf := config.New()
if err = ReadConfig(&conf, cmd.Flags()); err != nil {
conf, err := GetConfig(cmd.Flags())
if err != nil {
log.WithError(err).Fatal("Failed to read config")
return
}
skip_currentblock_cache, _ := cmd.Flags().GetBool("no-state-cache")
flagLevel, _ := cmd.Flags().GetString("level")
lvl, err := log.ParseLevel(flagLevel)
if err == nil {
@ -360,7 +378,7 @@ func serverCmd(cmd *cobra.Command, args []string) {
processor := SpawnProccessor(
shClient,
stateLoader(conf, chainInfo, cache, skip_currentblock_cache),
stateLoader(conf, cmd.Flags().Lookup("start-block"), chainInfo, cache, skip_currentblock_cache),
stateSaver(cache),
driver.NewPublisher(context.Background(), rdb, api_redis.Namespace{
Prefix: conf.Redis.Prefix,
@ -371,7 +389,7 @@ func serverCmd(cmd *cobra.Command, args []string) {
)
// Run the application
run(&conf, shClient, processor)
run(conf, shClient, processor)
// Close the processor properly
processor.Close()