| 1 | package runnercfg |
| 2 | |
| 3 | import ( |
| 4 | "sort" |
| 5 | "sync" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // JobInfo is één job in het overzicht. |
| 10 | type JobInfo struct { |
| 11 | ID int64 `json:"id"` |
| 12 | Name string `json:"name"` |
| 13 | Project string `json:"project"` |
| 14 | Pipeline int64 `json:"pipeline"` |
| 15 | Image string `json:"image"` |
| 16 | Status string `json:"status"` |
| 17 | Started time.Time `json:"started_at"` |
| 18 | Finished time.Time `json:"finished_at,omitempty"` |
| 19 | URL string `json:"url,omitempty"` |
| 20 | } |
| 21 | |
| 22 | // Tracker houdt actieve en recente jobs en de pauze bij. Veilig voor |
| 23 | // gelijktijdig gebruik. |
| 24 | type Tracker struct { |
| 25 | mu sync.Mutex |
| 26 | active map[int64]JobInfo |
| 27 | recent []JobInfo |
| 28 | paused bool |
| 29 | images map[string]time.Time |
| 30 | } |
| 31 | |
| 32 | const recentMax = 25 |
| 33 | |
| 34 | // Start registreert een lopende job. |
| 35 | func (t *Tracker) Start(j JobInfo) { |
| 36 | t.mu.Lock() |
| 37 | defer t.mu.Unlock() |
| 38 | if t.active == nil { |
| 39 | t.active = map[int64]JobInfo{} |
| 40 | } |
| 41 | if j.Started.IsZero() { |
| 42 | j.Started = time.Now() |
| 43 | } |
| 44 | j.Status = "running" |
| 45 | t.active[j.ID] = j |
| 46 | if j.Image != "" { |
| 47 | if t.images == nil { |
| 48 | t.images = map[string]time.Time{} |
| 49 | } |
| 50 | t.images[j.Image] = time.Now() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Finish zet een job bij de recente jobs. |
| 55 | func (t *Tracker) Finish(id int64, status string) { |
| 56 | t.mu.Lock() |
| 57 | defer t.mu.Unlock() |
| 58 | j, ok := t.active[id] |
| 59 | if !ok { |
| 60 | return |
| 61 | } |
| 62 | delete(t.active, id) |
| 63 | j.Status, j.Finished = status, time.Now() |
| 64 | t.recent = append([]JobInfo{j}, t.recent...) |
| 65 | if len(t.recent) > recentMax { |
| 66 | t.recent = t.recent[:recentMax] |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Active geeft de lopende jobs, oudste eerst. |
| 71 | func (t *Tracker) Active() []JobInfo { |
| 72 | t.mu.Lock() |
| 73 | defer t.mu.Unlock() |
| 74 | out := make([]JobInfo, 0, len(t.active)) |
| 75 | for _, j := range t.active { |
| 76 | out = append(out, j) |
| 77 | } |
| 78 | sort.Slice(out, func(i, k int) bool { return out[i].Started.Before(out[k].Started) }) |
| 79 | return out |
| 80 | } |
| 81 | |
| 82 | // Recent geeft de laatst afgeronde jobs, nieuwste eerst. |
| 83 | func (t *Tracker) Recent() []JobInfo { |
| 84 | t.mu.Lock() |
| 85 | defer t.mu.Unlock() |
| 86 | return append([]JobInfo(nil), t.recent...) |
| 87 | } |
| 88 | |
| 89 | // SetPaused zet de pauze (drain): lopende jobs lopen af, nieuwe niet. |
| 90 | func (t *Tracker) SetPaused(on bool) { |
| 91 | t.mu.Lock() |
| 92 | t.paused = on |
| 93 | t.mu.Unlock() |
| 94 | } |
| 95 | |
| 96 | // Paused zegt of er geen nieuwe jobs opgepakt worden. |
| 97 | func (t *Tracker) Paused() bool { |
| 98 | t.mu.Lock() |
| 99 | defer t.mu.Unlock() |
| 100 | return t.paused |
| 101 | } |
| 102 | |
| 103 | // SafeToUpgrade: niets loopt meer. |
| 104 | func (t *Tracker) SafeToUpgrade() bool { |
| 105 | t.mu.Lock() |
| 106 | defer t.mu.Unlock() |
| 107 | return len(t.active) == 0 |
| 108 | } |
| 109 | |
| 110 | // StaleImages geeft de images die het langst niet gebruikt zijn, boven keep. |
| 111 | func (t *Tracker) StaleImages(keep int) []string { |
| 112 | t.mu.Lock() |
| 113 | defer t.mu.Unlock() |
| 114 | type iu struct { |
| 115 | ref string |
| 116 | at time.Time |
| 117 | } |
| 118 | var all []iu |
| 119 | inUse := map[string]bool{} |
| 120 | for _, j := range t.active { |
| 121 | inUse[j.Image] = true |
| 122 | } |
| 123 | for ref, at := range t.images { |
| 124 | all = append(all, iu{ref, at}) |
| 125 | } |
| 126 | sort.Slice(all, func(i, k int) bool { return all[i].at.After(all[k].at) }) |
| 127 | var out []string |
| 128 | for i, x := range all { |
| 129 | if i >= keep && !inUse[x.ref] { |
| 130 | out = append(out, x.ref) |
| 131 | delete(t.images, x.ref) |
| 132 | } |
| 133 | } |
| 134 | return out |
| 135 | } |