forked from longtao.wu/droboticflow
98 lines
3.1 KiB
Go
98 lines
3.1 KiB
Go
package worker
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type HTTPServer struct {
|
|
store store.Store
|
|
runtime *Runtime
|
|
}
|
|
|
|
func NewHTTPHandler(dataStore store.Store, runtime *Runtime) http.Handler {
|
|
server := &HTTPServer{store: dataStore, runtime: runtime}
|
|
router := chi.NewRouter()
|
|
|
|
router.Get("/worker-api/health", server.handleHealth)
|
|
router.Get("/worker-api/stats", server.handleStats)
|
|
router.Post("/worker-api/process-once", server.handleProcessOnce)
|
|
router.Post("/worker-api/runs/{id}/retry", server.handleRetry)
|
|
|
|
return router
|
|
}
|
|
|
|
func (server *HTTPServer) handleHealth(writer http.ResponseWriter, _ *http.Request) {
|
|
renderWorkerJSON(writer, http.StatusOK, map[string]string{"status": "ok", "service": "droboticflow-worker"})
|
|
}
|
|
|
|
func (server *HTTPServer) handleStats(writer http.ResponseWriter, request *http.Request) {
|
|
runs, err := server.store.ListRuns(request.Context(), "")
|
|
if err != nil {
|
|
renderWorkerJSON(writer, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
response := map[string]int{"queued": 0, "running": 0, "waitingForAudit": 0}
|
|
for _, run := range runs {
|
|
switch run.Status {
|
|
case domain.RunStatusQueued:
|
|
response["queued"]++
|
|
case domain.RunStatusRunning:
|
|
response["running"]++
|
|
case domain.RunStatusWaitingForAudit:
|
|
response["waitingForAudit"]++
|
|
}
|
|
}
|
|
renderWorkerJSON(writer, http.StatusOK, response)
|
|
}
|
|
|
|
func (server *HTTPServer) handleProcessOnce(writer http.ResponseWriter, request *http.Request) {
|
|
run, err := server.runtime.ProcessOnce(request.Context())
|
|
if err != nil {
|
|
renderWorkerJSON(writer, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
renderWorkerJSON(writer, http.StatusOK, map[string]any{"run": run})
|
|
}
|
|
|
|
func (server *HTTPServer) handleRetry(writer http.ResponseWriter, request *http.Request) {
|
|
run, err := server.store.GetRun(request.Context(), chi.URLParam(request, "id"))
|
|
if err != nil {
|
|
renderWorkerJSON(writer, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
if run == nil {
|
|
renderWorkerJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
|
return
|
|
}
|
|
run.Status = domain.RunStatusQueued
|
|
run.StartedAt = ""
|
|
run.EndedAt = ""
|
|
run.DurationMS = 0
|
|
run.CurrentNodeID = ""
|
|
run.NodeExecutions = []domain.NodeExecutionRecord{}
|
|
run.Logs = []domain.LogEntryRecord{}
|
|
run.ResourceSeries = []domain.ResourceSample{}
|
|
run.Artifacts = []domain.ExecutionArtifact{}
|
|
run.K8sRefs = []domain.K8sRef{}
|
|
run.Error = ""
|
|
run.AvailableAt = nowISO()
|
|
run.UpdatedAt = nowISO()
|
|
if err := server.store.UpsertRun(request.Context(), *run); err != nil {
|
|
renderWorkerJSON(writer, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
renderWorkerJSON(writer, http.StatusAccepted, run)
|
|
}
|
|
|
|
func renderWorkerJSON(writer http.ResponseWriter, status int, payload any) {
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
writer.WriteHeader(status)
|
|
_ = json.NewEncoder(writer).Encode(payload)
|
|
}
|