1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-07-03 11:53:41 +02:00

config/config.go: put Ship stuff into its own struct/section and also provide a shorthand "string" value for "ship" field (only url)

This commit is contained in:
Henrik Hautakoski 2023-04-17 15:17:46 +02:00
parent e860c81601
commit 5fa1dbb504
4 changed files with 115 additions and 44 deletions

View file

@ -48,13 +48,13 @@ func readerLoop() {
switch state { switch state {
case RS_CONNECT: case RS_CONNECT:
recon_cnt++ recon_cnt++
log.Infof("Connecting to ship at: %s (Try %d)", conf.ShipApi, recon_cnt) log.Infof("Connecting to ship at: %s (Try %d)", conf.Ship.Url, recon_cnt)
err := shClient.Connect(conf.ShipApi) err := shClient.Connect(conf.Ship.Url)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
if recon_cnt >= 3 { if recon_cnt >= 3 {
msg := fmt.Sprintf("Failed to connect to ship at '%s'", conf.ShipApi) msg := fmt.Sprintf("Failed to connect to ship at '%s'", conf.Ship.Url)
if err := notify.Send(context.Background(), conf.Name, msg); err != nil { if err := notify.Send(context.Background(), conf.Name, msg); err != nil {
log.WithError(err).Error("Failed to send notification") log.WithError(err).Error("Failed to send notification")
} }
@ -206,18 +206,18 @@ func main() {
return return
} }
if conf.StartBlockNum == shipclient.NULL_BLOCK_NUMBER { if conf.Ship.StartBlockNum == shipclient.NULL_BLOCK_NUMBER {
if conf.IrreversibleOnly { if conf.Ship.IrreversibleOnly {
conf.StartBlockNum = uint32(chainInfo.LastIrreversibleBlockNum) conf.Ship.StartBlockNum = uint32(chainInfo.LastIrreversibleBlockNum)
} else { } else {
conf.StartBlockNum = uint32(chainInfo.HeadBlockNum) conf.Ship.StartBlockNum = uint32(chainInfo.HeadBlockNum)
} }
} }
shClient = shipclient.NewClient(func(c *shipclient.Client) { shClient = shipclient.NewClient(func(c *shipclient.Client) {
c.StartBlock = conf.StartBlockNum c.StartBlock = conf.Ship.StartBlockNum
c.EndBlock = conf.EndBlockNum c.EndBlock = conf.Ship.EndBlockNum
c.IrreversibleOnly = conf.IrreversibleOnly c.IrreversibleOnly = conf.Ship.IrreversibleOnly
}) })
processor := app.SpawnProccessor( processor := app.SpawnProccessor(

View file

@ -1,11 +1,13 @@
{ {
"name": "ship-reader-1", "name": "ship-reader-1",
"api": "http://127.0.0.1:8080", "api": "http://127.0.0.1:8080",
"ship_api": "127.0.0.1:8089", "ship": {
"irreversible_only": false, "url": "127.0.0.1:8089",
"max_messages_in_flight": 10, "irreversible_only": false,
"start_block_num": 4294967295, "max_messages_in_flight": 10,
"end_block_num": 4294967295, "start_block_num": 4294967295,
"end_block_num": 4294967295
},
"telegram": { "telegram": {
"id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw", "id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
"channel": -123456789 "channel": -123456789

View file

@ -20,27 +20,32 @@ type TelegramConfig struct {
Channel int64 `json:"channel"` Channel int64 `json:"channel"`
} }
type Config struct { type ShipConfig struct {
Name string `json:"name"` Url string `json:"url"`
ShipApi string `json:"ship_api"`
Api string `json:"api"`
Redis RedisConfig `json:"redis"`
Telegram TelegramConfig `json:"telegram"`
IrreversibleOnly bool `json:"irreversible_only"` IrreversibleOnly bool `json:"irreversible_only"`
MaxMessagesInFlight uint32 `json:"max_messages_in_flight"` MaxMessagesInFlight uint32 `json:"max_messages_in_flight"`
StartBlockNum uint32 `json:"start_block_num"` StartBlockNum uint32 `json:"start_block_num"`
EndBlockNum uint32 `json:"end_block_num"` EndBlockNum uint32 `json:"end_block_num"`
} }
type Config struct {
Name string `json:"name"`
Ship ShipConfig `json:"ship"`
Api string `json:"api"`
Redis RedisConfig `json:"redis"`
Telegram TelegramConfig `json:"telegram"`
}
func Parse(data []byte) (*Config, error) { func Parse(data []byte) (*Config, error) {
cfg := Config{ cfg := Config{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER, Ship: ShipConfig{
EndBlockNum: shipclient.NULL_BLOCK_NUMBER, StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10, EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
IrreversibleOnly: false, MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Redis: RedisConfig{ Redis: RedisConfig{
Addr: "localhost:6379", Addr: "localhost:6379",
Password: "", Password: "",
@ -53,6 +58,20 @@ func Parse(data []byte) (*Config, error) {
return &cfg, err return &cfg, err
} }
func (ship *ShipConfig) UnmarshalJSON(data []byte) error {
var err error
if err = json.Unmarshal(data, &ship.Url); err != nil {
//
type ShipConfigRaw ShipConfig
raw := ShipConfigRaw(*ship)
if err = json.Unmarshal(data, &raw); err == nil {
*ship = ShipConfig(raw)
}
}
return err
}
func Load(filename string) (*Config, error) { func Load(filename string) (*Config, error) {
bytes, err := ioutil.ReadFile(filename) bytes, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {

View file

@ -10,10 +10,13 @@ import (
func TestParse_Default(t *testing.T) { func TestParse_Default(t *testing.T) {
expected := Config{ expected := Config{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER, Ship: ShipConfig{
EndBlockNum: shipclient.NULL_BLOCK_NUMBER, StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10, EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
IrreversibleOnly: false, MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Redis: RedisConfig{ Redis: RedisConfig{
Addr: "localhost:6379", Addr: "localhost:6379",
Password: "", Password: "",
@ -29,13 +32,15 @@ func TestParse_Default(t *testing.T) {
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
expected := Config{ expected := Config{
Name: "ship-reader-1", Name: "ship-reader-1",
Api: "http://127.0.0.1:8080", Api: "http://127.0.0.1:8080",
ShipApi: "127.0.0.1:8089", Ship: ShipConfig{
StartBlockNum: 23671836, Url: "127.0.0.1:8089",
EndBlockNum: 23872222, StartBlockNum: 23671836,
IrreversibleOnly: true, EndBlockNum: 23872222,
MaxMessagesInFlight: 1337, IrreversibleOnly: true,
MaxMessagesInFlight: 1337,
},
Telegram: TelegramConfig{ Telegram: TelegramConfig{
Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw", Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
Channel: -123456789, Channel: -123456789,
@ -51,11 +56,56 @@ func TestParse(t *testing.T) {
cfg, err := Parse([]byte(`{ cfg, err := Parse([]byte(`{
"name": "ship-reader-1", "name": "ship-reader-1",
"api": "http://127.0.0.1:8080", "api": "http://127.0.0.1:8080",
"ship_api": "127.0.0.1:8089", "ship": {
"irreversible_only": true, "url": "127.0.0.1:8089",
"max_messages_in_flight": 1337, "irreversible_only": true,
"start_block_num": 23671836, "max_messages_in_flight": 1337,
"end_block_num": 23872222, "start_block_num": 23671836,
"end_block_num": 23872222
},
"telegram": {
"id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
"channel": -123456789
},
"redis": {
"addr": "localhost:6379",
"password": "passwd",
"db": 4,
"prefix": "some::ship"
}
}`))
require.NoError(t, err)
require.Equal(t, cfg, &expected)
}
func TestParseShorthandShipUrl(t *testing.T) {
expected := Config{
Name: "ship-reader-1",
Api: "http://127.0.0.1:8080",
Ship: ShipConfig{
Url: "127.0.0.1:8089",
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Telegram: TelegramConfig{
Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
Channel: -123456789,
},
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "passwd",
DB: 4,
Prefix: "some::ship",
},
}
cfg, err := Parse([]byte(`{
"name": "ship-reader-1",
"api": "http://127.0.0.1:8080",
"ship": "127.0.0.1:8089",
"telegram": { "telegram": {
"id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw", "id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
"channel": -123456789 "channel": -123456789