P1-26: live job log viewer + WS browser fan-out hub
Closes the P1-21 remainder.
internal/server/ws/jobhub.go — new JobHub. Per-job_id set of
subscribers; each gets a 64-deep buffered channel with a writer
goroutine. Broadcast is non-blocking: if a subscriber is slow,
its channel fills and messages are dropped for that subscriber
only — the agent's read loop is never blocked by a stuck browser.
The agent dispatchAgentMessage path mirrors job.started /
job.progress / log.stream / job.finished envelopes onto the hub
in addition to its existing persistence work. The wire shape is
the same end-to-end, so client-side JS switches on env.type the
same way Go code does.
GET /api/jobs/{id}/stream is the browser endpoint. Auth via
session cookie (HTTP layer); upgrade; subscribe; pump until
context closes.
GET /jobs/{id} renders the live log page. Three states (queued/
running/succeeded/failed) drive the header pill, the progress
bar block, the failure summary panel, and the action button
(Cancel job while running, Back to host afterwards). Already-
persisted log lines are server-rendered on initial load; new
lines arrive over the WS and append to #log-stream. Auto-scrolls
unless the user scrolls up (a "⇢ Follow" pill re-attaches).
On job.finished the page reloads after 600ms to pick up the
final-state header rendered server-side.
POST /hosts/{id}/run-backup now sets HX-Redirect → /jobs/{job_id}
on success so HTMX lands the operator straight on the live log.
For non-HTMX callers (curl / plain form post) it 303s to the
same target.
store.ListJobLogs returns persisted log lines for initial render
on page load.
Browser-verified end-to-end: enrol → run a real backup against a
sibling restic/rest-server → live progress + 11 log lines stream
in → succeeded pill + final stats land after page reload.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -91,6 +91,48 @@ func (s *Store) AppendJobLog(ctx context.Context, jobID string, seq int64, ts ti
|
||||
return nil
|
||||
}
|
||||
|
||||
// JobLogLine is one persisted log line, ready to render.
|
||||
type JobLogLine struct {
|
||||
Seq int64
|
||||
TS time.Time
|
||||
Stream string // stdout|stderr|event
|
||||
Payload string
|
||||
}
|
||||
|
||||
// ListJobLogs returns persisted log lines for a job in seq order.
|
||||
// afterSeq lets pagers / reconnect-resuming clients fetch only the
|
||||
// tail; passing 0 returns from the beginning. limit caps the result
|
||||
// (0 means no cap).
|
||||
func (s *Store) ListJobLogs(ctx context.Context, jobID string, afterSeq int64, limit int) ([]JobLogLine, error) {
|
||||
q := `SELECT seq, ts, stream, payload FROM job_logs
|
||||
WHERE job_id = ? AND seq > ? ORDER BY seq ASC`
|
||||
args := []any{jobID, afterSeq}
|
||||
if limit > 0 {
|
||||
q += ` LIMIT ?`
|
||||
args = append(args, limit)
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, q, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: list job logs: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []JobLogLine
|
||||
for rows.Next() {
|
||||
var l JobLogLine
|
||||
var ts string
|
||||
if err := rows.Scan(&l.Seq, &ts, &l.Stream, &l.Payload); err != nil {
|
||||
return nil, fmt.Errorf("store: scan job log: %w", err)
|
||||
}
|
||||
t, perr := time.Parse(time.RFC3339Nano, ts)
|
||||
if perr != nil {
|
||||
return nil, fmt.Errorf("store: parse job log ts: %w", perr)
|
||||
}
|
||||
l.TS = t
|
||||
out = append(out, l)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// GetJob returns a job row.
|
||||
func (s *Store) GetJob(ctx context.Context, id string) (*Job, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
|
||||
Reference in New Issue
Block a user