From 688ac52705c2e4bd015e8f369969de6a91e26d80 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 22 May 2023 00:26:30 +0300 Subject: catch signals for shutdown --- cmd/gorum/main.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'cmd') diff --git a/cmd/gorum/main.go b/cmd/gorum/main.go index 223da9d..e39f620 100644 --- a/cmd/gorum/main.go +++ b/cmd/gorum/main.go @@ -4,6 +4,10 @@ import ( "context" "flag" "fmt" + "log" + "os" + "os/signal" + "syscall" "codeberg.org/snonux/gorum/internal" ) @@ -21,8 +25,24 @@ func main() { return } - ctx, cancel := context.WithCancel(context.Background()) + ctx, cancel := contextWithSignal() defer cancel() - internal.Run(ctx, *configFile) + internal.Start(ctx, *configFile) + + <-ctx.Done() + log.Println("Good bye!") +} + +func contextWithSignal() (context.Context, context.CancelFunc) { + ctx, cancel := context.WithCancel(context.Background()) + + go func() { + ch := make(chan os.Signal) + signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) + <-ch + cancel() + }() + + return ctx, cancel } -- cgit v1.2.3