refactor(process): replace os file ops with coreio.Local in pidfile and registry

Replace all os.ReadFile, os.WriteFile, os.MkdirAll, os.Remove calls in
pidfile.go and registry.go with coreio.Local equivalents (Read, Write,
EnsureDir, Delete). Add forge.lthn.ai/core/go-io v0.1.2 as a direct dependency.

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Snider 2026-03-16 18:27:34 +00:00
parent c3b9374ae1
commit 715c14f048
4 changed files with 29 additions and 19 deletions

2
go.mod
View file

@ -11,6 +11,8 @@ require (
)
require (
forge.lthn.ai/core/go-io v0.1.2 // indirect
forge.lthn.ai/core/go-log v0.0.2 // indirect
github.com/99designs/gqlgen v0.17.88 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/agnivade/levenshtein v1.2.1 // indirect

4
go.sum
View file

@ -2,6 +2,10 @@ forge.lthn.ai/core/api v0.1.2 h1:VKHOQhjWcNCG4Xf9lJfrABRwk/+1tp8YsdQzPNVxzek=
forge.lthn.ai/core/api v0.1.2/go.mod h1:vDkEihL/Cn1yKF8oA2jjf1CVOcd7kOP/WYWoIHIu2+E=
forge.lthn.ai/core/go v0.3.1 h1:5FMTsUhLcxSr07F9q3uG0Goy4zq4eLivoqi8shSY4UM=
forge.lthn.ai/core/go v0.3.1/go.mod h1:gE6c8h+PJ2287qNhVUJ5SOe1kopEwHEquvinstpuyJc=
forge.lthn.ai/core/go-io v0.1.2 h1:q8hj2jtOFqAgHlBr5wsUAOXtaFkxy9gqGrQT/il0WYA=
forge.lthn.ai/core/go-io v0.1.2/go.mod h1:PbNKW1Q25ywSOoQXeGdQHbV5aiIrTXvHIQ5uhplA//g=
forge.lthn.ai/core/go-log v0.0.2 h1:zJEgbajs5AjvhYzsbyybzn+S2Titiv56r3BG5E1cNUo=
forge.lthn.ai/core/go-log v0.0.2/go.mod h1:r14MXKOD3LF/sI8XUJQhRk/SZHBE7jAFVuCfgkXoZPw=
forge.lthn.ai/core/go-ws v0.2.0 h1:w1uG5MgUeGoWGu1hBh8liTXcsMJDrjvPGeIsg6fyvYk=
forge.lthn.ai/core/go-ws v0.2.0/go.mod h1:iDbJuR1NT27czjtNIluxnEdLrnfsYQdEBIrsoZnpkCk=
github.com/99designs/gqlgen v0.17.88 h1:neMQDgehMwT1vYIOx/w5ZYPUU/iMNAJzRO44I5Intoc=

View file

@ -8,6 +8,8 @@ import (
"strings"
"sync"
"syscall"
coreio "forge.lthn.ai/core/go-io"
)
// PIDFile manages a process ID file for single-instance enforcement.
@ -27,8 +29,8 @@ func (p *PIDFile) Acquire() error {
p.mu.Lock()
defer p.mu.Unlock()
if data, err := os.ReadFile(p.path); err == nil {
pid, err := strconv.Atoi(strings.TrimSpace(string(data)))
if data, err := coreio.Local.Read(p.path); err == nil {
pid, err := strconv.Atoi(strings.TrimSpace(data))
if err == nil && pid > 0 {
if proc, err := os.FindProcess(pid); err == nil {
if err := proc.Signal(syscall.Signal(0)); err == nil {
@ -36,17 +38,17 @@ func (p *PIDFile) Acquire() error {
}
}
}
_ = os.Remove(p.path)
_ = coreio.Local.Delete(p.path)
}
if dir := filepath.Dir(p.path); dir != "." {
if err := os.MkdirAll(dir, 0755); err != nil {
if err := coreio.Local.EnsureDir(dir); err != nil {
return fmt.Errorf("failed to create PID directory: %w", err)
}
}
pid := os.Getpid()
if err := os.WriteFile(p.path, []byte(strconv.Itoa(pid)), 0644); err != nil {
if err := coreio.Local.Write(p.path, strconv.Itoa(pid)); err != nil {
return fmt.Errorf("failed to write PID file: %w", err)
}
@ -57,7 +59,7 @@ func (p *PIDFile) Acquire() error {
func (p *PIDFile) Release() error {
p.mu.Lock()
defer p.mu.Unlock()
return os.Remove(p.path)
return coreio.Local.Delete(p.path)
}
// Path returns the PID file path.
@ -69,12 +71,12 @@ func (p *PIDFile) Path() string {
// Returns (pid, true) if the process is alive, (pid, false) if dead/stale,
// or (0, false) if the file doesn't exist or is invalid.
func ReadPID(path string) (int, bool) {
data, err := os.ReadFile(path)
data, err := coreio.Local.Read(path)
if err != nil {
return 0, false
}
pid, err := strconv.Atoi(strings.TrimSpace(string(data)))
pid, err := strconv.Atoi(strings.TrimSpace(data))
if err != nil || pid <= 0 {
return 0, false
}

View file

@ -7,6 +7,8 @@ import (
"strings"
"syscall"
"time"
coreio "forge.lthn.ai/core/go-io"
)
// DaemonEntry records a running daemon in the registry.
@ -47,7 +49,7 @@ func (r *Registry) Register(entry DaemonEntry) error {
entry.Started = time.Now()
}
if err := os.MkdirAll(r.dir, 0755); err != nil {
if err := coreio.Local.EnsureDir(r.dir); err != nil {
return err
}
@ -56,12 +58,12 @@ func (r *Registry) Register(entry DaemonEntry) error {
return err
}
return os.WriteFile(r.entryPath(entry.Code, entry.Daemon), data, 0644)
return coreio.Local.Write(r.entryPath(entry.Code, entry.Daemon), string(data))
}
// Unregister removes a daemon entry from the registry.
func (r *Registry) Unregister(code, daemon string) error {
return os.Remove(r.entryPath(code, daemon))
return coreio.Local.Delete(r.entryPath(code, daemon))
}
// Get reads a single daemon entry and checks whether its process is alive.
@ -69,19 +71,19 @@ func (r *Registry) Unregister(code, daemon string) error {
func (r *Registry) Get(code, daemon string) (*DaemonEntry, bool) {
path := r.entryPath(code, daemon)
data, err := os.ReadFile(path)
data, err := coreio.Local.Read(path)
if err != nil {
return nil, false
}
var entry DaemonEntry
if err := json.Unmarshal(data, &entry); err != nil {
_ = os.Remove(path)
if err := json.Unmarshal([]byte(data), &entry); err != nil {
_ = coreio.Local.Delete(path)
return nil, false
}
if !isAlive(entry.PID) {
_ = os.Remove(path)
_ = coreio.Local.Delete(path)
return nil, false
}
@ -97,19 +99,19 @@ func (r *Registry) List() ([]DaemonEntry, error) {
var alive []DaemonEntry
for _, path := range matches {
data, err := os.ReadFile(path)
data, err := coreio.Local.Read(path)
if err != nil {
continue
}
var entry DaemonEntry
if err := json.Unmarshal(data, &entry); err != nil {
_ = os.Remove(path)
if err := json.Unmarshal([]byte(data), &entry); err != nil {
_ = coreio.Local.Delete(path)
continue
}
if !isAlive(entry.PID) {
_ = os.Remove(path)
_ = coreio.Local.Delete(path)
continue
}