feat(*) exit if parent died

Run a goroutine every minute to check if parent died and exit.
Assumption: if parent dies, the process gets adopted by init (pid 1).
This commit is contained in:
Guilherme Salazar
2020-01-10 21:00:44 -08:00
parent 63003ae67a
commit 479ed3f974

16
main.go
View File

@@ -12,6 +12,7 @@ import (
"os"
"reflect"
"fmt"
"time"
)
var version = "development"
@@ -99,6 +100,10 @@ func startServer() {
runServer(listener)
}
func isParentAlive() bool {
return os.Getppid() != 1 // assume ppid 1 means process was adopted by init
}
func main() {
if *showVersion == true {
printVersion()
@@ -111,6 +116,17 @@ func main() {
}
if socket != "" {
go func() {
for {
if ! isParentAlive() {
log.Printf("Kong exited; shutting down...")
os.Exit(0)
}
time.Sleep(1 * time.Second)
}
}()
startServer()
}
}