mirror of
https://github.com/cexll/myclaude.git
synced 2026-02-05 02:30:26 +08:00
- Move all source files to internal/{app,backend,config,executor,logger,parser,utils}
- Integrate third-party libraries: zerolog, goccy/go-json, gopsutil, cobra/viper
- Add comprehensive unit tests for utils package (94.3% coverage)
- Add performance benchmarks for string operations
- Fix error display: cleanup warnings no longer pollute Recent Errors
- Add GitHub Actions CI workflow
- Add Makefile for build automation
- Add README documentation
Generated with SWE-Agent.ai
Co-Authored-By: SWE-Agent.ai <noreply@swe-agent.ai>
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package logger
|
|
|
|
import (
|
|
"errors"
|
|
"math"
|
|
"time"
|
|
|
|
"github.com/shirou/gopsutil/v3/process"
|
|
)
|
|
|
|
func pidToInt32(pid int) (int32, bool) {
|
|
if pid <= 0 || pid > math.MaxInt32 {
|
|
return 0, false
|
|
}
|
|
return int32(pid), true
|
|
}
|
|
|
|
// isProcessRunning reports whether a process with the given pid appears to be running.
|
|
// It is intentionally conservative on errors to avoid deleting logs for live processes.
|
|
func isProcessRunning(pid int) bool {
|
|
pid32, ok := pidToInt32(pid)
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
exists, err := process.PidExists(pid32)
|
|
if err == nil {
|
|
return exists
|
|
}
|
|
|
|
// If we can positively identify that the process doesn't exist, report false.
|
|
if errors.Is(err, process.ErrorProcessNotRunning) {
|
|
return false
|
|
}
|
|
|
|
// Permission/inspection failures: assume it's running to be safe.
|
|
return true
|
|
}
|
|
|
|
// getProcessStartTime returns the start time of a process.
|
|
// Returns zero time if the start time cannot be determined.
|
|
func getProcessStartTime(pid int) time.Time {
|
|
pid32, ok := pidToInt32(pid)
|
|
if !ok {
|
|
return time.Time{}
|
|
}
|
|
|
|
proc, err := process.NewProcess(pid32)
|
|
if err != nil {
|
|
return time.Time{}
|
|
}
|
|
|
|
ms, err := proc.CreateTime()
|
|
if err != nil || ms <= 0 {
|
|
return time.Time{}
|
|
}
|
|
|
|
return time.UnixMilli(ms)
|
|
}
|
|
|
|
func IsProcessRunning(pid int) bool { return isProcessRunning(pid) }
|
|
|
|
func GetProcessStartTime(pid int) time.Time { return getProcessStartTime(pid) }
|