Skip to content

Commit

Permalink
Add flag to customize shutdown gracetime (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Perez authored and ddollar committed Jun 6, 2016
1 parent 733e197 commit 9bebfec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions fixtures/configs/.forego
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
procfile: Procfile.dev
concurrency: foo=2,bar=3
port: 15000
shutdown_grace_time: 30
18 changes: 13 additions & 5 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import (
"time"
)

const shutdownGraceTime = 3 * time.Second
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] [-c concurrency] [-p port] [-r]",
Usage: "start [process name] [-f procfile] [-e env] [-c concurrency] [-p port] [-t timeout] [-r]",
Short: "Start the application",
Long: `
Start the application specified by a Procfile (defaults to ./Procfile)
Expand All @@ -33,6 +34,7 @@ Examples:
forego start
forego start web
forego start -f Procfile.test -e .env.test
forego start -t 30
`,
}

Expand All @@ -42,11 +44,12 @@ func init() {
cmdStart.Flag.IntVar(&flagPort, "p", defaultPort, "port")
cmdStart.Flag.StringVar(&flagConcurrency, "c", "", "concurrency")
cmdStart.Flag.BoolVar(&flagRestart, "r", false, "restart")
err := readConfigFile(".forego", &flagProcfile, &flagPort, &flagConcurrency)
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) error {
func readConfigFile(config_path string, flagProcfile *string, flagPort *int, flagConcurrency *string, flagShutdownGraceTime *int) error {
config, err := ReadConfig(config_path)

if config["procfile"] != "" {
Expand All @@ -59,6 +62,11 @@ func readConfigFile(config_path string, flagProcfile *string, flagPort *int, fla
} else {
*flagPort = defaultPort
}
if config["shutdown_grace_time"] != "" {
*flagShutdownGraceTime, err = strconv.Atoi(config["shutdown_grace_time"])
} else {
*flagShutdownGraceTime = defaultShutdownGraceTime
}
*flagConcurrency = config["concurrency"]
return err
}
Expand Down Expand Up @@ -237,7 +245,7 @@ func runStart(cmd *Command, args []string) {
// When teardown fires, start the grace timer
f.teardown.FallHook = func() {
go func() {
time.Sleep(shutdownGraceTime)
time.Sleep(time.Duration(flagShutdownGraceTime) * time.Second)
of.SystemOutput("Grace time expired")
f.teardownNow.Fall()
}()
Expand Down
7 changes: 6 additions & 1 deletion start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func TestConfigBeOverrideByForegoFile(t *testing.T) {
var procfile = "Profile"
var port = 5000
var concurrency string = "web=2"
err := readConfigFile("./fixtures/configs/.forego", &procfile, &port, &concurrency)
var gracetime int = 3
err := readConfigFile("./fixtures/configs/.forego", &procfile, &port, &concurrency, &gracetime)

if err != nil {
t.Fatalf("Cannot set default values from forego config file")
Expand All @@ -167,4 +168,8 @@ func TestConfigBeOverrideByForegoFile(t *testing.T) {
if concurrency != "foo=2,bar=3" {
t.Fatal("concurrency should be 'foo=2,bar=3', got %s", concurrency)
}

if gracetime != 30 {
t.Fatal("gracetime should be 3, got %d", gracetime)
}
}

0 comments on commit 9bebfec

Please sign in to comment.