mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-19 04:50:02 +02:00
WIP
This commit is contained in:
parent
764db98417
commit
074c089679
7 changed files with 272 additions and 29 deletions
20
app/service/notifier/notifier.go
Normal file
20
app/service/notifier/notifier.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package notifier
|
||||
|
||||
import (
|
||||
"github.com/nikoksr/notify"
|
||||
)
|
||||
|
||||
type NotifierInit func() (notify.Notifier, error)
|
||||
|
||||
func InitNotifier(initializers ...NotifierInit) error {
|
||||
for _, init := range initializers {
|
||||
notifier, err := init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
notify.UseServices(notifier)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
17
app/service/redis/client.go
Normal file
17
app/service/redis/client.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
func NewClient(cfg Config) (*redis.Client, error) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: cfg.Addr,
|
||||
Password: cfg.Password,
|
||||
DB: cfg.DB,
|
||||
})
|
||||
|
||||
return rdb, rdb.Ping(context.Background()).Err()
|
||||
}
|
||||
26
app/service/shipclient/client.go
Normal file
26
app/service/shipclient/client.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package shipclient
|
||||
|
||||
import (
|
||||
"eosio-ship-trace-reader/config"
|
||||
|
||||
"github.com/eoscanada/eos-go"
|
||||
shipclient "github.com/eosswedenorg-go/antelope-ship-client"
|
||||
)
|
||||
|
||||
func NewClient(cfg *config.Config, chain *eos.InfoResp) (*shipclient.Client, error) {
|
||||
if cfg.StartBlockNum == config.NULL_BLOCK_NUMBER {
|
||||
if cfg.IrreversibleOnly {
|
||||
cfg.StartBlockNum = uint32(chain.LastIrreversibleBlockNum)
|
||||
} else {
|
||||
cfg.StartBlockNum = uint32(chain.HeadBlockNum)
|
||||
}
|
||||
}
|
||||
|
||||
options := func(c *shipclient.Client) {
|
||||
c.StartBlock = cfg.StartBlockNum
|
||||
c.EndBlock = cfg.EndBlockNum
|
||||
c.IrreversibleOnly = cfg.IrreversibleOnly
|
||||
}
|
||||
|
||||
return shipclient.NewClient(options), nil
|
||||
}
|
||||
|
|
@ -1,6 +1,21 @@
|
|||
package telegram
|
||||
|
||||
import (
|
||||
"github.com/nikoksr/notify"
|
||||
"github.com/nikoksr/notify/service/telegram"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Id string `json:"id"`
|
||||
Channel int64 `json:"channel"`
|
||||
}
|
||||
|
||||
func Notifier(conf Config) func() (notify.Notifier, error) {
|
||||
return func() (notify.Notifier, error) {
|
||||
tg, err := telegram.New(conf.Id)
|
||||
if err == nil {
|
||||
tg.AddReceivers(conf.Channel)
|
||||
}
|
||||
return tg, err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
106
app/ship/client.go
Normal file
106
app/ship/client.go
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
package ship
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/nikoksr/notify"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
shipclient "github.com/eosswedenorg-go/antelope-ship-client"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
sh *shipclient.Client
|
||||
api string
|
||||
|
||||
done chan interface{}
|
||||
}
|
||||
|
||||
func New(api string, client *shipclient.Client) *Client {
|
||||
return &Client{
|
||||
api: api,
|
||||
sh: client,
|
||||
done: make(chan interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) connect() {
|
||||
var recon_cnt uint = 0
|
||||
|
||||
for {
|
||||
recon_cnt++
|
||||
log.Infof("Connecting to ship at: %s (Try %d)", c.api, recon_cnt)
|
||||
err := c.sh.Connect(c.api)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
if recon_cnt >= 3 {
|
||||
msg := fmt.Sprintf("Failed to connect to ship at '%s'", c.api)
|
||||
if err := notify.Send(context.Background(), "Ship_reader", msg); err != nil {
|
||||
log.WithError(err).Error("Failed to send notification")
|
||||
}
|
||||
recon_cnt = 0
|
||||
}
|
||||
|
||||
log.Info("Trying again in 5 seconds ....")
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
err = c.sh.SendBlocksRequest()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Connected
|
||||
log.Infof("Connected, Start: %d, End: %d", c.sh.StartBlock, c.sh.EndBlock)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) read() {
|
||||
err := c.sh.Read()
|
||||
if err != nil {
|
||||
if shErr, ok := err.(shipclient.ClientError); ok {
|
||||
|
||||
// Bail out if socket is closed
|
||||
if shErr.Type == shipclient.ErrSockClosed {
|
||||
log.Info("Socket closed, Exiting")
|
||||
return
|
||||
}
|
||||
|
||||
// Reconnect
|
||||
if shErr.Type == shipclient.ErrSockRead || shErr.Type == shipclient.ErrNotConnected {
|
||||
c.connect()
|
||||
}
|
||||
}
|
||||
|
||||
log.WithError(err).Error("Failed to read from ship")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Run() error {
|
||||
defer c.Close()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-c.done:
|
||||
return nil
|
||||
default:
|
||||
c.read()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Close() {
|
||||
err := c.sh.Shutdown()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to send close message")
|
||||
}
|
||||
|
||||
close(c.done)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue