-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handling for outdated PID files (#43)
* Add handling for outdated PID files * goimports * Fix goreleaser config * Bump Goreleaser & Goreleaser workflow
- Loading branch information
1 parent
c95c449
commit 5b8502c
Showing
7 changed files
with
171 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package pidfile | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"syscall" | ||
|
||
"github.com/pkg/errors" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type PIDFile struct { | ||
path string | ||
fd int | ||
} | ||
|
||
func New(path string) PIDFile { | ||
return PIDFile{ | ||
path: path, | ||
} | ||
} | ||
|
||
func (f PIDFile) Acquire() error { | ||
if f.path == "" { | ||
return nil | ||
} | ||
|
||
if err := os.MkdirAll(filepath.Dir(f.path), 0o755); err != nil { | ||
return errors.Wrapf(err, "failed to create pid file directory %q", filepath.Dir(f.path)) | ||
} | ||
|
||
fd, err := syscall.Open(f.path, syscall.O_CREAT|syscall.O_EXCL|syscall.O_WRONLY, 0o644) | ||
switch err { | ||
case syscall.EEXIST: | ||
if err := f.removePIDFileIfOutdated(); err != nil { | ||
return err | ||
} | ||
|
||
return f.Acquire() | ||
case nil: | ||
if _, err := syscall.Write(fd, pidToByteString()); err != nil { | ||
return errors.Wrapf(err, "failed to write pid to pid file %q", f.path) | ||
} | ||
|
||
log.Info("acquired pid file ", f.path) | ||
default: | ||
return errors.Wrapf(err, "failed to open pid file %q", f.path) | ||
} | ||
|
||
f.fd = fd | ||
return nil | ||
} | ||
|
||
func (f PIDFile) removePIDFileIfOutdated() error { | ||
pidStr, err := os.ReadFile(f.path) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to read pid file '%s'", f.path) | ||
} | ||
|
||
pid, err := strconv.Atoi(string(pidStr)) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to parse pid file '%s'", f.path) | ||
} | ||
|
||
process, err := os.FindProcess(pid) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to find process with pid %d", pid) | ||
} | ||
|
||
if err := process.Signal(syscall.Signal(0)); err == nil { | ||
return fmt.Errorf("pid file %q already exists and contains the PID of a running process", f.path) | ||
} | ||
|
||
log.Info("existing pid file contains the PID of a non-running process; removing it") | ||
|
||
if err := os.Remove(f.path); err != nil { | ||
return errors.Wrapf(err, "failed to remove pid file %q", f.path) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (f PIDFile) Release() error { | ||
if f.path == "" { | ||
return nil | ||
} | ||
|
||
if err := syscall.Close(f.fd); err != nil { | ||
return errors.Wrapf(err, "failed to close pid file %q", f.path) | ||
} | ||
|
||
if err := os.Remove(f.path); err != nil { | ||
return errors.Wrapf(err, "failed to remove pid file %q", f.path) | ||
} | ||
|
||
log.Info("released pid file ", f.path) | ||
return nil | ||
} | ||
|
||
func pidToByteString() []byte { | ||
return []byte(fmt.Sprintf("%d", os.Getpid())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package pidfile_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/mittwald/mittnite/pkg/pidfile" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPidFileCanBeAcquiredAndReleased(t *testing.T) { | ||
f := pidfile.New("./test1.pid") | ||
|
||
require.NoError(t, f.Acquire()) | ||
require.NoError(t, f.Release()) | ||
|
||
_, err := os.Stat("./test1.pid") | ||
require.True(t, os.IsNotExist(err)) | ||
} | ||
|
||
func TestPidFileCanBeAcquiredWhenOutdatedFileExists(t *testing.T) { | ||
f := pidfile.New("./test3.pid") | ||
|
||
require.NoError(t, os.WriteFile("./test3.pid", []byte("12345"), 0o644)) | ||
require.NoError(t, f.Acquire()) | ||
require.NoError(t, f.Release()) | ||
|
||
_, err := os.Stat("./test3.pid") | ||
require.True(t, os.IsNotExist(err)) | ||
} | ||
|
||
func TestPidFileCannotBeAcquiredWhileAlreadyHeld(t *testing.T) { | ||
f1 := pidfile.New("./test2.pid") | ||
f2 := pidfile.New("./test2.pid") | ||
|
||
closeF1 := make(chan struct{}) | ||
f1Acquired := make(chan struct{}) | ||
f1Closed := make(chan struct{}) | ||
|
||
go func() { | ||
require.NoError(t, f1.Acquire()) | ||
close(f1Acquired) | ||
<-closeF1 | ||
require.NoError(t, f1.Release()) | ||
close(f1Closed) | ||
}() | ||
|
||
<-f1Acquired | ||
|
||
require.Error(t, f2.Acquire()) | ||
close(closeF1) | ||
<-f1Closed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters