This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
monitor.go
104 lines (89 loc) · 2.92 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
package bux
import (
"bufio"
"context"
"errors"
"fmt"
"os"
"time"
"github.com/BuxOrg/bux/chainstate"
"github.com/BuxOrg/bux/cluster"
"github.com/BuxOrg/bux/utils"
"github.com/mrz1836/go-datastore"
)
// destinationMonitor is the struct of responses for Monitoring
type destinationMonitor struct {
LockingScript string `json:"locking_script" toml:"locking_script" yaml:"locking_script" bson:"locking_script"`
}
// loadMonitoredDestinations will load destinations that should be monitored
func loadMonitoredDestinations(ctx context.Context, client ClientInterface, monitor chainstate.MonitorService) error {
// Create conditions using the max monitor days
conditions := map[string]interface{}{
"monitor": map[string]interface{}{
"$gt": time.Now().Add(time.Duration(-24*monitor.GetMonitorDays()) * time.Hour),
},
}
// Create monitor query with max destinations
queryParams := &datastore.QueryParams{
Page: 1,
PageSize: monitor.GetMaxNumberOfDestinations(),
OrderByField: "monitor",
SortDirection: "desc",
}
// Get all destinations that match the query
var destinations []*destinationMonitor
if err := client.Datastore().GetModels(
ctx, &[]*Destination{}, conditions, queryParams, &destinations, defaultDatabaseReadTimeout,
); err != nil && !errors.Is(err, datastore.ErrNoResults) {
return err
}
// Loop all destinations and add to Monitor
for _, model := range destinations {
if err := monitor.Processor().Add(utils.P2PKHRegexpString, model.LockingScript); err != nil {
return err
}
}
// Debug line
if client.IsDebug() && client.Logger() != nil {
client.Logger().Info(ctx, fmt.Sprintf(
"[MONITOR] Added %d destinations to monitor with hash %s",
len(destinations), monitor.Processor().GetHash(),
))
}
return nil
}
// startDefaultMonitor will create a handler, start monitor, and store the first heartbeat
func startDefaultMonitor(ctx context.Context, client ClientInterface, monitor chainstate.MonitorService) error {
if client.Chainstate().Monitor().LoadMonitoredDestinations() {
if err := loadMonitoredDestinations(ctx, client, monitor); err != nil {
return err
}
}
_, err := client.Cluster().Subscribe(cluster.DestinationNew, func(data string) {
if monitor.IsDebug() {
monitor.Logger().Info(ctx, fmt.Sprintf("[MONITOR] added %s destination to monitor: %s", utils.P2PKHRegexpString, data))
}
if err := monitor.Processor().Add(utils.P2PKHRegexpString, data); err != nil {
client.Logger().Error(ctx, "could not add destination to monitor")
}
})
if err != nil {
return err
}
if monitor.IsDebug() {
// capture keyboard input and allow start and stop of the monitor
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
text := scanner.Text()
fmt.Printf("KEYBOARD input: %s\n", text)
if text == "e" {
if err = monitor.Stop(ctx); err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
}
}
}
}()
}
return nil
}