fix: address review findings for self-update (#15)
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -181,6 +182,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
resp := response{RequestID: requestID, Image: req.Image, Tag: req.Tag, Matched: len(services)}
|
||||
pending := map[int]int64{} // index into resp.Results -> job ID
|
||||
// An agent's own service goes last: jobs that reach an agent while it
|
||||
// replaces itself are lost, and messages to one agent arrive in order.
|
||||
slices.SortStableFunc(services, func(a, b model.Service) int { return selfRank(a) - selfRank(b) })
|
||||
for _, svc := range services {
|
||||
res, job := h.handle(work, svc, req.Tag, started, log)
|
||||
resp.Results = append(resp.Results, res)
|
||||
@@ -252,6 +256,14 @@ func (h *Handler) handle(ctx context.Context, svc model.Service, tag string, sta
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// selfRank sorts an agent's own service after all others.
|
||||
func selfRank(svc model.Service) int {
|
||||
if svc.Self {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (h *Handler) setJob(res *result, job model.Job) {
|
||||
res.JobID = job.ID
|
||||
res.JobURL = h.cfg.PublicURL + "/jobs/" + strconv.FormatInt(job.ID, 10)
|
||||
|
||||
Reference in New Issue
Block a user