Skip to content

Commit 41229be

Browse files
committed
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.
1 parent 99cd753 commit 41229be

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

munch.go

+2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"github.com/Whirlsplash/munch/pkg/config"
88
"github.com/Whirlsplash/munch/pkg/discord"
99
"github.com/Whirlsplash/munch/pkg/server"
10+
"github.com/Whirlsplash/munch/pkg/utilities"
1011
"github.com/spf13/viper"
1112
"sync"
1213
)
1314

1415
func main() {
1516
config.Setup()
17+
utilities.SetupSignalHandler()
1618

1719
var wg sync.WaitGroup
1820

pkg/utilities/signal.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (C) 2021-2021 The Whirlsplash Collective
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
4+
package utilities
5+
6+
import (
7+
"log"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
)
12+
13+
func SetupSignalHandler() {
14+
c := make(chan os.Signal)
15+
16+
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
17+
18+
go func() {
19+
<-c
20+
21+
log.Println("Killing Munch with SignalHandler")
22+
23+
os.Exit(1)
24+
}()
25+
}

0 commit comments

Comments
 (0)