|
| 1 | +package process |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/shirou/gopsutil/v4/process" |
| 10 | +) |
| 11 | + |
| 12 | +type Manager interface { |
| 13 | + ReadPidFile() (int, error) |
| 14 | + Name() string |
| 15 | + PidFilePath() string |
| 16 | + Exists() bool |
| 17 | + Terminate() error |
| 18 | + Kill() error |
| 19 | + FindProcess() (*os.Process, error) |
| 20 | + WritePidFile(pid int) error |
| 21 | +} |
| 22 | + |
| 23 | +type Process struct { |
| 24 | + name string |
| 25 | + pidFilePath string |
| 26 | + executablePath string |
| 27 | +} |
| 28 | + |
| 29 | +func New(name, pidFilePath, executablePath string) (*Process, error) { |
| 30 | + return &Process{name: name, pidFilePath: pidFilePath, executablePath: executablePath}, nil |
| 31 | +} |
| 32 | + |
| 33 | +func (p *Process) Name() string { |
| 34 | + return p.name |
| 35 | +} |
| 36 | + |
| 37 | +func (p *Process) PidFilePath() string { |
| 38 | + return p.pidFilePath |
| 39 | +} |
| 40 | + |
| 41 | +func (p *Process) ExecutablePath() string { |
| 42 | + return p.executablePath |
| 43 | +} |
| 44 | + |
| 45 | +func (p *Process) ReadPidFile() (int, error) { |
| 46 | + data, err := os.ReadFile(p.PidFilePath()) |
| 47 | + if err != nil { |
| 48 | + return -1, err |
| 49 | + } |
| 50 | + pidStr := strings.TrimSpace(string(data)) |
| 51 | + pid, err := strconv.Atoi(pidStr) |
| 52 | + if err != nil { |
| 53 | + return -1, fmt.Errorf("invalid pid file: %v", err) |
| 54 | + } |
| 55 | + return pid, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (p *Process) FindProcess() (*process.Process, error) { |
| 59 | + pid, err := p.ReadPidFile() |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("cannot find process: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + exists, err := process.PidExists(int32(pid)) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + if !exists { |
| 69 | + return nil, fmt.Errorf("process not found") |
| 70 | + } |
| 71 | + |
| 72 | + proc, err := process.NewProcess(int32(pid)) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("cannot find process: %v", err) |
| 75 | + } |
| 76 | + if proc == nil { |
| 77 | + return nil, fmt.Errorf("process not found") |
| 78 | + } |
| 79 | + name, err := proc.Name() |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("cannot find process name: %v", err) |
| 82 | + } |
| 83 | + if name != p.Name() { |
| 84 | + return nil, fmt.Errorf("pid %d is stale, and is being used by %s", pid, name) |
| 85 | + } |
| 86 | + exe, err := proc.Exe() |
| 87 | + if err != nil { |
| 88 | + return nil, fmt.Errorf("cannot find process exe: %v", err) |
| 89 | + } |
| 90 | + if exe != p.ExecutablePath() { |
| 91 | + return nil, fmt.Errorf("pid %d is stale, and is being used by %s", pid, exe) |
| 92 | + } |
| 93 | + return proc, nil |
| 94 | +} |
| 95 | + |
| 96 | +func (p *Process) Exists() bool { |
| 97 | + proc, err := p.FindProcess() |
| 98 | + return err == nil && proc != nil |
| 99 | +} |
| 100 | + |
| 101 | +func (p *Process) Terminate() error { |
| 102 | + proc, err := p.FindProcess() |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("cannot find process: %v", err) |
| 105 | + } |
| 106 | + return proc.Terminate() |
| 107 | +} |
| 108 | + |
| 109 | +func (p *Process) Kill() error { |
| 110 | + proc, err := p.FindProcess() |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("cannot find process: %v", err) |
| 113 | + } |
| 114 | + return proc.Kill() |
| 115 | +} |
| 116 | + |
| 117 | +func (p *Process) WritePidFile(pid int) error { |
| 118 | + return os.WriteFile(p.pidFilePath, []byte(strconv.Itoa(pid)), 0600) |
| 119 | +} |
0 commit comments