1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +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 {
case RS_CONNECT:
recon_cnt++
log.Infof("Connecting to ship at: %s (Try %d)", conf.ShipApi, recon_cnt)
err := shClient.Connect(conf.ShipApi)
log.Infof("Connecting to ship at: %s (Try %d)", conf.Ship.Url, recon_cnt)
err := shClient.Connect(conf.Ship.Url)
if err != nil {
log.Println(err)
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 {
log.WithError(err).Error("Failed to send notification")
}
@ -206,18 +206,18 @@ func main() {
return
}
if conf.StartBlockNum == shipclient.NULL_BLOCK_NUMBER {
if conf.IrreversibleOnly {
conf.StartBlockNum = uint32(chainInfo.LastIrreversibleBlockNum)
if conf.Ship.StartBlockNum == shipclient.NULL_BLOCK_NUMBER {
if conf.Ship.IrreversibleOnly {
conf.Ship.StartBlockNum = uint32(chainInfo.LastIrreversibleBlockNum)
} else {
conf.StartBlockNum = uint32(chainInfo.HeadBlockNum)
conf.Ship.StartBlockNum = uint32(chainInfo.HeadBlockNum)
}
}
shClient = shipclient.NewClient(func(c *shipclient.Client) {
c.StartBlock = conf.StartBlockNum
c.EndBlock = conf.EndBlockNum
c.IrreversibleOnly = conf.IrreversibleOnly
c.StartBlock = conf.Ship.StartBlockNum
c.EndBlock = conf.Ship.EndBlockNum
c.IrreversibleOnly = conf.Ship.IrreversibleOnly
})
processor := app.SpawnProccessor(

View file

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

View file

@ -20,27 +20,32 @@ type TelegramConfig struct {
Channel int64 `json:"channel"`
}
type Config struct {
Name string `json:"name"`
ShipApi string `json:"ship_api"`
Api string `json:"api"`
Redis RedisConfig `json:"redis"`
Telegram TelegramConfig `json:"telegram"`
type ShipConfig struct {
Url string `json:"url"`
IrreversibleOnly bool `json:"irreversible_only"`
MaxMessagesInFlight uint32 `json:"max_messages_in_flight"`
StartBlockNum uint32 `json:"start_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) {
cfg := Config{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
Ship: ShipConfig{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "",
@ -53,6 +58,20 @@ func Parse(data []byte) (*Config, error) {
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) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {

View file

@ -10,10 +10,13 @@ import (
func TestParse_Default(t *testing.T) {
expected := Config{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
Ship: ShipConfig{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "",
@ -29,13 +32,15 @@ func TestParse_Default(t *testing.T) {
func TestParse(t *testing.T) {
expected := Config{
Name: "ship-reader-1",
Api: "http://127.0.0.1:8080",
ShipApi: "127.0.0.1:8089",
StartBlockNum: 23671836,
EndBlockNum: 23872222,
IrreversibleOnly: true,
MaxMessagesInFlight: 1337,
Name: "ship-reader-1",
Api: "http://127.0.0.1:8080",
Ship: ShipConfig{
Url: "127.0.0.1:8089",
StartBlockNum: 23671836,
EndBlockNum: 23872222,
IrreversibleOnly: true,
MaxMessagesInFlight: 1337,
},
Telegram: TelegramConfig{
Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
Channel: -123456789,
@ -51,11 +56,56 @@ func TestParse(t *testing.T) {
cfg, err := Parse([]byte(`{
"name": "ship-reader-1",
"api": "http://127.0.0.1:8080",
"ship_api": "127.0.0.1:8089",
"irreversible_only": true,
"max_messages_in_flight": 1337,
"start_block_num": 23671836,
"end_block_num": 23872222,
"ship": {
"url": "127.0.0.1:8089",
"irreversible_only": true,
"max_messages_in_flight": 1337,
"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": {
"id": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
"channel": -123456789