-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
133 lines (99 loc) · 2.82 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package indexer
import (
"encoding/binary"
"time"
"github.com/dynamicgo/config"
"github.com/dynamicgo/slf4go"
"github.com/inwecrypto/neogo/rpc"
"github.com/syndtr/goleveldb/leveldb"
)
var key = []byte("key")
// Monitor indexer server
type Monitor struct {
slf4go.Logger
client *rpc.Client
etl *ETL
pullDuration time.Duration
db *leveldb.DB
}
// NewMonitor .
func NewMonitor(conf *config.Config) (*Monitor, error) {
client := rpc.NewClient(conf.GetString("indexer.neo", "http://localhost:8545"))
etl, err := newETL(conf)
if err != nil {
return nil, err
}
db, err := leveldb.OpenFile(conf.GetString("indexer.localdb", "./cursor"), nil)
if err != nil {
return nil, err
}
startindexer := uint64(conf.GetInt64("indexer.start", 0))
monitor := &Monitor{
Logger: slf4go.Get("neo-monitor"),
client: client,
etl: etl,
pullDuration: time.Second * conf.GetDuration("indexer.pull", 4),
db: db,
}
if monitor.getCursor() < startindexer {
if err := monitor.setCursor(startindexer); err != nil {
return nil, err
}
}
return monitor, nil
}
// Run .
func (monitor *Monitor) Run() {
ticker := time.NewTicker(monitor.pullDuration)
for range ticker.C {
monitor.DebugF("fetch geth last block number ...")
blocks, err := monitor.client.GetBlockCount()
monitor.DebugF("fetch geth last block number -- success, %d", blocks)
if err != nil {
monitor.ErrorF("fetch geth blocks err, %s", err)
}
for monitor.getCursor() < uint64(blocks) {
if err := monitor.fetchBlock(); err != nil {
break
}
}
}
}
func (monitor *Monitor) fetchBlock() error {
blockNumber := monitor.getCursor()
monitor.DebugF("fetch block(%d) ...", blockNumber)
block, err := monitor.client.GetBlockByIndex(int64(blockNumber))
if err != nil {
monitor.ErrorF("fetch geth block(%d) err, %s", blockNumber, err)
return err
}
monitor.DebugF("fetch block(%d) -- success", blockNumber)
monitor.DebugF("etl handle block(%d) ...", blockNumber)
if err := monitor.etl.Handle(block); err != nil {
monitor.ErrorF("etl handle geth block(%d) err, %s", blockNumber, err)
return err
}
monitor.DebugF("etl handle block(%d) -- success", blockNumber)
if err := monitor.setCursor(blockNumber + 1); err != nil {
monitor.ErrorF("monitor set cursor(%d) err, %s", blockNumber, err)
return err
}
return nil
}
func (monitor *Monitor) getCursor() uint64 {
buff, err := monitor.db.Get(key, nil)
if err != nil {
monitor.ErrorF("get Monitor local cursor error :%s", err)
return 0
}
if buff == nil {
monitor.ErrorF("get Monitor local cursor error : cursor not exists")
return 0
}
return binary.BigEndian.Uint64(buff)
}
func (monitor *Monitor) setCursor(cursor uint64) error {
buff := make([]byte, 8)
binary.BigEndian.PutUint64(buff, cursor)
return monitor.db.Put(key, buff, nil)
}