From 2005a8c8e52ae3fa232d5003f5038daa714bd89f Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Thu, 19 Oct 2017 16:31:39 -0500 Subject: [PATCH] Remove the pidfile after the server has exited --- CHANGELOG.md | 1 + cmd/influxd/run/command.go | 14 ++++++++-- cmd/influxd/run/command_test.go | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 cmd/influxd/run/command_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index e1cbba86236..7d3259b0a58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/cmd/influxd/run/command.go b/cmd/influxd/run/command.go index d8e99e7d63c..0caeb61b6f5 100644 --- a/cmd/influxd/run/command.go +++ b/cmd/influxd/run/command.go @@ -36,6 +36,7 @@ type Command struct { BuildTime string closing chan struct{} + pidfile string Closed chan struct{} Stdin io.Reader @@ -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()) @@ -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() @@ -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 @@ -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) } diff --git a/cmd/influxd/run/command_test.go b/cmd/influxd/run/command_test.go new file mode 100644 index 00000000000..cd1c83dae7d --- /dev/null +++ b/cmd/influxd/run/command_test.go @@ -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") + } +}