forked from cortesi/modd
/
daemon.go
185 lines (165 loc) · 3.62 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
package modd
import (
"os"
"os/exec"
"sync"
"time"
"github.com/cortesi/modd/conf"
"github.com/cortesi/modd/varcmd"
"github.com/cortesi/termlog"
)
// MinRestart is the minimum amount of time between daemon restarts
const MinRestart = 1 * time.Second
// A single daemon
type daemon struct {
conf conf.Daemon
log termlog.Stream
cmd *exec.Cmd
stop bool
sync.Mutex
}
func (d *daemon) Run() {
var lastStart time.Time
for d.stop != true {
d.log.Notice(">> starting...")
since := time.Now().Sub(lastStart)
if since < MinRestart {
time.Sleep(MinRestart - since)
}
lastStart = time.Now()
sh, err := getShell()
if err != nil {
d.log.Shout("%s", err)
return
}
c := exec.Command(sh, "-c", d.conf.Command)
stdo, err := c.StdoutPipe()
if err != nil {
d.log.Shout("%s", err)
continue
}
stde, err := c.StderrPipe()
if err != nil {
d.log.Shout("%s", err)
continue
}
wg := sync.WaitGroup{}
wg.Add(2)
go logOutput(&wg, stde, d.log.Warn)
go logOutput(&wg, stdo, d.log.Say)
d.Lock()
err = c.Start()
if err != nil {
d.log.Shout("%s", err)
d.Unlock()
continue
}
d.cmd = c
d.Unlock()
wg.Wait()
err = c.Wait()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
d.log.Warn("exited: %s", c.ProcessState.String())
} else {
d.log.Shout("exited: %s", err)
}
} else {
d.log.Warn("exited: %s", c.ProcessState.String())
}
}
}
func (d *daemon) Restart() {
d.Lock()
defer d.Unlock()
if d.cmd != nil {
d.log.Notice(">> sending signal %s", d.conf.RestartSignal)
d.cmd.Process.Signal(d.conf.RestartSignal)
}
}
func (d *daemon) Shutdown(sig os.Signal) {
d.Lock()
defer d.Unlock()
d.stop = true
if d.cmd != nil {
d.cmd.Process.Signal(sig)
}
}
// 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, Mod: nil, Vars: vars}
finalcmd, err := vcmd.Render(dmn.Command)
if err != nil {
return nil, err
}
dmn.Command = finalcmd
d[i] = &daemon{
conf: dmn,
log: log.Stream(niceHeader("daemon: ", dmn.Command)),
}
}
return &DaemonPen{daemons: d}, nil
}
// Start starts set of daemons, each specified by a command
func (dp *DaemonPen) Start() {
dp.Lock()
defer dp.Unlock()
for i := range dp.daemons {
go dp.daemons[i].Run()
}
}
// Restart all daemons in the pen
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
}
// Start all daemon pens
func (dw *DaemonWorld) Start() {
for _, dp := range dw.DaemonPens {
dp.Start()
}
}
// Shutdown all daemons with signal s
func (dw *DaemonWorld) Shutdown(s os.Signal) {
for _, dp := range dw.DaemonPens {
dp.Shutdown(s)
}
}