This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
/
executor.go
197 lines (169 loc) · 3.64 KB
/
executor.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
193
194
195
196
197
//go:generate stringer -type=DynoState,DynoInput
package hsup
import (
"errors"
"fmt"
"log"
"net/url"
"os/exec"
"strconv"
"github.com/fsouza/go-dockerclient"
"github.com/heroku/hsup/diag"
"github.com/opencontainers/runc/libcontainer"
)
const DefaultPort = "5000"
type DynoState int
const (
Stopped DynoState = iota
Started
Retiring
Retired
)
type DynoInput int
type IPInfo func() (ip string, port int)
const (
Retire DynoInput = iota
Restart
Exited
StayStarted
)
var ErrExecutorComplete = errors.New("Executor complete")
type Executor struct {
Args []string
DynoDriver DynoDriver
Release *Release
ProcessID int
ProcessType string
Status chan *ExitStatus
Complete chan struct{}
LogplexURL *url.URL
Binds map[string]string
// simple and abspath dyno driver properties
cmd *exec.Cmd
waiting chan struct{}
logsRelay *relay
// docker dyno driver properties
container *docker.Container
// libcontainer dyno driver properties
initExitStatus chan *ExitStatus
initProcess *libcontainer.Process
// FSM Fields
OneShot bool
State DynoState
NewInput chan DynoInput
// Status API fields
IPInfo IPInfo
}
func (ex *Executor) dlog(values ...interface{}) {
diag.Log(append(
[]interface{}{"Executor", ex.Name(), fmt.Sprintf("%p", ex)},
values...)...)
}
func (ex *Executor) Trigger(input DynoInput) {
ex.dlog("trigger", input)
select {
case ex.NewInput <- input:
case <-ex.Complete:
}
}
func (ex *Executor) wait() {
if s := ex.DynoDriver.Wait(ex); ex.Status != nil {
log.Println("Executor exits:", ex.Name(), "exit code:", s.Code)
ex.Status <- s
}
ex.Trigger(Exited)
}
func (ex *Executor) Tick() (err error) {
ex.dlog("waiting for tick... (current state:", ex.State.String()+")")
input := <-ex.NewInput
ex.dlog("ticking with input", ex.State)
start := func() error {
log.Printf("%v: starting\n", ex.Name())
if err = ex.DynoDriver.Start(ex); err != nil {
log.Printf("%v: start fails: %#+v", ex.Name(), err)
if ex.OneShot {
go ex.Trigger(Retire)
} else {
go ex.Trigger(Restart)
}
return err
}
ex.dlog("started")
ex.State = Started
go ex.wait()
return nil
}
again:
switch ex.State {
case Retired:
close(ex.Complete)
return ErrExecutorComplete
case Retiring:
switch input {
case Exited:
ex.State = Retired
goto again
case Retire:
return ex.DynoDriver.Stop(ex)
default:
return nil
}
case Stopped:
switch input {
case Retire:
ex.State = Retired
goto again
case Exited:
if ex.OneShot {
ex.State = Retired
goto again
}
return start()
case StayStarted:
fallthrough
case Restart:
return start()
default:
panic(fmt.Sprintln("Invalid input", input))
}
case Started:
switch input {
case Retire:
ex.State = Retiring
goto again
case Exited:
ex.State = Stopped
goto again
case Restart:
return ex.DynoDriver.Stop(ex)
default:
panic(fmt.Sprintln("Invalid input", input))
}
default:
panic(fmt.Sprintln("Invalid state", ex.State))
}
}
func (ex *Executor) Name() string {
return ex.ProcessType + "." + strconv.Itoa(ex.ProcessID)
}
// Convenience function to return an empty LogplexURL string when
// there *url.URL is nil.
//
// This is necessitated because of the repeated conversions between
// url.URL and string when dealing with hsup.Startup serialization
// constraints.
func (ex *Executor) logplexURLString() string {
if ex.LogplexURL == nil {
return ""
}
return ex.LogplexURL.String()
}
func (ex *Executor) bindPairs() []string {
pairs := make([]string, len(ex.Binds))
i := 0
for k, v := range ex.Binds {
pairs[i] = fmt.Sprintf("%s:%s", k, v)
i++
}
return pairs
}