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
+14, -37
......@@ -777,10 +777,6 @@ func jobIDFor(name, workspace string, ts int64) string {
777777 return fmt.Sprintf("%x", h[:4])
778778 }
779779
780-func shortSessionName(session, prefix string) string {
781- return strings.TrimPrefix(session, prefix)
782-}
783-
784780 func parseZMXList(output string) []SessionInfo {
785781 var sessions []SessionInfo
786782 lines := strings.Split(strings.TrimSpace(output), "\n")
......@@ -906,18 +902,6 @@ func syncArtifacts(cfg *Cfg, log *slog.Logger) error {
906902 return nil
907903 }
908904
909-func filterSessions(sessions []SessionInfo, prefix string) []SessionInfo {
910- var filtered []SessionInfo
911- for _, s := range sessions {
912- if strings.HasPrefix(s.Name, prefix) {
913- cs := s
914- cs.Short = shortSessionName(s.Name, prefix)
915- filtered = append(filtered, cs)
916- }
917- }
918- return filtered
919-}
920-
921905 func allCompleted(sessions []SessionInfo) bool {
922906 for _, s := range sessions {
923907 if s.Ended == "" {
......@@ -1063,7 +1047,7 @@ func killSessions(names []string) error {
10631047 return nil
10641048 }
10651049
1066-// runGC deletes completed CI zmx sessions that are not part of a running job.
1050+// runGC kills all ci. zmx sessions older than 3 hours, regardless of status.
10671051 func runGC(cfg *Cfg) error {
10681052 log := cfg.Logger.With("cmd", "gc")
10691053 log.Debug("running garbage collection")
......@@ -1075,33 +1059,26 @@ func runGC(cfg *Cfg) error {
10751059
10761060 sessions := parseZMXList(string(listOutput))
10771061
1078- // Group ci. sessions by job prefix: ci.<name>.<jobID>.
1079- groups := make(map[string][]SessionInfo)
1062+ cutoff := time.Now().Add(-3 * time.Hour).Unix()
1063+
1064+ var toKill []string
10801065 for _, s := range sessions {
10811066 if !strings.HasPrefix(s.Name, "ci.") {
10821067 continue
10831068 }
1084- // Extract job prefix: ci.<name>.<jobID>.
1085- prefix := extractJobPrefix(s.Name)
1086- if prefix == "" {
1087- continue
1069+
1070+ if s.Created == "" {
1071+ continue // skip sessions with no creation time
10881072 }
1089- groups[prefix] = append(groups[prefix], s)
1090- }
10911073
1092- // For each group, if all sessions are completed, kill them.
1093- var toKill []string
1094- for prefix, group := range groups {
1095- allDone := true
1096- for _, s := range group {
1097- if s.Ended == "" {
1098- allDone = false
1099- break
1100- }
1074+ var created int64
1075+ if _, err := fmt.Sscanf(s.Created, "%d", &created); err != nil {
1076+ continue
11011077 }
1102- if allDone {
1103- log.Debug("completed job, scheduling for gc", "prefix", prefix, "sessions", len(group))
1104- toKill = append(toKill, group[0].Name)
1078+
1079+ if created < cutoff {
1080+ log.Debug("session expired, scheduling for gc", "session", s.Name, "created", s.Created)
1081+ toKill = append(toKill, s.Name)
11051082 }
11061083 }
11071084