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
117 lines (97 loc) · 3.69 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
package bux
import (
"context"
"errors"
"fmt"
"strconv"
"time"
"github.com/BuxOrg/bux/chainstate"
"github.com/BuxOrg/bux/datastore"
"github.com/BuxOrg/bux/utils"
)
// 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", len(destinations)))
}
return nil
}
// checkMonitorHeartbeat will check for a Monitor heartbeat key and detect if it's locked or not
func checkMonitorHeartbeat(ctx context.Context, client ClientInterface, lockID string) (bool, error) {
// Make sure cachestore is loaded (safety check)
cs := client.Cachestore()
if cs == nil {
return false, nil
}
// Check if there is already a monitor loaded using this unique lock id & detect last heartbeat
lastHeartBeatString, _ := cs.Get(ctx, fmt.Sprintf(lockKeyMonitorLockID, lockID))
if len(lastHeartBeatString) > 0 { // Monitor has already loaded with this LockID
currentUnixTime := time.Now().UTC().Unix()
// Convert the heartbeat and then compare
unixTime, err := strconv.Atoi(lastHeartBeatString)
if err != nil {
return false, err
}
// Heartbeat is good - skip loading the monitor
if int64(unixTime+defaultMonitorHeartbeatMax) > currentUnixTime {
client.Logger().Info(ctx, fmt.Sprintf("monitor has already been loaded using this lockID: %s last heartbeat: %d", lockID, unixTime))
return true, nil
}
client.Logger().Info(ctx, fmt.Sprintf("found monitor lockID: %s but heartbeat is out of range: %d vs %d", lockID, unixTime, currentUnixTime))
}
return false, nil
}
// startDefaultMonitor will create a handler, start monitor, and store the first heartbeat
func startDefaultMonitor(ctx context.Context, client ClientInterface, monitor chainstate.MonitorService) error {
// Create a handler and load destinations if option has been set
handler := NewMonitorHandler(ctx, client, monitor)
if client.Chainstate().Monitor().LoadMonitoredDestinations() {
if err := loadMonitoredDestinations(ctx, client, monitor); err != nil {
return err
}
}
// Start the monitor
if err := monitor.Start(ctx, &handler); err != nil {
return err
}
// Set the cache-key lock for this monitor (with a heartbeat time of now)
if len(monitor.GetLockID()) > 0 {
return client.Cachestore().Set(
ctx,
fmt.Sprintf(lockKeyMonitorLockID, monitor.GetLockID()),
fmt.Sprintf("%d", time.Now().UTC().Unix()),
)
}
return nil
}