-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (38 loc) · 877 Bytes
/
main.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
//go:build !windows
// シグナルを受信する側です
//
// # 処理手順
//
// - signal.NotifyContext を利用して SIGTERM をフック
// - シグナルが送信されるのを待機
//
// REFERENCES:
// - https://stackoverflow.com/questions/9030680/list-of-currently-running-process-in-go
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
)
var (
appLog = log.New(os.Stderr, "[receiver] >>> ", 0)
)
func main() {
var (
mainCtx = context.Background()
procCtx, procCxl = context.WithTimeout(mainCtx, 10*time.Second)
signalCtx, signalCxl = signal.NotifyContext(procCtx, syscall.SIGTERM)
)
defer procCxl()
defer signalCxl()
appLog.Println("wait for SIGTERM")
select {
case <-procCtx.Done():
appLog.Println("timeout")
case <-signalCtx.Done():
appLog.Println("receive SIGTERM from sender")
}
}