BlueGit

timfa bluegit

Files

bluegit / internal / server / queue.go

queue.go

fix: ververs actieve pipelines en toon de wachtrijplek
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
1cf0f0da History
queue.go 2.6 KiB Download Raw
1package server
2
3import (
4 "context"
5 "fmt"
6
7 "gitlab.com/Timfa/bluegit/internal/ci"
8 "gitlab.com/Timfa/bluegit/internal/store"
9)
10
11// pipeSnap is één pipeline in de volgorde waarin Steve jobs oppakt.
12type pipeSnap struct {
13 Jobs []store.Job
14}
15
16// queueRanks geeft elke startklare queued job zijn plek in Steves
17// globale wachtrij. ci.concurrency beperkt hoeveel jobs tegelijk
18// running worden; die lopende jobs houden hun slot en krijgen geen
19// nummer. De wachtenden blijven 1, 2, 3, … tot er een slot vrijkomt.
20// Een job die nog op een voorganger of een resourcegroep wacht, heeft
21// geen plek: Steve slaat die over.
22func (s *Server) queueRanks(ctx context.Context) map[int64]int {
23 pipes, err := s.Store.ActivePipelines(ctx)
24 if err != nil {
25 return map[int64]int{}
26 }
27 snaps := make([]pipeSnap, 0, len(pipes))
28 for _, p := range pipes {
29 jobs, err := s.Store.JobsByPipeline(ctx, p.ID)
30 if err != nil {
31 continue
32 }
33 snaps = append(snaps, pipeSnap{Jobs: jobs})
34 }
35 return rankQueue(snaps)
36}
37
38func queueText(tr func(string, ...any) string, ranks map[int64]int, id int64) string {
39 if ranks == nil {
40 return ""
41 }
42 n := ranks[id]
43 if n < 1 {
44 return ""
45 }
46 return tr("pipe.queuePos", n)
47}
48
49// rankQueue nummert startklare jobs vanaf 1. Pipelines staan op id,
50// jobs daarbinnen op positie. Jobs die al running zijn vullen een
51// concurrency-slot en staan niet in de telling, zodat de eerstvolgende
52// wachtende job #1 is, ook als Steve al vol zit.
53func rankQueue(pipes []pipeSnap) map[int64]int {
54 busy := map[string]bool{}
55 for _, p := range pipes {
56 for _, j := range p.Jobs {
57 if j.Status == "running" && j.ResourceGroup != "" {
58 busy[fmt.Sprintf("%d\t%s", j.RepoID, j.ResourceGroup)] = true
59 }
60 }
61 }
62 ranks := map[int64]int{}
63 n := 0
64 for _, p := range pipes {
65 specs, status := ciSpecs(p.Jobs)
66 for _, j := range p.Jobs {
67 if j.Status != "queued" {
68 continue
69 }
70 if j.ResourceGroup != "" && busy[fmt.Sprintf("%d\t%s", j.RepoID, j.ResourceGroup)] {
71 continue
72 }
73 if ci.Gate(specs, status, specs[indexJob(p.Jobs, j.Name)]) != "ready" {
74 continue
75 }
76 n++
77 ranks[j.ID] = n
78 }
79 }
80 return ranks
81}
82
83func ciSpecs(jobs []store.Job) ([]ci.Job, map[string]string) {
84 specs := make([]ci.Job, len(jobs))
85 status := map[string]string{}
86 for i, j := range jobs {
87 specs[i] = ci.Job{
88 Name: j.Name, Stage: j.Stage, Needs: j.Needs, Optional: j.Optional,
89 NeedsSet: j.NeedsSet, AllowFailure: j.AllowFailure, When: j.When,
90 }
91 status[j.Name] = j.Status
92 }
93 return specs, status
94}
95
96func indexJob(jobs []store.Job, name string) int {
97 for i, j := range jobs {
98 if j.Name == name {
99 return i
100 }
101 }
102 return 0
103}