Skip to content

Commit

Permalink
resolve comment
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoyangLiu committed Aug 25, 2020
1 parent 6b66d2d commit 915364e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ func (cfg *LogConfig) Validate() {
}

type AlertConfig struct {
EnableAlert bool `json:"enable_alert"`
EnableAlert bool `json:"enable_alert"`
Interval int64 `json:"interval"`

TelegramBotId string `json:"telegram_bot_id"`
TelegramChatId string `json:"telegram_chat_id"`
Expand All @@ -153,6 +154,9 @@ func (cfg *AlertConfig) Validate() {
if !cfg.EnableAlert {
return
}
if cfg.Interval <= 0 {
panic("alert interval should be positive")
}
balanceThreshold, ok := big.NewInt(1).SetString(cfg.BalanceThreshold, 10)
if !ok {
panic(fmt.Sprintf("unrecognized balance_threshold"))
Expand Down
1 change: 1 addition & 0 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"alert_config": {
"enable_alert": false,
"interval": 300,
"telegram_bot_id": "your_bot_id",
"telegram_chat_id": "your_chat_id",
"balance_threshold": "1000000000000000000"
Expand Down
8 changes: 5 additions & 3 deletions relayer/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
AlertInterval = 60 * time.Second
RetryInterval = 5 * time.Second
)

func (r *Relayer) alert() {
Expand All @@ -25,19 +25,21 @@ func (r *Relayer) alert() {
for {
balance, err := r.bscExecutor.GetRelayerBalance()
if err != nil {
util.SendTelegramMessage(r.cfg.AlertConfig.TelegramBotId, r.cfg.AlertConfig.TelegramChatId, fmt.Sprintf("got relayer balance failure: %s", err.Error()))
time.Sleep(RetryInterval)
continue
} else {
balance, err := decimal.NewFromString(balance.String())
if err != nil {
common.Logger.Error(err.Error())
}
util.SendTelegramMessage(r.cfg.AlertConfig.TelegramBotId, r.cfg.AlertConfig.TelegramChatId, fmt.Sprintf("relayer balance: %s", balance.String()))
if balance.Cmp(balanceThreshold) <= 0 {
msg := fmt.Sprintf("bsc-relayer balance (%s:BNB) on Binance Smart Chain is less than threshold (%s:BNB)",
balance.Div(decimal.NewFromInt(1e18)).String(), balanceThreshold.Div(decimal.NewFromInt(1e18)).String())
util.SendTelegramMessage(r.cfg.AlertConfig.TelegramBotId, r.cfg.AlertConfig.TelegramChatId, msg)
}
}

time.Sleep(AlertInterval)
time.Sleep(time.Duration(r.cfg.AlertConfig.Interval) * time.Second)
}
}
2 changes: 1 addition & 1 deletion relayer/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/binance-chain/bsc-relayer/common"
)

func (r *Relayer)cleanPreviousPackages(height uint64) error {
func (r *Relayer) cleanPreviousPackages(height uint64) error {
blockSynced := false
common.Logger.Info("enter CleanPreviousPackages mode")
for _, channelId := range r.bbcExecutor.Config.CrossChainConfig.MonitorChannelList {
Expand Down
2 changes: 1 addition & 1 deletion relayer/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/binance-chain/bsc-relayer/common"
)

func (r *Relayer)registerRelayerHub() {
func (r *Relayer) registerRelayerHub() {
isRelayer, err := r.bscExecutor.IsRelayer()
if err != nil {
panic(err)
Expand Down

0 comments on commit 915364e

Please sign in to comment.