diff options
author | Valery Piashchynski <[email protected]> | 2023-08-14 17:05:11 +0200 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2023-08-14 17:05:11 +0200 |
commit | 00d24df903004641c739f83e755cf1800c3c81a4 (patch) | |
tree | 9643c1f9ceda50363b86e40b2aa2282e2f7de5a2 /internal | |
parent | 0a48c5557e6bfeada5fc143b138cd1b8ee6374d1 (diff) |
chore: watchgod, configuration update
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'internal')
-rw-r--r-- | internal/cli/serve/command.go | 8 | ||||
-rw-r--r-- | internal/sdnotify/sdnotify.go | 17 |
2 files changed, 24 insertions, 1 deletions
diff --git a/internal/cli/serve/command.go b/internal/cli/serve/command.go index 21454933..a4caa265 100644 --- a/internal/cli/serve/command.go +++ b/internal/cli/serve/command.go @@ -114,9 +114,15 @@ func NewCommand(override *[]string, cfgFile *string, silent *bool) *cobra.Comman if !*silent { if notified { fmt.Println("[INFO] sdnotify: notified") + stopCh := make(chan struct{}, 1) + if containerCfg.WatchdogSec > 0 { + fmt.Printf("[INFO] sdnotify: watchdog enabled, timeout: %d seconds\n", containerCfg.WatchdogSec) + sdnotify.StartWatchdog(containerCfg.WatchdogSec, stopCh) + } + // if notified -> notify about stop defer func() { - _, _ = sdnotify.SdNotify(sdnotify.Stopping) + stopCh <- struct{}{} }() } else { fmt.Println("[INFO] sdnotify: not notified") diff --git a/internal/sdnotify/sdnotify.go b/internal/sdnotify/sdnotify.go index aed38c92..d88c0e88 100644 --- a/internal/sdnotify/sdnotify.go +++ b/internal/sdnotify/sdnotify.go @@ -25,6 +25,7 @@ package sdnotify import ( "net" "os" + "time" ) type State string @@ -84,3 +85,19 @@ func SdNotify(state State) (bool, error) { return true, nil } + +func StartWatchdog(interval int, stopCh chan struct{}) { + go func() { + ticker := time.NewTicker(time.Duration(interval) * time.Second) + defer ticker.Stop() + + for { + select { + case <-stopCh: + return + case <-ticker.C: + _, _ = SdNotify(Watchdog) + } + } + }() +} |