monit.gno
1.37 Kb ยท 53 lines
1// Package monit links a monitoring system with the chain in both directions.
2//
3// The agent will periodically call Incr() and verify that the value is always
4// higher than the previously known one. The contract will store the last update
5// time and use it to detect whether or not the monitoring agent is functioning
6// correctly.
7package monit
8
9import (
10 "std"
11 "time"
12
13 "gno.land/p/demo/ownable"
14 "gno.land/p/demo/ufmt"
15 "gno.land/p/demo/watchdog"
16)
17
18var (
19 counter int
20 lastUpdate time.Time
21 lastCaller std.Address
22 watchdogDuration = 5 * time.Minute
23 wd = watchdog.Watchdog{Duration: watchdogDuration}
24 Ownable = ownable.NewWithOrigin()
25)
26
27// Incr increments the counter and informs the watchdog that we're alive.
28// This function can be called by anyone.
29func Incr(cur realm) int {
30 counter++
31 lastUpdate = time.Now()
32 lastCaller = std.PreviousRealm().Address()
33 wd.Alive()
34 return counter
35}
36
37// Reset resets the realm state.
38// This function can only be called by the admin.
39func Reset(cur realm) {
40 Ownable.AssertOwnedByPrevious()
41 counter = 0
42 lastCaller = std.PreviousRealm().Address()
43 lastUpdate = time.Now()
44 wd = watchdog.Watchdog{Duration: watchdogDuration}
45}
46
47func Render(_ string) string {
48 status := wd.Status()
49 return ufmt.Sprintf(
50 "counter=%d\nlast update=%s\nlast caller=%s\nstatus=%s",
51 counter, lastUpdate, lastCaller, status,
52 )
53}