Commit cb4e6af

Eric Bower  ·  2026-05-06 23:07:56 -0400 EDT
parent 33aaa49
refactor(gc): remove sessions older than 3 hours
1 files changed,  +14, -37
M main.go
+14, -37
 1@@ -777,10 +777,6 @@ func jobIDFor(name, workspace string, ts int64) string {
 2 	return fmt.Sprintf("%x", h[:4])
 3 }
 4 
 5-func shortSessionName(session, prefix string) string {
 6-	return strings.TrimPrefix(session, prefix)
 7-}
 8-
 9 func parseZMXList(output string) []SessionInfo {
10 	var sessions []SessionInfo
11 	lines := strings.Split(strings.TrimSpace(output), "\n")
12@@ -906,18 +902,6 @@ func syncArtifacts(cfg *Cfg, log *slog.Logger) error {
13 	return nil
14 }
15 
16-func filterSessions(sessions []SessionInfo, prefix string) []SessionInfo {
17-	var filtered []SessionInfo
18-	for _, s := range sessions {
19-		if strings.HasPrefix(s.Name, prefix) {
20-			cs := s
21-			cs.Short = shortSessionName(s.Name, prefix)
22-			filtered = append(filtered, cs)
23-		}
24-	}
25-	return filtered
26-}
27-
28 func allCompleted(sessions []SessionInfo) bool {
29 	for _, s := range sessions {
30 		if s.Ended == "" {
31@@ -1063,7 +1047,7 @@ func killSessions(names []string) error {
32 	return nil
33 }
34 
35-// runGC deletes completed CI zmx sessions that are not part of a running job.
36+// runGC kills all ci. zmx sessions older than 3 hours, regardless of status.
37 func runGC(cfg *Cfg) error {
38 	log := cfg.Logger.With("cmd", "gc")
39 	log.Debug("running garbage collection")
40@@ -1075,33 +1059,26 @@ func runGC(cfg *Cfg) error {
41 
42 	sessions := parseZMXList(string(listOutput))
43 
44-	// Group ci. sessions by job prefix: ci.<name>.<jobID>.
45-	groups := make(map[string][]SessionInfo)
46+	cutoff := time.Now().Add(-3 * time.Hour).Unix()
47+
48+	var toKill []string
49 	for _, s := range sessions {
50 		if !strings.HasPrefix(s.Name, "ci.") {
51 			continue
52 		}
53-		// Extract job prefix: ci.<name>.<jobID>.
54-		prefix := extractJobPrefix(s.Name)
55-		if prefix == "" {
56-			continue
57+
58+		if s.Created == "" {
59+			continue // skip sessions with no creation time
60 		}
61-		groups[prefix] = append(groups[prefix], s)
62-	}
63 
64-	// For each group, if all sessions are completed, kill them.
65-	var toKill []string
66-	for prefix, group := range groups {
67-		allDone := true
68-		for _, s := range group {
69-			if s.Ended == "" {
70-				allDone = false
71-				break
72-			}
73+		var created int64
74+		if _, err := fmt.Sscanf(s.Created, "%d", &created); err != nil {
75+			continue
76 		}
77-		if allDone {
78-			log.Debug("completed job, scheduling for gc", "prefix", prefix, "sessions", len(group))
79-			toKill = append(toKill, group[0].Name)
80+
81+		if created < cutoff {
82+			log.Debug("session expired, scheduling for gc", "session", s.Name, "created", s.Created)
83+			toKill = append(toKill, s.Name)
84 		}
85 	}
86