Skip to content

Commit

Permalink
Merge pull request #8 from upfluence/jl-exit-codes
Browse files Browse the repository at this point in the history
Catch SIGTERM and SIGINT
  • Loading branch information
jlevesy committed Dec 7, 2015
2 parents 5c7b569 + 6bd351b commit 205b24c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
9 changes: 6 additions & 3 deletions etcdenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"os/signal"
"strings"
"syscall"
)

const currentVersion = "0.3.1"
Expand Down Expand Up @@ -78,12 +79,12 @@ func main() {
}

if flags.Version {
fmt.Printf("etcdenv v%s", currentVersion)
log.Printf("etcdenv v%s", currentVersion)
os.Exit(0)
}

signalChan := make(chan os.Signal)
signal.Notify(signalChan, os.Interrupt, os.Kill)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

if flags.WatchedKeys == "" {
watchedKeysList = []string{}
Expand All @@ -107,8 +108,10 @@ func main() {
go ctx.Run()

select {
case <-signalChan:
case sig := <-signalChan:
log.Printf("Received signal %s", sig)
ctx.ExitChan <- true
case <-ctx.ExitChan:
log.Printf("Catching ExitChan, doing nothin'")
}
}
11 changes: 6 additions & 5 deletions etcdenv/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package etcdenv

import (
"errors"
"fmt"
"github.com/cenkalti/backoff"
"github.com/coreos/go-etcd/etcd"
"log"
Expand Down Expand Up @@ -176,22 +175,24 @@ func (ctx *Context) Run() {
for {
select {
case <-responseChan:
log.Println("Process restarted")
log.Println("Environment changed, restarting child process..")
ctx.CurrentEnv = ctx.fetchEtcdVariables()
ctx.Runner.Restart(ctx.CurrentEnv)
log.Println("Process restarted")
case <-ctx.ExitChan:
log.Println("Asking the runner to stop")
ctx.Runner.Stop()
log.Println("Runner stopped")
case status := <-processExitChan:
log.Println(fmt.Sprintf("Process exited with the status %d", status))

log.Printf("Child process exited with status %d\n", status)
if ctx.ShutdownBehaviour == "exit" {
ctx.ExitChan <- true
os.Exit(status)
} else if ctx.ShutdownBehaviour == "restart" {
log.Println("Process restarted")
ctx.CurrentEnv = ctx.fetchEtcdVariables()
ctx.Runner.Restart(ctx.CurrentEnv)
go ctx.Runner.WatchProcess(processExitChan)
log.Println("Process restarted")
}
}
}
Expand Down

0 comments on commit 205b24c

Please sign in to comment.