|
|
@@ -0,0 +1,41 @@
|
|
|
+package containers
|
|
|
+
|
|
|
+import (
|
|
|
+ log "github.com/sirupsen/logrus"
|
|
|
+ "os"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+var hbStopChan = make(chan struct{})
|
|
|
+
|
|
|
+func DoHeartbeat(hbFilePath string) {
|
|
|
+ defer func() {
|
|
|
+ if err := recover(); err != nil {
|
|
|
+ log.Errorf("DoHeartbeat panic: %v", err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ doHeartbeat(hbFilePath)
|
|
|
+ ticker := time.NewTicker(time.Second * 30)
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case <-ticker.C:
|
|
|
+ doHeartbeat(hbFilePath)
|
|
|
+ case <-hbStopChan:
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func doHeartbeat(hbFilePath string) {
|
|
|
+ timeUnix := time.Now().Unix()
|
|
|
+ timeStr := strconv.FormatInt(timeUnix, 10)
|
|
|
+ if err := os.WriteFile(hbFilePath, []byte(timeStr), 0644); err != nil {
|
|
|
+ log.Errorf("write heartbeat to file [%s] occurs error: %v", hbFilePath, err.Error())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func StopHeartbeat() {
|
|
|
+ close(hbStopChan)
|
|
|
+}
|