|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/pkg/errors" |
| 12 | +) |
| 13 | + |
| 14 | +// GetProcessStartTime returns the start time of the active process if still active |
| 15 | +func GetProcessStartTime(pid int) (string, error) { |
| 16 | + pidString := fmt.Sprintf("%d", pid) |
| 17 | + startTime, err := exec.Command("bash", "-c", "ps -o lstart= -p "+pidString).Output() |
| 18 | + if err != nil { |
| 19 | + return "", errors.Wrap(err, "failed to execute bash ps command") |
| 20 | + } |
| 21 | + return string(startTime), nil |
| 22 | +} |
| 23 | + |
| 24 | +// SaveCurrentPidAndStartTime stores current process id with start date in file extName.pid |
| 25 | +// Example: 325 Tue Dec 8 15:54:04 2020 |
| 26 | +func SaveCurrentPidAndStartTime(path string) error { |
| 27 | + pid := os.Getpid() |
| 28 | + pidString := fmt.Sprintf("%d", pid) |
| 29 | + startTime, err := GetProcessStartTime(pid) |
| 30 | + if err != nil { |
| 31 | + return errors.Wrap(err, "failed to execute bash ps command") |
| 32 | + } |
| 33 | + |
| 34 | + b := []byte(fmt.Sprintf("%s\t%s", pidString, startTime)) |
| 35 | + return errors.Wrap(ioutil.WriteFile(path, b, chmod), "extName.pid: failed to write") |
| 36 | +} |
| 37 | + |
| 38 | +// DeleteCurrentPidAndStartTime delete the file created by SaveCurrentPidAndStartTime |
| 39 | +func DeleteCurrentPidAndStartTime(path string) error { |
| 40 | + return errors.Wrap(os.Remove(path), "failed to delete "+path) |
| 41 | +} |
| 42 | + |
| 43 | +// ReadPidAndStartTime reads the stored pid and process start time from a file extName.pid |
| 44 | +// Returns 0 and "" if path not found |
| 45 | +func ReadPidAndStartTime(path string) (int, string, error) { |
| 46 | + b, err := ioutil.ReadFile(path) |
| 47 | + if err != nil { |
| 48 | + if os.IsNotExist(err) { |
| 49 | + return 0, "", nil |
| 50 | + } |
| 51 | + return 0, "", errors.Wrap(err, "extName.pid: failed to read:"+path) |
| 52 | + } |
| 53 | + data := strings.Split(string(b), "\t") |
| 54 | + if len(data) != 2 { |
| 55 | + return 0, "", errors.Wrap(err, "unexpected format in extName.pid:"+string(b)) |
| 56 | + } |
| 57 | + |
| 58 | + pid, err := strconv.Atoi(data[0]) |
| 59 | + if err != nil { |
| 60 | + return 0, "", errors.Wrap(err, "failed to convert pid:"+data[0]) |
| 61 | + } |
| 62 | + return pid, data[1], nil |
| 63 | +} |
| 64 | + |
| 65 | +// IsExtensionStillRunning checks if there is active process for the same extension name |
| 66 | +func IsExtensionStillRunning(path string) bool { |
| 67 | + // Check if we have a file record for previous process |
| 68 | + previousPid, previousStartTime, err := ReadPidAndStartTime(path) |
| 69 | + if err != nil || previousPid == 0 || previousStartTime == "" { |
| 70 | + return false |
| 71 | + } |
| 72 | + |
| 73 | + // Try to get previous process start time |
| 74 | + startTime, err := GetProcessStartTime(previousPid) |
| 75 | + if err != nil || startTime == "" { |
| 76 | + return false |
| 77 | + } |
| 78 | + |
| 79 | + return startTime == previousStartTime |
| 80 | +} |
0 commit comments