watchdog.gno

0.57 Kb ยท 39 lines
 1package watchdog
 2
 3import "time"
 4
 5type Watchdog struct {
 6	Duration   time.Duration
 7	lastUpdate time.Time
 8	lastDown   time.Time
 9}
10
11func (w *Watchdog) Alive() {
12	now := time.Now()
13	if !w.IsAlive() {
14		w.lastDown = now
15	}
16	w.lastUpdate = now
17}
18
19func (w Watchdog) Status() string {
20	if w.IsAlive() {
21		return "OK"
22	}
23	return "KO"
24}
25
26func (w Watchdog) IsAlive() bool {
27	return time.Since(w.lastUpdate) < w.Duration
28}
29
30func (w Watchdog) UpSince() time.Time {
31	return w.lastDown
32}
33
34func (w Watchdog) DownSince() time.Time {
35	if !w.IsAlive() {
36		return w.lastUpdate
37	}
38	return time.Time{}
39}