bfa006f4ab
- SSH key management: generate ed25519 keypairs or import public keys from UI (/ssh-keys), per-machine key selection in Machines form, one-time private key download with hash verification - Fix engine to use machine-specific SSH key (was hardcoded to server key) - Job log persistence: write to job_logs table (DB) with batched inserts, buffer of 50 lines; GetAllFiltered with status/pair/date range filters - EventBus refactor: per-job subscriber channels, global channel, non-blocking - SSE endpoints: /jobs/stream (all), /jobs/:id/log/stream (per-job live) - JobDetail page: live log streaming, auto-scroll, cancel, duration - JobHistory: filters (pair, status, date range), pagination, link to detail - Cleanup scheduler: daily purge of job_logs and finished jobs older than SYNCSERVER_RETENTION_DAYS (default 30) - Migration 0002: indexes on job_logs(job_id), jobs(status,created_at), jobs(sync_pair_id)
101 lines
2.1 KiB
Go
101 lines
2.1 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/syncserver/internal/syncengine"
|
|
)
|
|
|
|
type SSEHandler struct {
|
|
engine *syncengine.Engine
|
|
}
|
|
|
|
func NewSSEHandler(engine *syncengine.Engine) *SSEHandler {
|
|
return &SSEHandler{engine: engine}
|
|
}
|
|
|
|
func (h *SSEHandler) StreamAll(w http.ResponseWriter, r *http.Request) {
|
|
flusher, ok := w.(http.Flusher)
|
|
if !ok {
|
|
http.Error(w, "SSE not supported", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
w.Header().Set("Connection", "keep-alive")
|
|
w.Header().Set("X-Accel-Buffering", "no")
|
|
flusher.Flush()
|
|
|
|
if h.engine == nil {
|
|
return
|
|
}
|
|
|
|
events, unsub := h.engine.SubscribeGlobal()
|
|
defer unsub()
|
|
|
|
for {
|
|
select {
|
|
case evt := <-events:
|
|
data, _ := json.Marshal(evt)
|
|
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", evt.Type, data)
|
|
flusher.Flush()
|
|
case <-r.Context().Done():
|
|
return
|
|
case <-time.After(30 * time.Second):
|
|
fmt.Fprintf(w, ": keepalive\n\n")
|
|
flusher.Flush()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (h *SSEHandler) StreamJob(w http.ResponseWriter, r *http.Request) {
|
|
jobIDStr := r.URL.Query().Get("job_id")
|
|
if jobIDStr == "" {
|
|
http.Error(w, "job_id required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
jobID, err := strconv.ParseInt(jobIDStr, 10, 64)
|
|
if err != nil {
|
|
http.Error(w, "invalid job_id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
flusher, ok := w.(http.Flusher)
|
|
if !ok {
|
|
http.Error(w, "SSE not supported", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
w.Header().Set("Connection", "keep-alive")
|
|
w.Header().Set("X-Accel-Buffering", "no")
|
|
flusher.Flush()
|
|
|
|
if h.engine == nil {
|
|
return
|
|
}
|
|
|
|
events, unsub := h.engine.SubscribeJob(jobID)
|
|
defer unsub()
|
|
|
|
for {
|
|
select {
|
|
case evt := <-events:
|
|
data, _ := json.Marshal(evt)
|
|
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", evt.Type, data)
|
|
flusher.Flush()
|
|
case <-r.Context().Done():
|
|
return
|
|
case <-time.After(30 * time.Second):
|
|
fmt.Fprintf(w, ": keepalive\n\n")
|
|
flusher.Flush()
|
|
}
|
|
}
|
|
}
|