refactor: queries struct and pkg updates

- update cic-go to latest
- separate sql queries by logic
This commit is contained in:
2022-05-11 12:23:44 +03:00
parent 29af3b5c21
commit 41f75b4efc
8 changed files with 97 additions and 128 deletions

View File

@@ -2,8 +2,9 @@ package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic_go/cic_net"
cic_net "github.com/grassrootseconomics/cic-go/net"
"github.com/hibiken/asynq"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf"
@@ -11,7 +12,6 @@ import (
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/nleof/goyesql"
"github.com/rs/zerolog/log"
"strings"
)
@@ -27,6 +27,11 @@ type config struct {
Syncers map[string]string `koanf:"syncers"`
}
type queries struct {
core goyesql.Queries
dashboard goyesql.Queries
}
func loadConfig(configFilePath string, k *koanf.Koanf) error {
confFile := file.Provider(configFilePath)
if err := k.Load(confFile, toml.Parser()); err != nil {
@@ -77,45 +82,21 @@ func connectCicNet(rpcProvider string, tokenIndex common.Address) error {
return nil
}
func loadQueries(sqlFile string) error {
var err error
queries, err = goyesql.ParseFile(sqlFile)
func loadQueries(sqlFilesPath string) error {
coreQueries, err := goyesql.ParseFile(fmt.Sprintf("%s/core.sql", sqlFilesPath))
if err != nil {
return err
}
return nil
}
func bootstrapScheduler(redis asynq.RedisConnOpt) (*asynq.Scheduler, error) {
scheduler := asynq.NewScheduler(redis, nil)
for k, v := range conf.Syncers {
task := asynq.NewTask(k, nil)
_, err := scheduler.Register(v, task)
if err != nil {
return nil, err
}
log.Info().Msgf("successfully registered %s syncer", k)
dashboardQueries, err := goyesql.ParseFile(fmt.Sprintf("%s/dashboard.sql", sqlFilesPath))
if err != nil {
return err
}
return scheduler, nil
}
func bootstrapProcessor(redis asynq.RedisConnOpt) (*asynq.Server, *asynq.ServeMux) {
processorServer := asynq.NewServer(
redis,
asynq.Config{
Concurrency: 5,
},
)
mux := asynq.NewServeMux()
mux.HandleFunc("token", tokenSyncer)
mux.HandleFunc("cache", cacheSyncer)
mux.HandleFunc("ussd", ussdSyncer)
return processorServer, mux
preparedQueries = &queries{
core: coreQueries,
dashboard: dashboardQueries,
}
return nil
}

View File

@@ -1,11 +1,10 @@
package main
import (
"github.com/grassrootseconomics/cic_go/cic_net"
cic_net "github.com/grassrootseconomics/cic-go/net"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf"
"github.com/lmittmann/w3"
"github.com/nleof/goyesql"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/sys/unix"
@@ -16,10 +15,10 @@ import (
var (
k = koanf.New(".")
queries goyesql.Queries
conf config
db *pgxpool.Pool
cicnetClient *cic_net.CicNet
preparedQueries *queries
conf config
db *pgxpool.Pool
cicnetClient *cic_net.CicNet
)
func init() {
@@ -29,7 +28,7 @@ func init() {
log.Fatal().Err(err).Msg("failed to load config")
}
if err := loadQueries("queries.sql"); err != nil {
if err := loadQueries("queries"); err != nil {
log.Fatal().Err(err).Msg("failed to load sql file")
}