-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
49 lines (38 loc) · 1.18 KB
/
listener.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
package termination_listener
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func New(terminationChannel chan bool) *Listener {
fmt.Println("[INFO] Create new termination signals listener")
return &Listener{
syscallCh: make(chan os.Signal),
terminationChannel: terminationChannel,
// stopChannel: make(chan bool, 1),
}
}
func (l *Listener) Start() {
if !l.active {
fmt.Println("[INFO] Start termination signals listener (os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL)")
signal.Notify(l.syscallCh, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL)
go l.listen()
l.active = true
fmt.Printf("[DEBUG] Termination signals listener active %t \n", l.active)
} else {
fmt.Println("[WARN] Termination signals listener already running")
}
}
func (l *Listener) Shutdown(timeout time.Duration) {
if l.active {
fmt.Printf("[WARN] Shutdown termination signals listener, timeout %.0f sec \n", timeout.Seconds())
// l.stopChannel <- true
time.Sleep(timeout)
// close(l.stopChannel)
close(l.syscallCh)
} else {
fmt.Println("[WARN] Termination signals listener not running yet")
}
}