-
Notifications
You must be signed in to change notification settings - Fork 13
/
entry.go
53 lines (44 loc) · 1.45 KB
/
entry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package rtwatcher
import (
"errors"
"github.com/ActiveState/cli/internal/analytics/dimensions"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/logging"
"github.com/shirou/gopsutil/v3/process"
)
type entry struct {
PID int `json:"pid"`
Exec string `json:"exec"`
Source string `json:"source"`
Dims *dimensions.Values `json:"dims"`
}
// processError wraps an OS-level error, not a State Tool error.
type processError struct {
*errs.WrapperError
}
func (e entry) IsRunning() (bool, error) {
logging.Debug("Checking if %s (%d) is still running", e.Exec, e.PID)
proc, err := process.NewProcess(int32(e.PID))
if err != nil {
if errors.Is(err, process.ErrorProcessNotRunning) {
logging.Debug("Process %d is no longer running, recieved error: %s", e.PID, err.Error())
return false, nil
}
return false, errs.Wrap(err, "Could not find process: %d", e.PID)
}
exe, err := proc.Exe()
if err != nil {
return false, &processError{errs.Wrap(err, "Could not get executable of process: %d", e.PID)}
}
match, err := fileutils.PathsMatch(exe, e.Exec)
if err != nil {
return false, errs.Wrap(err, "Could not compare paths: %s, %s", exe, e.Exec)
}
if match {
logging.Debug("Process %d matched", e.PID)
return true, nil
}
logging.Debug("Process %d not matched, expected %s to match %s", e.PID, exe, e.Exec)
return false, nil
}