-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimmortalize.go
128 lines (108 loc) · 2.65 KB
/
immortalize.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"errors"
"flag"
"github.com/sirupsen/logrus"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
)
var log = logrus.New()
func run(minLifetime uint, maxLifetime uint, command string) int {
cmd := exec.Command(command)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan,
syscall.SIGHUP,
syscall.SIGTERM,
syscall.SIGINT,
syscall.SIGQUIT)
cmd.Start()
lifetimeChan := make(chan bool)
if 0 < maxLifetime {
go func() {
time.Sleep(time.Duration(maxLifetime) * time.Second)
log.Info("Maximum lifetime has passed.")
cmd.Process.Signal(syscall.SIGTERM)
log.Info("Signal 'terminated' has been sent to the process.")
}()
}
go func() {
time.Sleep(time.Duration(minLifetime) * time.Second)
log.Info("Minimum lifetime has passed.")
for {
lifetimeChan <- true
}
}()
go func() {
for s := range signalChan {
if s == syscall.SIGTERM {
log.Infof("Signal '%v' has been received.", s)
<-lifetimeChan
}
cmd.Process.Signal(s)
log.Infof("Signal '%v' has been forwarded to the process.", s)
}
}()
var status int
if err := cmd.Wait(); err != nil {
if e2, ok := err.(*exec.ExitError); ok {
if s, ok := e2.Sys().(syscall.WaitStatus); ok {
status = s.ExitStatus()
} else {
log.Fatal("Failed to execute command")
}
}
} else {
status = 0
}
return status
}
func configLog(level string, logPath string) error {
var l logrus.Level
switch level {
case "trace":
l = logrus.TraceLevel
case "debug":
l = logrus.DebugLevel
case "info":
l = logrus.InfoLevel
default:
return errors.New("log level must be one of 'info', 'debug', or 'trace'")
}
log.SetFormatter(&logrus.JSONFormatter{})
log.SetLevel(l)
if logPath == "" {
log.Out = os.Stderr
return nil
}
f, err := os.Create(logPath)
if err != nil {
return err
}
log.Out = f
return nil
}
func main() {
minLifetimePtr := flag.Uint("min-lifetime", 0,
"Time duration for minimum process lifetime in seconds")
maxLifetimePtr := flag.Uint("max-lifetime", 0,
"Time duration for maximum process lifetime in seconds")
commandPtr := flag.String("command", "command", "command to immortalize")
levelPtr := flag.String(
"log-level", "info", "Log level: 'info', 'debug', or 'trace'")
logPathPtr := flag.String(
"log-path", "", "Log path: default to stderr")
flag.Parse()
if err := configLog(*levelPtr, *logPathPtr); err != nil {
panic(err)
}
log.Debugf("PID: %v", os.Getpid())
if 0 < *maxLifetimePtr && *maxLifetimePtr < *minLifetimePtr {
log.Fatal("min-lifetime cannot be higher than max-lifetime.")
}
os.Exit(run(*minLifetimePtr, *maxLifetimePtr, *commandPtr))
}