-
Notifications
You must be signed in to change notification settings - Fork 128
/
daemon.go
192 lines (173 loc) · 4.16 KB
/
daemon.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package modd
import (
"os"
"os/exec"
"sync"
"time"
"github.com/cortesi/modd/conf"
"github.com/cortesi/modd/shell"
"github.com/cortesi/modd/varcmd"
"github.com/cortesi/termlog"
)
const (
// MinRestart is the minimum amount of time between daemon restarts
MinRestart = 500 * time.Millisecond
// MulRestart is the exponential backoff multiplier applied when the daemon exits uncleanly
MulRestart = 2
// MaxRestart is the maximum amount of time between daemon restarts
MaxRestart = 8 * time.Second
)
// A single daemon
type daemon struct {
conf conf.Daemon
indir string
ex *shell.Executor
log termlog.Stream
shell string
stop bool
sync.Mutex
}
func (d *daemon) Run() {
var lastStart time.Time
delay := MinRestart
for d.stop != true {
if delay > MinRestart {
d.log.Notice(">> restart backoff... %dms", delay/time.Millisecond)
}
if !lastStart.IsZero() {
time.Sleep(delay)
}
d.log.Notice(">> starting...")
lastStart = time.Now()
err, pstate := d.ex.Run(d.log, false)
if err != nil {
d.log.Shout("execution error: %s", err)
} else if pstate.Error != nil {
if _, ok := pstate.Error.(*exec.ExitError); ok {
d.log.Warn("exited: %s", pstate.ProcState)
} else {
d.log.Shout("exited: %s", err)
}
} else {
d.log.Warn("exited: %s", pstate.ProcState)
}
// If we exited cleanly, or the process ran for > MaxRestart, we reset
// the delay timer
if time.Now().Sub(lastStart) > MaxRestart {
delay = MinRestart
} else {
delay *= MulRestart
if delay > MaxRestart {
delay = MaxRestart
}
}
}
}
// Restart the daemon, or start it if it's not yet running
func (d *daemon) Restart() {
d.Lock()
defer d.Unlock()
if d.ex == nil {
ex, err := shell.NewExecutor(d.shell, d.conf.Command, d.indir)
if err != nil {
d.log.Shout("Could not create executor: %s", err)
}
d.ex = ex
go d.Run()
} else {
d.log.Notice(">> sending signal %s", d.conf.RestartSignal)
err := d.ex.Signal(d.conf.RestartSignal)
if err != nil {
d.log.Warn(
"failed to send %s signal to %s: %v", d.conf.RestartSignal, d.conf.Command, err,
)
}
}
}
func (d *daemon) Shutdown(sig os.Signal) error {
d.log.Notice(">> stopping")
d.stop = true
if d.ex != nil {
return d.ex.Stop()
}
return nil
}
// DaemonPen is a group of daemons in a single block, managed as a unit.
type DaemonPen struct {
daemons []*daemon
sync.Mutex
}
// NewDaemonPen creates a new DaemonPen
func NewDaemonPen(block conf.Block, vars map[string]string, log termlog.TermLog) (*DaemonPen, error) {
d := make([]*daemon, len(block.Daemons))
for i, dmn := range block.Daemons {
vcmd := varcmd.VarCmd{Block: nil, Modified: nil, Vars: vars}
finalcmd, err := vcmd.Render(dmn.Command)
if err != nil {
return nil, err
}
dmn.Command = finalcmd
var indir string
if block.InDir != "" {
indir = block.InDir
} else {
indir, err = os.Getwd()
if err != nil {
return nil, err
}
}
sh, err := shell.GetShellName(vars[shellVarName])
if err != nil {
return nil, err
}
d[i] = &daemon{
conf: dmn,
log: log.Stream(niceHeader("daemon: ", dmn.Command)),
shell: sh,
indir: indir,
}
}
return &DaemonPen{daemons: d}, nil
}
// Restart all daemons in the pen, or start them if they're not running yet.
func (dp *DaemonPen) Restart() {
dp.Lock()
defer dp.Unlock()
if dp.daemons != nil {
for _, d := range dp.daemons {
d.Restart()
}
}
}
// Shutdown all daemons in the pen
func (dp *DaemonPen) Shutdown(sig os.Signal) {
dp.Lock()
defer dp.Unlock()
if dp.daemons != nil {
for _, d := range dp.daemons {
d.Shutdown(sig)
}
}
}
// DaemonWorld represents the entire world of daemons
type DaemonWorld struct {
DaemonPens []*DaemonPen
}
// NewDaemonWorld creates a DaemonWorld
func NewDaemonWorld(cnf *conf.Config, log termlog.TermLog) (*DaemonWorld, error) {
daemonPens := make([]*DaemonPen, len(cnf.Blocks))
for i, b := range cnf.Blocks {
d, err := NewDaemonPen(b, cnf.GetVariables(), log)
if err != nil {
return nil, err
}
daemonPens[i] = d
}
return &DaemonWorld{daemonPens}, nil
}
// Shutdown all daemons with signal s
func (dw *DaemonWorld) Shutdown(s os.Signal) {
for _, dp := range dw.DaemonPens {
dp.Shutdown(s)
}
}