BlueGit

timfa bluegit

Files

bluegit / internal / runner / control.go

control.go

control.go 2.3 KiB Download Raw
1package runner
2
3import (
4 "context"
5 "fmt"
6 "strings"
7
8 "gitlab.com/Timfa/bluegit/internal/images"
9 "gitlab.com/Timfa/bluegit/internal/runnercfg"
10 "gitlab.com/Timfa/bluegit/internal/store"
11)
12
13// Beheerbare instellingen (runnercfg) voor de ingebouwde én de aparte
14// runner: limiet op gelijktijdige jobs, limieten per container,
15// pull-policy, eigen presets en pauze. Alles wordt live toegepast.
16
17// maxSlots is de bovengrens van het slotkanaal; de echte limiet is ctlLimit.
18const maxSlots = 64
19
20type control struct {
21 limit int
22 runArgs []string
23 pull string
24 timeout int
25 presets map[string]images.Override
26 keep bool // werkmappen na afloop bewaren (cleanup_work uit)
27 set bool
28}
29
30// Configure past instellingen live toe. presets is het resultaat van
31// Settings.Overrides (ci.presets plus eigen presets).
32func (r *Runner) Configure(s runnercfg.Settings, presets map[string]images.Override) {
33 r.mu.Lock()
34 defer r.mu.Unlock()
35 r.ctl = control{limit: s.Concurrency, runArgs: s.RunArgs(), pull: s.PullPolicy, timeout: s.JobTimeoutMin, presets: presets, keep: !s.CleanupWork, set: true}
36 if n := strings.TrimSpace(s.Name); n != "" {
37 r.Name = n
38 }
39 if r.Jobs != nil {
40 r.Jobs.SetPaused(s.Paused)
41 }
42}
43
44func (r *Runner) controls() control {
45 r.mu.RLock()
46 defer r.mu.RUnlock()
47 return r.ctl
48}
49
50func (r *Runner) presetSet() map[string]images.Override {
51 c := r.controls()
52 if c.set && c.presets != nil {
53 return c.presets
54 }
55 return r.Presets
56}
57
58// Paused zegt of de runner geen nieuwe jobs oppakt (drain).
59func (r *Runner) Paused() bool {
60 return r.Jobs != nil && r.Jobs.Paused()
61}
62
63// Active is het aantal jobs dat deze runner nu draait.
64func (r *Runner) Active() int {
65 if r.slots == nil {
66 return 0
67 }
68 return len(r.slots)
69}
70
71func (r *Runner) trackStart(job store.Job) {
72 if r.Jobs == nil {
73 return
74 }
75 info := runnercfg.JobInfo{ID: job.ID, Name: job.Name, Pipeline: job.PipelineID, Image: job.Image}
76 if r.Store != nil {
77 if repo, err := r.Store.RepoByID(context.Background(), job.RepoID); err == nil {
78 info.Project = repo.FullPath()
79 info.URL = fmt.Sprintf("%s/%s/-/pipelines/%d/jobs/%d", strings.TrimRight(r.BaseURL, "/"), repo.FullPath(), job.PipelineID, job.ID)
80 }
81 }
82 r.Jobs.Start(info)
83}
84
85func (r *Runner) trackDone(id int64, status string) {
86 if r.Jobs != nil {
87 r.Jobs.Finish(id, status)
88 }
89}