-
Notifications
You must be signed in to change notification settings - Fork 176
/
start.go
332 lines (271 loc) · 8.19 KB
/
start.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"errors"
"fmt"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
)
const defaultPort = 5000
const defaultShutdownGraceTime = 3
var flagPort int
var flagConcurrency string
var flagRestart bool
var flagShutdownGraceTime int
var envs envFiles
var cmdStart = &Command{
Run: runStart,
Usage: "start [process name] [-f procfile] [-e env] [-p port] [-c concurrency] [-r] [-t shutdown_grace_time]",
Short: "Start the application",
Long: `
Start the application specified by a Procfile. The directory containing the
Procfile is used as the working directory.
The following options are available:
-f procfile Set the Procfile. Defaults to './Procfile'.
-e env Add an environment file, containing variables in 'KEY=value', or
'export KEY=value', form. These variables will be set in the
environment of each process. If no environment files are
specified, a file called .env is used if it exists.
-p port Sets the base port number; each process will have a PORT variable
in its environment set to a unique value based on this. This may
also be set via a PORT variable in the environment, or in an
environment file, and otherwise defaults to 5000.
-c concurrency
Start a specific number of instances of each process. The
argument should be in the format 'foo=1,bar=2,baz=0'. Use the
name 'all' to set the default number of instances. By default,
one instance of each process is started.
-r Restart a process which exits. Without this, if a process exits,
forego will kill all other processes and exit.
-t shutdown_grace_time
Set the shutdown grace time that each process is given after
being asked to stop. Once this grace time expires, the process is
forcibly terminated. By default, it is 3 seconds.
If there is a file named .forego in the current directory, it will be read in
the same way as an environment file, and the values of variables procfile, port,
concurrency, and shutdown_grace_time used to change the corresponding default
values.
Examples:
# start every process
forego start
# start only the web process
forego start web
# start every process specified in Procfile.test, with the environment specified in .env.test
forego start -f Procfile.test -e .env.test
# start every process, with a timeout of 30 seconds
forego start -t 30
`,
}
func init() {
cmdStart.Flag.StringVar(&flagProcfile, "f", "Procfile", "procfile")
cmdStart.Flag.Var(&envs, "e", "env")
cmdStart.Flag.IntVar(&flagPort, "p", defaultPort, "port")
cmdStart.Flag.StringVar(&flagConcurrency, "c", "", "concurrency")
cmdStart.Flag.BoolVar(&flagRestart, "r", false, "restart")
cmdStart.Flag.IntVar(&flagShutdownGraceTime, "t", defaultShutdownGraceTime, "shutdown grace time")
err := readConfigFile(".forego", &flagProcfile, &flagPort, &flagConcurrency, &flagShutdownGraceTime)
handleError(err)
}
func readConfigFile(config_path string, flagProcfile *string, flagPort *int, flagConcurrency *string, flagShutdownGraceTime *int) error {
config, err := ReadConfig(config_path)
if config["procfile"] != "" {
*flagProcfile = config["procfile"]
} else {
*flagProcfile = "Procfile"
}
if config["port"] != "" {
*flagPort, err = strconv.Atoi(config["port"])
} else {
*flagPort = defaultPort
}
if config["shutdown_grace_time"] != "" {
*flagShutdownGraceTime, err = strconv.Atoi(config["shutdown_grace_time"])
} else {
*flagShutdownGraceTime = defaultShutdownGraceTime
}
*flagConcurrency = config["concurrency"]
return err
}
func parseConcurrency(value string) (map[string]int, error) {
concurrency := map[string]int{}
if strings.TrimSpace(value) == "" {
return concurrency, nil
}
parts := strings.Split(value, ",")
for _, part := range parts {
if !strings.Contains(part, "=") {
return concurrency, errors.New("Concurrency should be in the format: foo=1,bar=2")
}
nameValue := strings.Split(part, "=")
n, v := strings.TrimSpace(nameValue[0]), strings.TrimSpace(nameValue[1])
if n == "" || v == "" {
return concurrency, errors.New("Concurrency should be in the format: foo=1,bar=2")
}
numProcs, err := strconv.ParseInt(v, 10, 16)
if err != nil {
return concurrency, err
}
concurrency[n] = int(numProcs)
}
return concurrency, nil
}
type Forego struct {
outletFactory *OutletFactory
teardown, teardownNow Barrier // signal shutting down
wg sync.WaitGroup
}
func (f *Forego) monitorInterrupt() {
handler := make(chan os.Signal, 1)
signal.Notify(handler, syscall.SIGALRM, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
first := true
for sig := range handler {
switch sig {
case syscall.SIGINT:
fmt.Println(" | ctrl-c detected")
fallthrough
default:
f.teardown.Fall()
if !first {
f.teardownNow.Fall()
}
first = false
}
}
}
func basePort(env Env) (int, error) {
if flagPort != defaultPort {
return flagPort, nil
} else if env["PORT"] != "" {
return strconv.Atoi(env["PORT"])
} else if os.Getenv("PORT") != "" {
return strconv.Atoi(os.Getenv("PORT"))
}
return defaultPort, nil
}
func (f *Forego) startProcess(idx, procNum int, proc ProcfileEntry, env Env, of *OutletFactory) {
port, err := basePort(env)
if err != nil {
panic(err)
}
port = port + (idx * 100)
const interactive = false
workDir := filepath.Dir(flagProcfile)
ps := NewProcess(workDir, proc.Command, env, interactive)
procName := fmt.Sprint(proc.Name, ".", procNum+1)
ps.Env["PORT"] = strconv.Itoa(port)
ps.Stdin = nil
stdout, err := ps.StdoutPipe()
if err != nil {
panic(err)
}
stderr, err := ps.StderrPipe()
if err != nil {
panic(err)
}
pipeWait := new(sync.WaitGroup)
pipeWait.Add(2)
go of.LineReader(pipeWait, procName, idx, stdout, false)
go of.LineReader(pipeWait, procName, idx, stderr, true)
of.SystemOutput(fmt.Sprintf("starting %s on port %d", procName, port))
finished := make(chan struct{}) // closed on process exit
err = ps.Start()
if err != nil {
f.teardown.Fall()
of.SystemOutput(fmt.Sprint("Failed to start ", procName, ": ", err))
return
}
f.wg.Add(1)
go func() {
defer f.wg.Done()
defer close(finished)
pipeWait.Wait()
ps.Wait()
}()
f.wg.Add(1)
go func() {
defer f.wg.Done()
select {
case <-finished:
if flagRestart {
f.startProcess(idx, procNum, proc, env, of)
} else {
f.teardown.Fall()
}
case <-f.teardown.Barrier():
// Forego tearing down
if !osHaveSigTerm {
of.SystemOutput(fmt.Sprintf("Killing %s", procName))
ps.Process.Kill()
return
}
of.SystemOutput(fmt.Sprintf("sending SIGTERM to %s", procName))
ps.SendSigTerm()
// Give the process a chance to exit, otherwise kill it.
select {
case <-f.teardownNow.Barrier():
of.SystemOutput(fmt.Sprintf("Killing %s", procName))
ps.SendSigKill()
case <-finished:
}
}
}()
}
func runStart(cmd *Command, args []string) {
pf, err := ReadProcfile(flagProcfile)
handleError(err)
concurrency, err := parseConcurrency(flagConcurrency)
handleError(err)
env, err := loadEnvs(envs)
handleError(err)
of := NewOutletFactory()
of.Padding = pf.LongestProcessName(concurrency)
f := &Forego{
outletFactory: of,
}
go f.monitorInterrupt()
// When teardown fires, start the grace timer
f.teardown.FallHook = func() {
go func() {
time.Sleep(time.Duration(flagShutdownGraceTime) * time.Second)
of.SystemOutput("Grace time expired")
f.teardownNow.Fall()
}()
}
var singleton string = ""
if len(args) > 0 {
singleton = args[0]
if !pf.HasProcess(singleton) {
of.ErrorOutput(fmt.Sprintf("no such process: %s", singleton))
}
}
defaultConcurrency := 1
var all bool
for name, num := range concurrency {
if name == "all" {
defaultConcurrency = num
all = true
}
}
for idx, proc := range pf.Entries {
numProcs := defaultConcurrency
if len(concurrency) > 0 {
if value, ok := concurrency[proc.Name]; ok {
numProcs = value
} else if !all {
continue
}
}
for i := 0; i < numProcs; i++ {
if (singleton == "") || (singleton == proc.Name) {
f.startProcess(idx, i, proc, env, of)
}
}
}
<-f.teardown.Barrier()
f.wg.Wait()
}