-
Notifications
You must be signed in to change notification settings - Fork 0
/
canceloninterruptorterminate.go
43 lines (34 loc) · 1.36 KB
/
canceloninterruptorterminate.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
// OS-related utilities
package osutil
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"github.com/function61/gokit/log/logex"
)
// makes (background-derived) context that cancels on SIGINT ("Ctrl + c") or SIGTERM. it
// stops signal listening on first received signal, so that if user sends next signal
// (teardown maybe stuck?), it terminates the process immediately.
//
// TODO: will signal.NotifyContext() cover our needs?
func CancelOnInterruptOrTerminate(logger *log.Logger) context.Context {
ctx, cancel := context.WithCancel(context.Background())
// need a buffered channel because the sending side is non-blocking
ch := make(chan os.Signal, 1)
// "The SIGINT signal is sent to a process by its controlling terminal when a user wishes to interrupt the process"
// "The SIGTERM signal is sent to a process to request its termination"
// "SIGINT is nearly identical to SIGTERM"
// https://en.wikipedia.org/wiki/Signal_(IPC)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
go func() {
logex.Levels(logger).Info.Printf("Got %s: STOPPING. (If stuck, send sig again to force exit.)", <-ch)
// stop accepting signals on the channel. this undoes the effect of this func,
// and thus makes the process terminate on the next received signal (so you can stop
// your program if the cleanup code gets stuck)
signal.Stop(ch)
cancel()
}()
return ctx
}