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

Remove the pidfile after the server has exited #8987

Merged
merged 1 commit into from
Oct 20, 2017
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
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")
}
}