Initial import

This commit is contained in:
George Suntres
2026-03-29 11:37:56 -04:00
commit 783e21ea9d
4 changed files with 101 additions and 0 deletions

54
main.go Normal file
View File

@@ -0,0 +1,54 @@
package sys
import (
"os"
"os/signal"
"syscall"
"sync"
"log"
)
var onExitMutex sync.Mutex
var onExitHandlers int
func init() {
onExitHandlers = 0
}
type OnExitFunc func()
func OnExit(fn OnExitFunc) {
onExitMutex.Lock()
onExitHandlers++
onExitMutex.Unlock()
// shutdown hook
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
// ctx, cancel := context.WithTimeout(context.Background(), *wait)
// defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
log.Println("Calling on exit...")
// controller.Stop()
fn()
onExitMutex.Lock()
onExitHandlers--
onExitMutex.Unlock()
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
if onExitHandlers == 0 {
log.Println("no more exit handler, will exit")
os.Exit(0)
}
}()
}