2024-10-12 10:59:11 -05:00
|
|
|
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
|
2024-10-12 11:54:01 -05:00
|
|
|
h.MyTicker(h.Delay, h.Hostname, func() {
|
2024-10-12 10:59:11 -05:00
|
|
|
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:
|
2024-10-12 11:54:01 -05:00
|
|
|
log.Log(POLL, "Watchdog() ticked", name, "Current time: ", t)
|
2024-10-12 10:59:11 -05:00
|
|
|
h.Scan()
|
|
|
|
// f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|