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 26, 2017
1 parent 1e419cf commit 6caca36
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- [#1581](https://github.com/influxdata/kapacitor/pull/1581): Add SNMP sysUpTime to SNMP Trap service
- [#1547](https://github.com/influxdata/kapacitor/issues/1547): Fix panic on recording replay with HTTPPostHandler.
- [#1623](https://github.com/influxdata/kapacitor/issues/1623): Fix k8s incluster master api dns resolution
- [#1630](https://github.com/influxdata/kapacitor/issues/1630): Remove the pidfile after the server has exited.

## v1.3.3 [2017-08-11]

Expand Down
16 changes: 13 additions & 3 deletions cmd/kapacitord/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Command struct {
Commit string

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

Stdin io.Reader
Expand Down Expand Up @@ -104,7 +105,7 @@ func (cmd *Command) Run(args ...string) error {
// Initialize Logging Services
cmd.diagService = diagnostic.NewService(config.Logging, cmd.Stdout, cmd.Stderr)
if err := cmd.diagService.Open(); err != nil {
return fmt.Errorf("failed to opend diagnostic service: %v", err)
return fmt.Errorf("failed to open diagnostic service: %v", err)
}

// Initialize cmd diagnostic
Expand All @@ -118,6 +119,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

// Create server from config and start it.
buildInfo := server.BuildInfo{Version: cmd.Version, Commit: cmd.Commit, Branch: cmd.Branch}
Expand All @@ -142,6 +144,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 @@ -165,6 +168,14 @@ func (cmd *Command) monitorServerErrors() {
}
}

func (cmd *Command) removePIDFile() {
if cmd.pidfile != "" {
if err := os.Remove(cmd.pidfile); err != nil {
cmd.Diag.Error("unable to remove pidfile", 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 @@ -191,8 +202,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
70 changes: 70 additions & 0 deletions cmd/kapacitord/run/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package run_test

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
"text/template"
"time"

"fmt"

"github.com/influxdata/kapacitor/cmd/kapacitord/run"
)

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

// Write out a config file that does not attempt to connect to influxdb.
configFile := filepath.Join(tmpdir, "kapacitor.conf")
configTemplate := template.Must(template.New("config_file").Parse(`data_dir = "{{.TempDir}}/data"
[[influxdb]]
enabled = false
[replay]
dir = "{{.TempDir}}/replay"
[storage]
boltdb = "{{.TempDir}}/kapacitor.db"
[task]
dir = "{{.TempDir}}/tasks"
[load]
dir = "{{.TempDir}}/load"
`))
var buf bytes.Buffer
if err := configTemplate.Execute(&buf, map[string]string{"TempDir": tmpdir}); err != nil {
t.Fatalf("unable to generate config file: %s", err)
}
fmt.Println(buf.String())
if err := ioutil.WriteFile(configFile, buf.Bytes(), 0600); err != nil {
t.Fatalf("unable to write %s: %s", configFile, err)
}

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

cmd := run.NewCommand()
if err := cmd.Run("-config", configFile, "-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(5 * time.Second)
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 6caca36

Please sign in to comment.