-
Notifications
You must be signed in to change notification settings - Fork 37
Add a ProcessManager interface for managing processes by pid file #293
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package process | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/shirou/gopsutil/v4/process" | ||
) | ||
|
||
type Manager interface { | ||
ReadPidFile() (int, error) | ||
Name() string | ||
PidFilePath() string | ||
Exists() bool | ||
Terminate() error | ||
Kill() error | ||
FindProcess() (*os.Process, error) | ||
WritePidFile(pid int) error | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this? Also this seem to be the Process interface. Calling it Manager is confusing. |
||
|
||
type Process struct { | ||
name string | ||
pidFilePath string | ||
executablePath string | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This nice, but do we really need private fields and accessors? We can simplify by making all fields public and remove the accessors. |
||
|
||
func New(name, pidFilePath, executablePath string) (*Process, error) { | ||
return &Process{name: name, pidFilePath: pidFilePath, executablePath: executablePath}, nil | ||
} | ||
|
||
func (p *Process) Name() string { | ||
return p.name | ||
} | ||
|
||
func (p *Process) PidFilePath() string { | ||
return p.pidFilePath | ||
} | ||
|
||
func (p *Process) ExecutablePath() string { | ||
return p.executablePath | ||
} | ||
|
||
func (p *Process) ReadPidFile() (int, error) { | ||
data, err := os.ReadFile(p.PidFilePath()) | ||
if err != nil { | ||
return -1, err | ||
} | ||
pidStr := strings.TrimSpace(string(data)) | ||
pid, err := strconv.Atoi(pidStr) | ||
if err != nil { | ||
return -1, fmt.Errorf("invalid pid file: %v", err) | ||
} | ||
return pid, nil | ||
} | ||
|
||
func (p *Process) FindProcess() (*process.Process, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it need to be public or it an be a private helper? Exposing the underlying process package in this packages means we can never change the implementation, for example using another package to validate the process executable path. |
||
pid, err := p.ReadPidFile() | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot find process: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This underling error may be:
The caller can use errors.Is(err, os.ErrNotExists) to detect the error. |
||
} | ||
|
||
exists, err := process.PidExists(int32(pid)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !exists { | ||
return nil, fmt.Errorf("process not found") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be hard to tell if the process does not exist when we return this error. We need to return which is easy to check like os.ErrNotExist for this special case. |
||
|
||
proc, err := process.NewProcess(int32(pid)) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot find process: %v", err) | ||
} | ||
if proc == nil { | ||
return nil, fmt.Errorf("process not found") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why check if the process exists with process.PidExists(int32(pid)) if we get the same information by creating a process with process.NewProcess(int32(pid))? Check how PidExists is implemented, I guess it create a process internally. |
||
name, err := proc.Name() | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot find process name: %v", err) | ||
} | ||
if name != p.Name() { | ||
return nil, fmt.Errorf("pid %d is stale, and is being used by %s", pid, name) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pid is stale. How the user can tell that it can delete the pidfile? We need an error that the user can check, meaning that there is no such process matching name, pid and executable path. |
||
exe, err := proc.Exe() | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot find process exe: %v", err) | ||
} | ||
if exe != p.ExecutablePath() { | ||
return nil, fmt.Errorf("pid %d is stale, and is being used by %s", pid, exe) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue here. |
||
} | ||
return proc, nil | ||
} | ||
|
||
func (p *Process) Exists() bool { | ||
vyasgun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
proc, err := p.FindProcess() | ||
return err == nil && proc != nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may be wrong - maybe the process exists but we don't have permissions to get the details? In minikube we return (bool, error) so it is easy to tell if the process exists or we cannot tell. |
||
} | ||
|
||
func (p *Process) Terminate() error { | ||
proc, err := p.FindProcess() | ||
if err != nil { | ||
return fmt.Errorf("cannot find process: %v", err) | ||
} | ||
return proc.Terminate() | ||
} | ||
|
||
func (p *Process) Kill() error { | ||
vyasgun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
proc, err := p.FindProcess() | ||
if err != nil { | ||
return fmt.Errorf("cannot find process: %v", err) | ||
} | ||
return proc.Kill() | ||
} | ||
|
||
func (p *Process) WritePidFile(pid int) error { | ||
return os.WriteFile(p.pidFilePath, []byte(strconv.Itoa(pid)), 0600) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package process | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we need only public names, so we can use process_test package. This will be better tests since the test code will be the same as real code using this package. |
||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
dummyProcessName = "sleep" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which platforms are supported? I don't see any build tags in this file. |
||
dummyProcessArgs = "60" | ||
) | ||
|
||
var ( | ||
dummyProcess *exec.Cmd | ||
managedProcess *Process | ||
pidFilePath = filepath.Join(os.TempDir(), "pid") | ||
) | ||
|
||
func startDummyProcess() error { | ||
dummyProcess = exec.Command(dummyProcessName, dummyProcessArgs) | ||
err := dummyProcess.Start() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need TestMain? Starting a tiny process is very quick, we don't need to start a dummy process before all tests. Check minikube process package for similar tests: If you want to use the same process for multiple tests you can use sub tests. The parent tests can start the process, run the sub tests, and clean up. |
||
err := startDummyProcess() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, "Failed to start process:", err) | ||
os.Exit(1) | ||
} | ||
|
||
managedProcess, err = New(dummyProcessName, pidFilePath, dummyProcess.Path) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, "Failed to create process:", err) | ||
os.Exit(1) | ||
} | ||
err = managedProcess.WritePidFile(dummyProcess.Process.Pid) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
exitCode := m.Run() | ||
if dummyProcess.Process != nil { | ||
_ = dummyProcess.Process.Kill() | ||
} | ||
|
||
os.Exit(exitCode) | ||
} | ||
|
||
func TestProcess_Name(t *testing.T) { | ||
assert.Equal(t, dummyProcessName, managedProcess.Name()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test the Name() accessor which does not really need any testing. |
||
} | ||
|
||
func TestProcess_FindProcess(t *testing.T) { | ||
vyasgun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
foundProcess, err := managedProcess.FindProcess() | ||
assert.NoError(t, err) | ||
assert.NotNil(t, foundProcess) | ||
assert.Equal(t, dummyProcess.Process.Pid, int(foundProcess.Pid)) | ||
|
||
assert.True(t, managedProcess.Exists()) | ||
} | ||
|
||
func TestProcess_KillProcess(t *testing.T) { | ||
vyasgun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
err := managedProcess.Kill() | ||
assert.NoError(t, err) | ||
assert.False(t, managedProcess.Exists()) | ||
|
||
// Try to kill the non-existent process | ||
// This should result in an error | ||
err = managedProcess.Kill() | ||
assert.Error(t, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kill will be easier to use if it is idempotent. Trying to kill a process that does not exist is not an error from the caller point of view. |
||
} | ||
|
||
func TestProcess_FindProcess_InvalidPidFile(t *testing.T) { | ||
vyasgun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tmpfile, err := os.CreateTemp("", "invalid_pid") | ||
require.NoError(t, err) | ||
defer os.Remove(tmpfile.Name()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use t.TempDir() |
||
|
||
// Write non-numeric content into the file to mimic an invalid pid | ||
_, err = tmpfile.WriteString("non-numeric") | ||
require.NoError(t, err) | ||
tmpfile.Close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why open a temporary file and close it manually instead os.WriteFile()? If the issue is using a temporary file with random name, the solution is t.TempDir(). |
||
|
||
invalidProcess, err := New("invalid-process", tmpfile.Name(), "invalid-path") | ||
assert.NoError(t, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of checking for error that we never return, remove the error. New cannot fail. |
||
|
||
foundProcess, err := invalidProcess.FindProcess() | ||
assert.Error(t, err) | ||
assert.Nil(t, foundProcess) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No copyright/license info?