add federated worker registration and heartbeats

This commit is contained in:
kami
2026-07-26 20:32:45 +04:00
parent 26011ed33c
commit 6e4df6d2ff
3 changed files with 111 additions and 0 deletions
+39
View File
@@ -10,6 +10,7 @@ import (
"orchestra/internal/authz"
"orchestra/internal/delivery"
"orchestra/internal/domain"
"orchestra/internal/federation"
"orchestra/internal/herdr"
"orchestra/internal/operations"
"orchestra/internal/orchestrator"
@@ -81,6 +82,7 @@ func main() {
}
}
mux := http.NewServeMux()
workers := &federation.Registry{}
providerHealth := map[string]*provider.Supervisor{}
surface := func(r *http.Request) authz.Surface {
v := authz.ParseSurface(r.Header.Get("X-Orchestra-Surface"))
@@ -330,6 +332,43 @@ func main() {
}
json.NewEncoder(w).Encode(out)
})
mux.HandleFunc("/v1/federation/workers", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
json.NewEncoder(w).Encode(workers.Snapshot())
return
}
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", 405)
return
}
var worker federation.Worker
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&worker) != nil {
http.Error(w, "invalid worker", 400)
return
}
if err := workers.Register(worker); err != nil {
http.Error(w, err.Error(), 400)
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(worker)
})
mux.HandleFunc("/v1/federation/workers/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || !strings.HasSuffix(r.URL.Path, "/heartbeat") {
http.Error(w, "not found", 404)
return
}
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(parts) != 5 {
http.Error(w, "not found", 404)
return
}
if err := workers.Heartbeat(parts[3]); err != nil {
http.Error(w, err.Error(), 404)
return
}
w.WriteHeader(http.StatusNoContent)
})
if base := os.Getenv("ORCHESTRA_GITEA_URL"); base != "" {
g := provider.Gitea{BaseURL: base, Token: os.Getenv("ORCHESTRA_GITEA_TOKEN"), WebhookSecret: os.Getenv("ORCHESTRA_GITEA_WEBHOOK_SECRET"), Owner: os.Getenv("ORCHESTRA_GITEA_OWNER"), Repo: os.Getenv("ORCHESTRA_GITEA_REPO")}
reflecting := provider.ReflectingSink{Sink: s, Tasks: s, Reflector: g}