Utility function to handle exit signal
This commit is contained in:
parent
e5c81519bf
commit
ad0f5e0d0f
18
cli/cli.go
18
cli/cli.go
@ -3,7 +3,9 @@ package cli
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func SetDefaultValueFromEnv(value *string, key string, defaultValue string) {
|
||||
@ -28,3 +30,19 @@ func SetIntDefaultValueFromEnv(value *int, key string, defaultValue int) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Part interface {
|
||||
Start() error
|
||||
Stop()
|
||||
}
|
||||
|
||||
func HandleExit(p Part) {
|
||||
signals := make(chan os.Signal, 1)
|
||||
signal.Notify(signals, os.Kill, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
<-signals
|
||||
p.Stop()
|
||||
os.Exit(0)
|
||||
}()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user