186 lines
5.9 KiB
Go
186 lines
5.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
"github.com/coder/websocket/wsjson"
|
|
|
|
"git.a-wan.net/alan/pipelines/examples"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// Handler serves the persistent connection. Canceling ctx stops active clients.
|
|
func Handler(ctx context.Context, stores ...*pipeline.Store) http.Handler {
|
|
mux := http.NewServeMux()
|
|
var runs *runner
|
|
var runErr error
|
|
if len(stores) > 0 {
|
|
runs, runErr = newRunner(ctx, stores[0])
|
|
if runErr == nil {
|
|
runs.schedulerWG.Add(1)
|
|
go runs.scheduleLoop()
|
|
}
|
|
}
|
|
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if runErr != nil {
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
_, _ = w.Write([]byte(`{"healthy":false}`))
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(`{"healthy":true}`))
|
|
})
|
|
mux.HandleFunc("GET /connect", func(w http.ResponseWriter, r *http.Request) {
|
|
conn, err := websocket.Accept(w, r, nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer conn.CloseNow()
|
|
conn.SetReadLimit(2 << 20)
|
|
if len(stores) > 0 {
|
|
items, err := stores[0].List(ctx)
|
|
if err != nil {
|
|
log.Printf("list pipelines: %v", err)
|
|
return
|
|
}
|
|
writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
err = wsjson.Write(writeCtx, conn, protocol.Message{Type: protocol.PipelineList, Pipelines: items, NotificationEndpoints: notificationEndpoints()})
|
|
cancel()
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
for {
|
|
var request protocol.Message
|
|
if err := wsjson.Read(ctx, conn, &request); err != nil {
|
|
return
|
|
}
|
|
log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version)
|
|
response := protocol.Message{Type: protocol.Pong, ID: request.ID}
|
|
if request.Type == protocol.List || request.Type == protocol.Submit {
|
|
response.NotificationEndpoints = notificationEndpoints()
|
|
}
|
|
if request.Type == protocol.Ping {
|
|
response.Time = time.Now().UTC().Format(time.RFC3339)
|
|
} else if request.Type == protocol.ListTemplates {
|
|
response.Type = protocol.Templates
|
|
response.Templates = examples.Catalog()
|
|
} else if request.Type == protocol.Validate {
|
|
graph, err := pipeline.Parse(request.Source)
|
|
response.Type = protocol.Validated
|
|
if err != nil {
|
|
response.Type = protocol.Error
|
|
response.Error = err.Error()
|
|
} else {
|
|
response.Graph = &graph
|
|
}
|
|
} else if request.Type == protocol.Submit && len(stores) > 0 {
|
|
response.Type = protocol.Submitted
|
|
response.Name = strings.TrimSpace(request.Name)
|
|
version, err := stores[0].SubmitChecked(ctx, request.Name, request.Source, request.CreateOnly, request.ExpectedVersion)
|
|
if err != nil {
|
|
response.Type = protocol.Error
|
|
response.Error = err.Error()
|
|
} else {
|
|
response.Version = version
|
|
response.Pipelines, err = stores[0].List(ctx)
|
|
if err != nil {
|
|
log.Printf("list pipelines after submission: %v", err)
|
|
}
|
|
for _, item := range response.Pipelines {
|
|
if item.Name == response.Name {
|
|
response.Detail, err = stores[0].Get(ctx, item.ID, version)
|
|
if err != nil {
|
|
log.Printf("load submitted pipeline: %v", err)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
} else if request.Type == protocol.GetPipeline && len(stores) > 0 {
|
|
response.Type = protocol.Pipeline
|
|
detail, err := stores[0].Get(ctx, request.PipelineID, request.Version)
|
|
if err != nil {
|
|
response.Type = protocol.Error
|
|
response.Error = err.Error()
|
|
} else {
|
|
response.Detail = detail
|
|
}
|
|
} else if request.Type == protocol.List && len(stores) > 0 {
|
|
response.Type = protocol.PipelineList
|
|
items, err := stores[0].List(ctx)
|
|
if err != nil {
|
|
response.Type = protocol.Error
|
|
response.Error = err.Error()
|
|
} else {
|
|
response.Pipelines = items
|
|
}
|
|
} else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun) {
|
|
var err error
|
|
switch request.Type {
|
|
case protocol.RunPipeline:
|
|
response.Type = protocol.RunStarted
|
|
response.RunDetail, err = runs.start(request)
|
|
case protocol.RetryRun:
|
|
response.Type = protocol.RunStarted
|
|
response.RunDetail, err = runs.retry(request)
|
|
case protocol.ListRuns:
|
|
response.Type = protocol.Runs
|
|
response.Runs, err = runs.listRuns()
|
|
case protocol.GetRun:
|
|
response.Type = protocol.Run
|
|
response.RunDetail, err = runs.getRun(request.RunID)
|
|
case protocol.CancelRun:
|
|
response.Type = protocol.Run
|
|
err = runs.cancel(request.RunID)
|
|
if err == nil {
|
|
response.RunDetail, err = runs.getRun(request.RunID)
|
|
}
|
|
}
|
|
if err != nil {
|
|
response.Type = protocol.Error
|
|
response.Error = err.Error()
|
|
}
|
|
} else {
|
|
response.Type = protocol.Error
|
|
response.Error = "unknown command: " + request.Type
|
|
if runErr != nil {
|
|
response.Error = "execution unavailable: " + runErr.Error()
|
|
}
|
|
}
|
|
writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
err := wsjson.Write(writeCtx, conn, response)
|
|
cancel()
|
|
if err != nil {
|
|
log.Printf("write response: %v", err)
|
|
return
|
|
}
|
|
selectedVersion := response.Version
|
|
if response.Detail != nil {
|
|
selectedVersion = response.Detail.Version
|
|
}
|
|
log.Printf("response sent: client=%s type=%q id=%q time=%q error=%q version=%d", r.RemoteAddr, response.Type, response.ID, response.Time, response.Error, selectedVersion)
|
|
}
|
|
})
|
|
return &runHandler{Handler: mux, runner: runs}
|
|
}
|
|
|
|
type runHandler struct {
|
|
http.Handler
|
|
runner *runner
|
|
}
|
|
|
|
// WaitRuns drains active execution before the database is closed on shutdown.
|
|
func WaitRuns(handler http.Handler) {
|
|
if h, ok := handler.(*runHandler); ok && h.runner != nil {
|
|
h.runner.schedulerWG.Wait()
|
|
h.runner.wg.Wait()
|
|
}
|
|
}
|