From 2d5370c9a6dd8ab566ec2943e20738b31a7a6606 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 12 Oct 2025 00:11:00 -0500 Subject: restructure code --- exit.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'exit.go') diff --git a/exit.go b/exit.go index c6390b5..5a6c5a1 100644 --- a/exit.go +++ b/exit.go @@ -1,6 +1,7 @@ package prep import ( + "fmt" "os" "time" @@ -8,15 +9,21 @@ import ( "go.wit.com/log" ) -// initializes logging and command line options +// since we know when the command starts (duh, this parses os.Args) +// this is a convienent way to provide a standard exit format back +// to the shell that also has built in timing! + +// also, it supports a custom Exit() back to your application func (pb *Auto) GoodExit(msg string) { + go ExitWatchdog() dur := time.Since(pb.Ctime.AsTime()) log.Infof("%s: %s (%s)\n", pb.Argname, msg, config.FormatDuration(dur)) os.Exit(0) } func (pb *Auto) BadExit(msg string, err error) { + go ExitWatchdog() if err != nil { log.Info(err) } @@ -24,3 +31,28 @@ func (pb *Auto) BadExit(msg string, err error) { log.Infof("%s: %s (%s)\n", pb.Argname, msg, config.FormatDuration(dur)) os.Exit(-1) } + +// this code doesn't need to be this complicated. I put it here as reference code for myself so I could remember where it is. +func ExitWatchdog() { + dog := time.NewTicker(time.Second) + defer dog.Stop() + dogchan := 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 <-dogchan: + fmt.Println("Done!") + return + case t := <-dog.C: + _ = t + log.Info("argv.Exit() watchdog: stalled in", myAuto.appName+".Exit()") + // h.Scan() + } + } +} -- cgit v1.2.3