/
shell.go
76 lines (61 loc) · 1.76 KB
/
shell.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
package shell
import (
"context"
"os/exec"
"syscall"
"time"
"github.com/sirupsen/logrus"
"github.com/Shopify/goose/logger"
"github.com/Shopify/goose/statsd"
)
var log = logger.New("shell")
const defaultGracefulTerminationOnCancelPeriod = 3 * time.Second
type Env map[string]string
func New(ctx context.Context, path string, args ...string) Supervisor {
return NewDefaultBuilder(ctx, path, args...).Prepare()
}
func NewDefaultBuilder(ctx context.Context, path string, args ...string) Builder {
return NewBuilder(ctx, path, args...).
WithOSEnv().
WithSysProcAttr(&syscall.SysProcAttr{Setpgid: true}).
WithContextCancellation(defaultGracefulTerminationOnCancelPeriod)
}
func NewBuilder(ctx context.Context, path string, args ...string) Builder {
w := &wrapper{
path: path,
args: args,
}
w.ctx = logger.WithLoggable(ctx, w)
return w
}
type wrapper struct {
args []string
env []string
ctx context.Context
path string
dir string
cmd *exec.Cmd
sysProcAttr *syscall.SysProcAttr
// When a context is provided and it is canceled while the process is
// running, we send SIGTERM to the process. if, after this period, the
// process is still running, we send SIGKILL. If left unspecified, the
// default is 3 seconds.
gracefulTerminationOnCancelTimeout time.Duration
// Number of times the command was "killed" after the context was canceled.
killedByCancel uint32
osEnv bool
ctxCancellation bool
}
func (w *wrapper) StatsTags() statsd.Tags {
return statsd.Tags{
// Only report the command since the rest may contain sensitive information.
"cmdPath": w.path,
}
}
func (w *wrapper) LogFields() logrus.Fields {
return logrus.Fields{
"cmdPath": w.path,
"cmdArgs": w.args,
"cmdEnv": w.env,
}
}