Skip to content

Commit

Permalink
Remove the pidfile after the server has exited
Browse files Browse the repository at this point in the history
  • Loading branch information
jsternberg committed Oct 19, 2017
1 parent f58a194 commit 2005a8c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
- [#7797](https://github.com/influxdata/influxdb/issues/7706): Fix data deleted outside of time range
- [#8822](https://github.com/influxdata/influxdb/issues/8822): Fix data dropped incorrectly during compaction
- [#8780](https://github.com/influxdata/influxdb/issues/8780): Prevent deadlock during collectd, graphite, opentsdb, and udp shutdown.
- [#8983](https://github.com/influxdata/influxdb/issues/8983): Remove the pidfile after the server has exited.

## v1.3.4 [unreleased]

Expand Down
14 changes: 12 additions & 2 deletions cmd/influxd/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Command struct {
BuildTime string

closing chan struct{}
pidfile string
Closed chan struct{}

Stdin io.Reader
Expand Down Expand Up @@ -81,6 +82,7 @@ func (cmd *Command) Run(args ...string) error {
if err := cmd.writePIDFile(options.PIDFile); err != nil {
return fmt.Errorf("write pid file: %s", err)
}
cmd.pidfile = options.PIDFile

// Parse config
config, err := cmd.ParseConfig(options.GetConfigPath())
Expand Down Expand Up @@ -132,6 +134,7 @@ func (cmd *Command) Run(args ...string) error {
// Close shuts down the server.
func (cmd *Command) Close() error {
defer close(cmd.Closed)
defer cmd.removePIDFile()
close(cmd.closing)
if cmd.Server != nil {
return cmd.Server.Close()
Expand All @@ -151,6 +154,14 @@ func (cmd *Command) monitorServerErrors() {
}
}

func (cmd *Command) removePIDFile() {
if cmd.pidfile != "" {
if err := os.Remove(cmd.pidfile); err != nil {
cmd.Logger.Error("unable to remove pidfile", zap.Error(err))
}
}
}

// ParseFlags parses the command line flags from args and returns an options set.
func (cmd *Command) ParseFlags(args ...string) (Options, error) {
var options Options
Expand All @@ -176,8 +187,7 @@ func (cmd *Command) writePIDFile(path string) error {
}

// Ensure the required directory structure exists.
err := os.MkdirAll(filepath.Dir(path), 0777)
if err != nil {
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
return fmt.Errorf("mkdir: %s", err)
}

Expand Down
45 changes: 45 additions & 0 deletions cmd/influxd/run/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package run_test

import (
"io/ioutil"
"os"
"testing"

"path/filepath"

"time"

"github.com/influxdata/influxdb/cmd/influxd/run"
)

func TestPIDFile(t *testing.T) {
tmpdir, err := ioutil.TempDir(os.TempDir(), "influxd-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)

pidFile := filepath.Join(tmpdir, "influxdb.pid")

cmd := run.NewCommand()
if err := cmd.Run("-pidfile", pidFile); err != nil {
t.Fatalf("unexpected error: %s", err)
}

if _, err := os.Stat(pidFile); err != nil {
t.Fatalf("could not stat pid file: %s", err)
}
go cmd.Close()

timeout := time.NewTimer(100 * time.Millisecond)
select {
case <-timeout.C:
t.Fatal("unexpected timeout")
case <-cmd.Closed:
timeout.Stop()
}

if _, err := os.Stat(pidFile); err == nil {
t.Fatal("expected pid file to be removed")
}
}

0 comments on commit 2005a8c

Please sign in to comment.