Skip to content

Commit

Permalink
Fix CI failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Warehime committed Jun 22, 2022
1 parent 24e4621 commit b762879
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
38 changes: 16 additions & 22 deletions cmd/algorand-indexer/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,19 @@ type daemonConfig struct {
genesisJSONPath string
}

func newDaemonConfig() *daemonConfig {
return &daemonConfig{}
}

func newDaemonCmd(config *daemonConfig) *cobra.Command {
cmd := &cobra.Command{
Use: "daemon",
Short: "run indexer daemon",
Long: "run indexer daemon. Serve api on HTTP.",
//Args:
Run: func(cmd *cobra.Command, args []string) {
config.flags = cmd.Flags()
if err := runDaemon(config); err != nil {
panic(exit{1})
}
},
}
config.setFlags(cmd.Flags())
return cmd
var daemonCfg = &daemonConfig{}

var daemonCmd = &cobra.Command{
Use: "daemon",
Short: "run indexer daemon",
Long: "run indexer daemon. Serve api on HTTP.",
//Args:
Run: func(cmd *cobra.Command, args []string) {
daemonCfg.flags = cmd.Flags()
if err := runDaemon(daemonCfg); err != nil {
panic(exit{1})
}
},
}

func configureIndexerDataDir(cfg *daemonConfig) error {
Expand Down Expand Up @@ -163,7 +157,7 @@ func loadIndexerParamConfig(cfg *daemonConfig) error {
// If we auto-loaded configs but a user supplied them as well, we have an error
if paramConfigFound {
if cfg.suppliedAPIConfigFile != "" {
err = fmt.Errorf("api parameter configuration was found in data directory (%s) as well as supplied via command line. Only provide one.",
err = fmt.Errorf("api parameter configuration was found in data directory (%s) as well as supplied via command line. Only provide one",
filepath.Join(cfg.indexerDataDir, autoLoadParameterConfigName))
logger.WithError(err).Errorf("indexer parameter config error: %v", err)
return err
Expand Down Expand Up @@ -298,7 +292,7 @@ func runDaemon(daemonConfig *daemonConfig) error {
var wg sync.WaitGroup
if bot != nil {
wg.Add(1)
go runBlockImporter(daemonConfig, &wg, db, availableCh, bot, ctx, &opts)
go runBlockImporter(ctx, daemonConfig, &wg, db, availableCh, bot, &opts)
} else {
logger.Info("No block importer configured.")
}
Expand All @@ -313,7 +307,7 @@ func runDaemon(daemonConfig *daemonConfig) error {
return err
}

func runBlockImporter(cfg *daemonConfig, wg *sync.WaitGroup, db idb.IndexerDb, dbAvailable chan struct{}, bot fetcher.Fetcher, ctx context.Context, opts *idb.IndexerDbOptions) {
func runBlockImporter(ctx context.Context, cfg *daemonConfig, wg *sync.WaitGroup, db idb.IndexerDb, dbAvailable chan struct{}, bot fetcher.Fetcher, opts *idb.IndexerDbOptions) {
// Need to redefine exitHandler() for every go-routine
defer exitHandler()
defer wg.Done()
Expand Down
24 changes: 12 additions & 12 deletions cmd/algorand-indexer/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func createTempDir(t *testing.T) string {
func TestConfigWithEnableAllParamsExpectError(t *testing.T) {
indexerDataDir := createTempDir(t)
defer os.RemoveAll(indexerDataDir)
daemonConfig := newDaemonConfig()
daemonConfig := &daemonConfig{}
daemonConfig.flags = pflag.NewFlagSet("indexer", 0)
daemonConfig.indexerDataDir = indexerDataDir
daemonConfig.enableAllParameters = true
Expand All @@ -139,7 +139,7 @@ func TestConfigDoesNotExistExpectError(t *testing.T) {
indexerDataDir := createTempDir(t)
defer os.RemoveAll(indexerDataDir)
tempConfigFile := indexerDataDir + "/indexer.yml"
daemonConfig := newDaemonConfig()
daemonConfig := &daemonConfig{}
daemonConfig.flags = pflag.NewFlagSet("indexer", 0)
daemonConfig.indexerDataDir = indexerDataDir
daemonConfig.configFile = tempConfigFile
Expand All @@ -157,7 +157,7 @@ func TestConfigInvalidExpectError(t *testing.T) {
defer os.RemoveAll(indexerDataDir)
tempConfigFile := indexerDataDir + "/indexer-alt.yml"
os.WriteFile(tempConfigFile, []byte(";;;"), fs.ModePerm)
daemonConfig := newDaemonConfig()
daemonConfig := &daemonConfig{}
daemonConfig.flags = pflag.NewFlagSet("indexer", 0)
daemonConfig.indexerDataDir = indexerDataDir
daemonConfig.configFile = tempConfigFile
Expand All @@ -176,7 +176,7 @@ func TestConfigSpecifiedTwiceExpectError(t *testing.T) {
defer os.RemoveAll(indexerDataDir)
tempConfigFile := indexerDataDir + "/indexer.yml"
os.WriteFile(tempConfigFile, []byte{}, fs.ModePerm)
daemonConfig := newDaemonConfig()
daemonConfig := &daemonConfig{}
daemonConfig.flags = pflag.NewFlagSet("indexer", 0)
daemonConfig.indexerDataDir = indexerDataDir
daemonConfig.configFile = tempConfigFile
Expand All @@ -195,12 +195,12 @@ func TestLoadAPIConfigGivenAutoLoadAndUserSuppliedExpectError(t *testing.T) {
autoloadPath := filepath.Join(indexerDataDir, autoLoadParameterConfigName)
userSuppliedPath := filepath.Join(indexerDataDir, "foobar.yml")
os.WriteFile(autoloadPath, []byte{}, fs.ModePerm)
cfg := newDaemonConfig()
cfg := &daemonConfig{}
cfg.indexerDataDir = indexerDataDir
cfg.suppliedAPIConfigFile = userSuppliedPath

err := loadIndexerParamConfig(cfg)
expectedErr := fmt.Errorf("api parameter configuration was found in data directory (%s) as well as supplied via command line. Only provide one.",
expectedErr := fmt.Errorf("api parameter configuration was found in data directory (%s) as well as supplied via command line. Only provide one",
autoloadPath)
if err.Error() != expectedErr.Error() {
t.Fatalf("expected error %v, but got %v", expectedErr, err)
Expand All @@ -212,7 +212,7 @@ func TestLoadAPIConfigGivenUserSuppliedExpectSuccess(t *testing.T) {
defer os.RemoveAll(indexerDataDir)

userSuppliedPath := filepath.Join(indexerDataDir, "foobar.yml")
cfg := newDaemonConfig()
cfg := &daemonConfig{}
cfg.indexerDataDir = indexerDataDir
cfg.suppliedAPIConfigFile = userSuppliedPath

Expand All @@ -228,7 +228,7 @@ func TestLoadAPIConfigGivenAutoLoadExpectSuccess(t *testing.T) {

autoloadPath := filepath.Join(indexerDataDir, autoLoadParameterConfigName)
os.WriteFile(autoloadPath, []byte{}, fs.ModePerm)
cfg := newDaemonConfig()
cfg := &daemonConfig{}
cfg.indexerDataDir = indexerDataDir

err := loadIndexerParamConfig(cfg)
Expand All @@ -239,7 +239,7 @@ func TestLoadAPIConfigGivenAutoLoadExpectSuccess(t *testing.T) {
}

func TestIndexerDataDirNotProvidedExpectError(t *testing.T) {
cfg := newDaemonConfig()
cfg := &daemonConfig{}

expectedErr := "indexer data directory was not provided"

Expand All @@ -251,7 +251,7 @@ func TestIndexerDataDirCreateFailExpectError(t *testing.T) {
defer os.RemoveAll(indexerDataDir)

invalidDir := filepath.Join(indexerDataDir, "foo", "bar")
cfg := newDaemonConfig()
cfg := &daemonConfig{}
cfg.indexerDataDir = invalidDir

assert.Error(t, configureIndexerDataDir(cfg))
Expand All @@ -261,7 +261,7 @@ func TestIndexerPidFileExpectSuccess(t *testing.T) {
indexerDataDir := createTempDir(t)
defer os.RemoveAll(indexerDataDir)

cfg := newDaemonConfig()
cfg := &daemonConfig{}
cfg.pidFilePath = path.Join(indexerDataDir, "pidFile")
assert.NoError(t, createIndexerPidFile(cfg))
}
Expand All @@ -271,7 +271,7 @@ func TestIndexerPidFileCreateFailExpectError(t *testing.T) {
defer os.RemoveAll(indexerDataDir)

invalidDir := filepath.Join(indexerDataDir, "foo", "bar")
cfg := newDaemonConfig()
cfg := &daemonConfig{}
cfg.pidFilePath = invalidDir

cfg.flags = pflag.NewFlagSet("indexer", 0)
Expand Down
2 changes: 1 addition & 1 deletion cmd/algorand-indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func init() {

rootCmd.AddCommand(importCmd)
importCmd.Hidden = true
daemonCmd := newDaemonCmd(newDaemonConfig())
daemonCfg.setFlags(daemonCmd.Flags())
rootCmd.AddCommand(daemonCmd)
rootCmd.AddCommand(apiConfigCmd)

Expand Down

0 comments on commit b762879

Please sign in to comment.