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

Clean shutdown of influxd server #3334

Merged
merged 1 commit into from
Jul 16, 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
34 changes: 32 additions & 2 deletions cmd/influxd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"flag"
"fmt"
"io"
"log"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/influxdb/influxdb/cmd/influxd/backup"
Expand Down Expand Up @@ -40,6 +43,8 @@ func main() {

// Main represents the program execution.
type Main struct {
Logger *log.Logger

Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Expand All @@ -48,6 +53,7 @@ type Main struct {
// NewMain return a new instance of Main.
func NewMain() *Main {
return &Main{
Logger: log.New(os.Stderr, "[run] ", log.LstdFlags),
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Expand All @@ -71,8 +77,32 @@ func (m *Main) Run(args ...string) error {
return fmt.Errorf("run: %s", err)
}

// Wait indefinitely.
<-(chan struct{})(nil)
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
m.Logger.Println("listening for signals")

// Block until one of the signals above is received
select {
case <-signalCh:
m.Logger.Println("signal received, initializing clean shutdown...")
go func() {
cmd.Close()
}()
}

// Block again until another signal is received, a shutdown timeout elapses,
// or the Command is gracefully closed
m.Logger.Println("waiting for clean shutdown...")
select {
case <-signalCh:
m.Logger.Println("second signal received, initializing hard shutdown")
case <-time.After(time.Second * 30):
m.Logger.Println("time limit reached, initializing hard shutdown")
case <-cmd.Closed:
m.Logger.Println("server shutdown completed")
}

// goodbye.

case "backup":
name := backup.NewCommand()
Expand Down
4 changes: 4 additions & 0 deletions cmd/influxd/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Command struct {
Commit string

closing chan struct{}
Closed chan struct{}

Stdin io.Reader
Stdout io.Writer
Expand All @@ -44,6 +45,7 @@ type Command struct {
func NewCommand() *Command {
return &Command{
closing: make(chan struct{}),
Closed: make(chan struct{}),
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Expand Down Expand Up @@ -109,6 +111,8 @@ func (cmd *Command) Run(args ...string) error {

// Close shuts down the server.
func (cmd *Command) Close() error {
defer close(cmd.Closed)
close(cmd.closing)
if cmd.Server != nil {
return cmd.Server.Close()
}
Expand Down
9 changes: 1 addition & 8 deletions cmd/influxd/run/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net"
"net/http"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"time"
Expand Down Expand Up @@ -349,6 +348,7 @@ func (s *Server) Close() error {
for _, service := range s.Services {
service.Close()
}

close(s.closing)
return nil
}
Expand Down Expand Up @@ -456,13 +456,6 @@ func startProfile(cpuprofile, memprofile string) {
runtime.MemProfileRate = 4096
}

go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
stopProfile()
os.Exit(0)
}()
}

// StopProfile closes the cpu and memory profiles if they are running.
Expand Down