-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
50 lines (42 loc) · 1.07 KB
/
event.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
package cliutil
import (
"os"
"os/signal"
"syscall"
)
// EventListener listens for SIGINT and SIGTERM signals and notifies the
// shutdown channel if it detects that either was sent.
type EventListener struct {
signal chan os.Signal
shutdown chan bool
}
// NewEventListener returns an EventListener with the channels initialized.
func NewEventListener() *EventListener {
return &EventListener{
signal: make(chan os.Signal),
shutdown: make(chan bool),
}
}
// Run runs the event listener in a goroutine and sends e message to
// EventListener.shutdown if a SIGINT or SIGTERM signal is detected.
func (e *EventListener) Run() *EventListener {
signal.Notify(e.signal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
for {
select {
case <-e.signal:
e.shutdown <- true
break
}
}
}()
return e
}
// Wait waits for EventListener.shutdown to receive a message.
func (e *EventListener) Wait() {
<-e.shutdown
}
// StopSignal stops relaying incoming signals to EventListener.signal.
func (e *EventListener) StopSignal() {
signal.Stop(e.signal)
}