feat: add durable schedule management and multi-node crash recovery

This commit is contained in:
2026-09-17 16:28:58 +08:00
parent d65cf96ea7
commit 6805506450
43 changed files with 2952 additions and 29 deletions

View File

@@ -0,0 +1,106 @@
package httpserver
import (
"net/http"
"net/url"
"strconv"
"strings"
"git.yuntuo.hk/k2safe/orbit/internal/fault"
core "git.yuntuo.hk/k2safe/orbit/model"
"git.yuntuo.hk/k2safe/orbit/platform/auth"
"git.yuntuo.hk/k2safe/orbit/pulse/schedule"
)
// RegisterSchedules 接入同一 HTTP 信封、认证、大小和严格 JSON 边界。
func RegisterSchedules(server *http.Server, s *schedule.Service) {
mux := server.Handler.(*http.ServeMux)
mux.HandleFunc("POST /v1/schedules", wrap(func(r *http.Request, t auth.Token) (any, int, error) {
var in schedule.CreateInput
if err := decodeBody(r, &in); err != nil {
return nil, 0, err
}
key, err := idempotencyKey(r)
if err != nil {
return nil, 0, err
}
out, err := s.Create(r.Context(), t, key, in)
return out, createdStatus(out.Deduplicated), err
}))
mux.HandleFunc("GET /v1/schedules", wrap(func(r *http.Request, t auth.Token) (any, int, error) {
q, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
return nil, 0, fault.Invalid("invalid query encoding")
}
for k, v := range q {
if len(v) != 1 || (k != "queue_id" && k != "limit" && k != "cursor") {
return nil, 0, fault.Invalid("invalid schedule query")
}
}
var in schedule.PageInput
in.Cursor = q.Get("cursor")
if q.Has("limit") {
in.Limit, err = strconv.Atoi(q.Get("limit"))
if err != nil || in.Limit < 1 {
return nil, 0, fault.Invalid("invalid limit")
}
}
out, err := s.List(r.Context(), t, core.ID(q.Get("queue_id")), in)
return out, 200, err
}))
mux.HandleFunc("GET /v1/schedules/{id}", wrap(func(r *http.Request, t auth.Token) (any, int, error) {
if _, err := historyQuery(r, false); err != nil {
return nil, 0, err
}
out, err := s.Get(r.Context(), t, core.ID(r.PathValue("id")))
return out, 200, err
}))
mux.HandleFunc("PATCH /v1/schedules/{id}", wrap(func(r *http.Request, t auth.Token) (any, int, error) {
var in schedule.PublishInput
if err := decodeBody(r, &in); err != nil {
return nil, 0, err
}
key, err := idempotencyKey(r)
if err != nil {
return nil, 0, err
}
out, err := s.Publish(r.Context(), t, core.ID(r.PathValue("id")), key, in)
return out, 200, err
}))
mux.HandleFunc("POST /v1/schedules/{command}", wrap(func(r *http.Request, t auth.Token) (any, int, error) {
id, command, ok := strings.Cut(r.PathValue("command"), ":")
if !ok || (command != "pause" && command != "resume") {
return nil, 0, fault.NotFound
}
var in schedule.ControlInput
if err := decodeBody(r, &in); err != nil {
return nil, 0, err
}
key, err := idempotencyKey(r)
if err != nil {
return nil, 0, err
}
out, err := s.Control(r.Context(), t, core.ID(id), key, in, command == "pause")
return out, 200, err
}))
mux.HandleFunc("GET /v1/schedules/{id}/versions/{version}", wrap(func(r *http.Request, t auth.Token) (any, int, error) {
if _, err := historyQuery(r, false); err != nil {
return nil, 0, err
}
raw := r.PathValue("version")
version, err := strconv.ParseUint(raw, 10, 64)
if err != nil || version == 0 || strconv.FormatUint(version, 10) != raw {
return nil, 0, fault.Invalid("invalid version")
}
out, err := s.Version(r.Context(), t, core.ID(r.PathValue("id")), version)
return out, 200, err
}))
mux.HandleFunc("GET /v1/schedules/{id}/occurrences", wrap(func(r *http.Request, t auth.Token) (any, int, error) {
in, err := historyQuery(r, true)
if err != nil {
return nil, 0, err
}
out, err := s.Occurrences(r.Context(), t, core.ID(r.PathValue("id")), schedule.PageInput{Limit: in.Limit, Cursor: in.Cursor})
return out, 200, err
}))
}