summaryrefslogtreecommitdiff
path: root/watchdog.go
blob: 41bc698ed16c1599d8177850ede670aba7776674 (plain)
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
package main

import (
	"fmt"
	"os"
	"time"

	"go.wit.com/log"
)

// scan hypervisors every i seconds
func (h *HyperT) NewWatchdog() {
	var delay int = 99
	var i int = delay
	h.MyTicker(h.Delay, h.Hostname, func() {
		i += 1
		// check if the env var is set to autoscan
		if os.Getenv("WATCHDOG_AUTO_SCAN") != "true" {
			if i < delay {
				i = delay
			}
			// print every 'delay' seconds
			if i%delay == 0 {
				log.Info("Not auto scanning", i, "WATCHDOG_AUTO_SCAN =", os.Getenv("WATCHDOG_AUTO_SCAN"))
			}
			return
		}
		if i < delay {
			return
		}
		i = 0
	})
}

// timeFunction takes a function as an argument and returns the execution time.
func TimeFunction(f func()) time.Duration {
	startTime := time.Now()      // Record the start time
	f()                          // Execute the function
	return time.Since(startTime) // Calculate the elapsed time
}

func (h *HyperT) MyTicker(t time.Duration, name string, f func()) {
	h.Dog = time.NewTicker(t)
	defer h.Dog.Stop()
	done := make(chan bool)
	/*
		// this example would exit/destroy the ticker in 10 seconds
		go func() {
			time.Sleep(10 * time.Second)
			done <- true
		}()
	*/
	for {
		select {
		case <-done:
			fmt.Println("Done!")
			return
		case t := <-h.Dog.C:
			log.Log(POLL, "Watchdog() ticked", name, "Current time: ", t)
			h.Scan()
			// f()
		}
	}
}