aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-07-05 18:30:03 -0700
committerFuwn <[email protected]>2021-07-05 18:30:03 -0700
commit41229be20da92dda7d3cd98a8f70b1465a2c98ab (patch)
tree6e8b96ecf2bbd9abf8038e4cac5eb670a8e07bf9
parentstyle(copyright): add header to hex.go (diff)
downloadmunch-41229be20da92dda7d3cd98a8f70b1465a2c98ab.tar.xz
munch-41229be20da92dda7d3cd98a8f70b1465a2c98ab.zip
feat(munch): setup signal handler
This commit adds a signal handler which detects Ctrl+Cs and then properly shuts down all of Munch. Before, only one of Munch's Goroutines would be shut down to the full leaving a dangling Goroutine, in this case; the Worlds bot.
-rw-r--r--munch.go2
-rw-r--r--pkg/utilities/signal.go25
2 files changed, 27 insertions, 0 deletions
diff --git a/munch.go b/munch.go
index da1a28e..4624538 100644
--- a/munch.go
+++ b/munch.go
@@ -7,12 +7,14 @@ import (
"github.com/Whirlsplash/munch/pkg/config"
"github.com/Whirlsplash/munch/pkg/discord"
"github.com/Whirlsplash/munch/pkg/server"
+ "github.com/Whirlsplash/munch/pkg/utilities"
"github.com/spf13/viper"
"sync"
)
func main() {
config.Setup()
+ utilities.SetupSignalHandler()
var wg sync.WaitGroup
diff --git a/pkg/utilities/signal.go b/pkg/utilities/signal.go
new file mode 100644
index 0000000..00fbf1e
--- /dev/null
+++ b/pkg/utilities/signal.go
@@ -0,0 +1,25 @@
+// Copyright (C) 2021-2021 The Whirlsplash Collective
+// SPDX-License-Identifier: GPL-3.0-only
+
+package utilities
+
+import (
+ "log"
+ "os"
+ "os/signal"
+ "syscall"
+)
+
+func SetupSignalHandler() {
+ c := make(chan os.Signal)
+
+ signal.Notify(c, os.Interrupt, syscall.SIGTERM)
+
+ go func() {
+ <-c
+
+ log.Println("Killing Munch with SignalHandler")
+
+ os.Exit(1)
+ }()
+}