summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-11-15 09:12:08 -0600
committerJeff Carr <[email protected]>2024-11-15 09:12:08 -0600
commit7b069233955c6f35270f0cb2cb49bf6038c11020 (patch)
tree15515a666395193f7f0aebccdc6cc68573267d3a
parent6c920e0925c5b9db4354c31a693140adf024d88b (diff)
add watchdog
-rw-r--r--Makefile2
-rw-r--r--main.go17
-rw-r--r--structs.go9
-rw-r--r--watchdog.go39
4 files changed, 56 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index 9d275bc..24726cb 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE=
all:
GO111MODULE=off go build -v -x -ldflags "-X main.VERSION=${VERSION} -X gui.GUIVERSION=${VERSION}"
- #./zood
+ ./zood
./zood --version
# this is for release builds using the go.mod files
diff --git a/main.go b/main.go
index 7c17d5e..ab7717c 100644
--- a/main.go
+++ b/main.go
@@ -18,6 +18,7 @@ import (
"embed"
"fmt"
"os"
+ "time"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/log"
@@ -37,6 +38,13 @@ func main() {
os.Exit(0)
}
+ me = new(stuff)
+ me.Zookeeper = "zookeeper.wit.com"
+ me.Hostname, _ = os.Hostname()
+ me.pollDelay = 3 * time.Second
+
+ log.DaemonMode(true)
+
installedPackages, err := getInstalledPackages()
if err != nil {
fmt.Println("Error:", err)
@@ -47,14 +55,7 @@ func main() {
for pkg, version := range installedPackages {
fmt.Printf("%s: %s\n", pkg, version)
}
- os.Exit(0)
-
- me = new(stuff)
- me.Hostname, _ = os.Hostname()
-
- log.DaemonMode(true)
-
- // reportOS()
+ go NewWatchdog()
startHTTP()
}
diff --git a/structs.go b/structs.go
index 67ca6c1..a8d7492 100644
--- a/structs.go
+++ b/structs.go
@@ -1,9 +1,14 @@
package main
+import "time"
+
var me *stuff
// this app's variables
type stuff struct {
- Hostname string
- dirs []string
+ Hostname string // my hostname to send to zookeeper
+ Zookeeper string // the dns name for the zookeeper
+ pollDelay time.Duration // how often to report our status
+ dog *time.Ticker // the watchdog timer
+ dirs []string
}
diff --git a/watchdog.go b/watchdog.go
new file mode 100644
index 0000000..38b1959
--- /dev/null
+++ b/watchdog.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+ "fmt"
+ "time"
+
+ "go.wit.com/log"
+)
+
+// 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 NewWatchdog() {
+ me.dog = time.NewTicker(me.pollDelay)
+ defer me.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 := <-me.dog.C:
+ log.Info("Watchdog() ticked", me.Zookeeper, "Current time: ", t)
+ // h.pollHypervisor()
+ // h.Scan()
+ }
+ }
+}