Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch SIGTERM and SIGINT #8

Merged
merged 5 commits into from
Dec 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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