-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools.go
64 lines (49 loc) · 1.46 KB
/
tools.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package tools
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/switchupcb/disgo"
"golang.org/x/sync/errgroup"
)
var (
// Signals represents common termination signals used to terminate the program.
//
// https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
Signals = []os.Signal{
syscall.SIGTERM,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGHUP,
}
)
// InterceptSignal blocks until the provided signals are intercepted.
//
// Upon receiving a signal, the given sessions are gracefully disconnected.
func InterceptSignal(signals []os.Signal, sessions ...*disgo.Session) error {
signalChannel := make(chan os.Signal, 1)
// set the syscalls that signalChannel is sent.
signal.Notify(signalChannel, signals...)
// block the calling goroutine until a signal is received.
<-signalChannel
disgo.Logger.Info().Msg("Closing sessions due to signal...")
eg := errgroup.Group{}
for _, session := range sessions {
s := session
eg.Go(func() error {
if err := s.Disconnect(); err != nil {
err = fmt.Errorf("error closing connection to Discord Gateway: %w", err)
disgo.LogSession(disgo.Logger.Error(), s.ID).Err(err).Msg("")
return err
}
return nil
})
}
if err := eg.Wait(); err != nil {
disgo.Logger.Warn().Msg("Not all sessions were closed successfully.")
return fmt.Errorf("error during signal intercept for termination: %w", err)
}
disgo.Logger.Info().Msg("Closed sessions successfully.")
return nil
}