Enhance PID collection by adding namespace service support and extending timeout for systemd queries

This commit is contained in:
anonpenguin23 2026-02-19 10:24:49 +02:00
parent aa2da83969
commit 40600c3557

View File

@ -2,6 +2,9 @@ package report
import ( import (
"context" "context"
"fmt"
"os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -93,24 +96,58 @@ var managedServiceUnits = []string{
// collectManagedPIDs queries systemd for the MainPID of each known service. // collectManagedPIDs queries systemd for the MainPID of each known service.
// Returns a set of PIDs that are legitimately managed by systemd (not orphans). // Returns a set of PIDs that are legitimately managed by systemd (not orphans).
func collectManagedPIDs() map[int]bool { func collectManagedPIDs() map[int]bool {
// Hard deadline: stop querying if this takes too long (e.g., node with many namespaces).
deadline := time.Now().Add(10 * time.Second)
pids := make(map[int]bool) pids := make(map[int]bool)
// Collect PIDs from global services.
for _, unit := range managedServiceUnits { for _, unit := range managedServiceUnits {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) addMainPID(pids, unit)
out, err := runCmd(ctx, "systemctl", "show", unit, "--property=MainPID") }
cancel()
if err != nil { // Collect PIDs from namespace service instances.
continue // Scan the namespaces data directory (same pattern as GetProductionServices).
} namespacesDir := "/opt/orama/.orama/data/namespaces"
props := parseProperties(out) nsEntries, err := os.ReadDir(namespacesDir)
if pidStr, ok := props["MainPID"]; ok { if err == nil {
if pid, err := strconv.Atoi(pidStr); err == nil && pid > 0 { nsServiceTypes := []string{"rqlite", "olric", "gateway"}
pids[pid] = true for _, nsEntry := range nsEntries {
if !nsEntry.IsDir() {
continue
}
if time.Now().After(deadline) {
break
}
ns := nsEntry.Name()
for _, svcType := range nsServiceTypes {
envFile := filepath.Join(namespacesDir, ns, svcType+".env")
if _, err := os.Stat(envFile); err == nil {
unit := fmt.Sprintf("orama-namespace-%s@%s", svcType, ns)
addMainPID(pids, unit)
}
} }
} }
} }
return pids return pids
} }
// addMainPID queries systemd for a unit's MainPID and adds it to the set.
func addMainPID(pids map[int]bool, unit string) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
out, err := runCmd(ctx, "systemctl", "show", unit, "--property=MainPID")
cancel()
if err != nil {
return
}
props := parseProperties(out)
if pidStr, ok := props["MainPID"]; ok {
if pid, err := strconv.Atoi(pidStr); err == nil && pid > 0 {
pids[pid] = true
}
}
}
// isOramaProcess checks if a command string contains any orama-related process name. // isOramaProcess checks if a command string contains any orama-related process name.
func isOramaProcess(command string) bool { func isOramaProcess(command string) bool {
lower := strings.ToLower(command) lower := strings.ToLower(command)