| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | "time" |
| 9 | |
| 10 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 11 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 12 | "gitlab.com/Timfa/bluegit/internal/store" |
| 13 | ) |
| 14 | |
| 15 | // gitMaintFirstDelay is de pauze na het starten, zodat de website |
| 16 | // eerst opkomt. In tests lager zetten. |
| 17 | var gitMaintFirstDelay = 2 * time.Minute |
| 18 | |
| 19 | // gitMaintRepoTimeout is de maximale duur per repository. |
| 20 | var gitMaintRepoTimeout = gitrepo.DefaultMaintTimeout |
| 21 | |
| 22 | // GitMaintRepoResult is de uitkomst voor één repository. |
| 23 | type GitMaintRepoResult struct { |
| 24 | Repo string `json:"repo"` |
| 25 | OK bool `json:"ok"` |
| 26 | Duration int64 `json:"duration_ms"` |
| 27 | Error string `json:"error,omitempty"` |
| 28 | } |
| 29 | |
| 30 | // GitMaintReport is de laatste onderhoudsronde. |
| 31 | type GitMaintReport struct { |
| 32 | Started time.Time `json:"started,omitempty"` |
| 33 | Finished time.Time `json:"finished,omitempty"` |
| 34 | Repos int `json:"repos"` |
| 35 | OK int `json:"ok"` |
| 36 | Skipped int `json:"skipped"` |
| 37 | Error string `json:"error,omitempty"` |
| 38 | Results []GitMaintRepoResult `json:"results,omitempty"` |
| 39 | } |
| 40 | |
| 41 | type gitMaintState struct { |
| 42 | mu sync.Mutex |
| 43 | running bool |
| 44 | last GitMaintReport |
| 45 | } |
| 46 | |
| 47 | // GitMaintenanceSweeper draait periodiek onderhoud op alle bare |
| 48 | // repositories. Uit als maintenance_interval "off" is. |
| 49 | func (s *Server) GitMaintenanceSweeper(ctx context.Context) { |
| 50 | every, on, err := s.Cfg.Git.MaintenanceEvery() |
| 51 | if err != nil { |
| 52 | if s.Log != nil { |
| 53 | s.Log.Error("git maintenance", "err", err) |
| 54 | } |
| 55 | return |
| 56 | } |
| 57 | if !on { |
| 58 | if s.Log != nil { |
| 59 | s.Log.Info("git maintenance disabled") |
| 60 | } |
| 61 | return |
| 62 | } |
| 63 | if s.Log != nil { |
| 64 | s.Log.Info("git maintenance scheduled", "interval", every.String()) |
| 65 | } |
| 66 | first := time.NewTimer(gitMaintFirstDelay) |
| 67 | defer first.Stop() |
| 68 | tick := time.NewTicker(every) |
| 69 | defer tick.Stop() |
| 70 | for { |
| 71 | select { |
| 72 | case <-ctx.Done(): |
| 73 | return |
| 74 | case <-first.C: |
| 75 | case <-tick.C: |
| 76 | } |
| 77 | s.RunGitMaintenance(ctx, "") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // RunGitMaintenance onderhoudt alle bare repositories, of één als |
| 82 | // repoPath (eigenaar/naam) gezet is. Eén ronde tegelijk; een tweede |
| 83 | // aanroep krijgt ok=false. Fouten op één repo slaan die over. |
| 84 | func (s *Server) RunGitMaintenance(ctx context.Context, repoPath string) (GitMaintReport, bool) { |
| 85 | s.gitMaint.mu.Lock() |
| 86 | if s.gitMaint.running { |
| 87 | s.gitMaint.mu.Unlock() |
| 88 | return s.gitMaint.last, false |
| 89 | } |
| 90 | s.gitMaint.running = true |
| 91 | s.gitMaint.mu.Unlock() |
| 92 | defer func() { |
| 93 | s.gitMaint.mu.Lock() |
| 94 | s.gitMaint.running = false |
| 95 | s.gitMaint.mu.Unlock() |
| 96 | }() |
| 97 | |
| 98 | rep := GitMaintReport{Started: time.Now().UTC()} |
| 99 | defer func() { |
| 100 | rep.Finished = time.Now().UTC() |
| 101 | s.gitMaint.mu.Lock() |
| 102 | s.gitMaint.last = rep |
| 103 | s.gitMaint.mu.Unlock() |
| 104 | }() |
| 105 | |
| 106 | storage := s.StoragePath() |
| 107 | if storage == "" { |
| 108 | if s.Log != nil { |
| 109 | s.Log.Info("git maintenance skipped", "reason", "no storage") |
| 110 | } |
| 111 | return rep, true |
| 112 | } |
| 113 | repos, err := s.reposForMaintenance(ctx, repoPath) |
| 114 | if err != nil { |
| 115 | rep.Error = err.Error() |
| 116 | if s.Log != nil { |
| 117 | s.Log.Error("git maintenance list", "err", err) |
| 118 | } |
| 119 | return rep, true |
| 120 | } |
| 121 | gitBin := s.Git |
| 122 | if gitBin == "" { |
| 123 | gitBin = s.Cfg.Git.Binary |
| 124 | } |
| 125 | for _, repo := range repos { |
| 126 | if ctx.Err() != nil { |
| 127 | break |
| 128 | } |
| 129 | path := gitrepo.Dir(storage, repo.OwnerPath, repo.Name) |
| 130 | name := repo.FullPath() |
| 131 | repoCtx, cancel := context.WithTimeout(ctx, gitMaintRepoTimeout) |
| 132 | res := gitrepo.Maintain(repoCtx, gitBin, path) |
| 133 | cancel() |
| 134 | item := GitMaintRepoResult{Repo: name, OK: res.OK, Duration: res.Duration.Milliseconds(), Error: res.Err} |
| 135 | rep.Repos++ |
| 136 | if res.OK { |
| 137 | rep.OK++ |
| 138 | if s.Log != nil { |
| 139 | s.Log.Info("git maintenance", "repo", name, "duration", res.Duration.Round(time.Millisecond).String()) |
| 140 | } |
| 141 | } else { |
| 142 | rep.Skipped++ |
| 143 | if s.Log != nil { |
| 144 | s.Log.Error("git maintenance skip", "repo", name, "err", res.Err) |
| 145 | } |
| 146 | } |
| 147 | rep.Results = append(rep.Results, item) |
| 148 | } |
| 149 | if s.Log != nil { |
| 150 | s.Log.Info("git maintenance done", "repos", rep.Repos, "ok", rep.OK, "skipped", rep.Skipped) |
| 151 | } |
| 152 | return rep, true |
| 153 | } |
| 154 | |
| 155 | func (s *Server) reposForMaintenance(ctx context.Context, repoPath string) ([]store.Repo, error) { |
| 156 | if repoPath == "" { |
| 157 | return s.Store.AllRepos(ctx) |
| 158 | } |
| 159 | owner, name, ok := splitRepoPath(repoPath) |
| 160 | if !ok { |
| 161 | return nil, errBadRepoPath |
| 162 | } |
| 163 | repo, err := s.Store.RepoByPath(ctx, owner, name) |
| 164 | if err != nil { |
| 165 | return nil, err |
| 166 | } |
| 167 | return []store.Repo{repo}, nil |
| 168 | } |
| 169 | |
| 170 | func splitRepoPath(p string) (owner, name string, ok bool) { |
| 171 | p = strings.Trim(strings.TrimSpace(p), "/") |
| 172 | owner, name, ok = strings.Cut(p, "/") |
| 173 | if !ok || owner == "" || name == "" || strings.Contains(name, "/") { |
| 174 | return "", "", false |
| 175 | } |
| 176 | return owner, name, true |
| 177 | } |
| 178 | |
| 179 | var errBadRepoPath = fail(400, i18n.E("err.notFound")) |
| 180 | |
| 181 | func (s *Server) gitMaintStatus() map[string]any { |
| 182 | every, on, err := s.Cfg.Git.MaintenanceEvery() |
| 183 | interval := "" |
| 184 | if err == nil && on { |
| 185 | interval = every.String() |
| 186 | } |
| 187 | s.gitMaint.mu.Lock() |
| 188 | defer s.gitMaint.mu.Unlock() |
| 189 | out := map[string]any{ |
| 190 | "enabled": on && err == nil, |
| 191 | "interval": interval, |
| 192 | "running": s.gitMaint.running, |
| 193 | } |
| 194 | last := s.gitMaint.last |
| 195 | if !last.Started.IsZero() { |
| 196 | out["last_started"] = last.Started.Format(time.RFC3339) |
| 197 | out["last_finished"] = last.Finished.Format(time.RFC3339) |
| 198 | out["repos"] = last.Repos |
| 199 | out["ok"] = last.OK |
| 200 | out["skipped"] = last.Skipped |
| 201 | out["results"] = last.Results |
| 202 | if last.Error != "" { |
| 203 | out["error"] = last.Error |
| 204 | } |
| 205 | } |
| 206 | return out |
| 207 | } |
| 208 | |
| 209 | func (s *Server) apiAdminGitMaintenance(w http.ResponseWriter, r *http.Request) { |
| 210 | if !apiUser(r).InstanceAdmin { |
| 211 | s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) |
| 212 | return |
| 213 | } |
| 214 | if r.Method == http.MethodPost { |
| 215 | var in struct { |
| 216 | Repo string `json:"repo"` |
| 217 | } |
| 218 | if err := readJSON(r, &in); err != nil { |
| 219 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 220 | return |
| 221 | } |
| 222 | rep, started := s.RunGitMaintenance(r.Context(), in.Repo) |
| 223 | if !started { |
| 224 | http.Error(w, "maintenance already running\n", http.StatusConflict) |
| 225 | return |
| 226 | } |
| 227 | writeJSON(w, http.StatusOK, s.reportJSON(rep)) |
| 228 | return |
| 229 | } |
| 230 | writeJSON(w, http.StatusOK, s.gitMaintStatus()) |
| 231 | } |
| 232 | |
| 233 | func (s *Server) reportJSON(rep GitMaintReport) map[string]any { |
| 234 | out := s.gitMaintStatus() |
| 235 | out["repos"] = rep.Repos |
| 236 | out["ok"] = rep.OK |
| 237 | out["skipped"] = rep.Skipped |
| 238 | out["results"] = rep.Results |
| 239 | if rep.Error != "" { |
| 240 | out["error"] = rep.Error |
| 241 | } |
| 242 | if !rep.Started.IsZero() { |
| 243 | out["last_started"] = rep.Started.Format(time.RFC3339) |
| 244 | } |
| 245 | if !rep.Finished.IsZero() { |
| 246 | out["last_finished"] = rep.Finished.Format(time.RFC3339) |
| 247 | } |
| 248 | return out |
| 249 | } |