planetmint-go/monitor/interface.go
Jürgen Eckel 1f2b1702ea
Eckelj/mqtt monitoring fix (#398)
* set keepalive ping
* made mutex more granular
* avoid using app.Logger to have a consistent log from the beginning
* made lazyLoading local to the module
* fixed ticker bug so that the tasks are executed in an endless loop
* fixed timezone bug of Add Participant. only validator local time is used not the timestamp from the mqtt messages as these might come from a different time zone (resulted in delayed deletion from the activeActors DB)
* improved connection management for the monitor
* added a watchdog task to restart the monitor in case of connection loss (every 2 min)
* removed obsolete SetContext function/interface
* removed obsolete time conversion method

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2024-05-15 13:23:06 +02:00

66 lines
1.5 KiB
Go

package monitor
import (
"sync"
"github.com/planetmint/planetmint-go/config"
"github.com/syndtr/goleveldb/leveldb"
)
type MQTTMonitorClientI interface {
AddParticipant(address string, lastSeenTS int64) (err error)
SelectPoPParticipantsOutOfActiveActors() (challenger string, challengee string, err error)
Start() (err error)
}
var monitorMutex sync.Mutex
var mqttMonitorInstance MQTTMonitorClientI
func SetMqttMonitorInstance(monitorInstance MQTTMonitorClientI) {
monitorMutex.Lock()
mqttMonitorInstance = monitorInstance
monitorMutex.Unlock()
}
func LazyMqttMonitorLoader(homeDir string) {
monitorMutex.Lock()
tmpInstance := mqttMonitorInstance
monitorMutex.Unlock()
if tmpInstance != nil {
return
}
if homeDir == "" {
homeDir = "./"
}
aciveActorsDB, err := leveldb.OpenFile(homeDir+"activeActors.db", nil)
if err != nil {
panic(err)
}
monitorMutex.Lock()
mqttMonitorInstance = NewMqttMonitorService(aciveActorsDB, *config.GetConfig())
monitorMutex.Unlock()
err = mqttMonitorInstance.Start()
if err != nil {
panic(err)
}
}
func SelectPoPParticipantsOutOfActiveActors() (challenger string, challengee string, err error) {
monitorMutex.Lock()
challenger, challengee, err = mqttMonitorInstance.SelectPoPParticipantsOutOfActiveActors()
monitorMutex.Unlock()
return
}
func Start() (err error) {
err = mqttMonitorInstance.Start()
return
}
func AddParticipant(address string, lastSeenTS int64) (err error) {
monitorMutex.Lock()
err = mqttMonitorInstance.AddParticipant(address, lastSeenTS)
monitorMutex.Unlock()
return
}