streamd v0.2.2: session uptimes, egress DNS, Support, agent/Widevine
All checks were successful
release / build-and-upload (push) Successful in 7m2s
All checks were successful
release / build-and-upload (push) Successful in 7m2s
Dashboard Live uptimes + Recent runs from stream_sessions; overview.recent_sessions. WireGuard netns resolv.conf + missing-deps WARN. Support donations card. Agent start/stop/update, job_backoff default off, wvkey ensure/venv, phonecap retries, modhost YAML enrich, DRM SetCredentials requires keys.
This commit is contained in:
parent
3ae1dd58b3
commit
a091922efa
34 changed files with 2391 additions and 154 deletions
211
apps/streamd/internal/db/sessions.go
Normal file
211
apps/streamd/internal/db/sessions.go
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Session end reasons recorded when a run closes.
|
||||
const (
|
||||
SessionStopped = "stopped"
|
||||
SessionNREExit = "nre_exit"
|
||||
SessionOrphaned = "orphaned"
|
||||
SessionTimeout = "timeout"
|
||||
)
|
||||
|
||||
// SessionRetention is how long closed run history is kept.
|
||||
const SessionRetention = 90 * 24 * time.Hour
|
||||
|
||||
// StreamSession is one media worker run: start → stop/crash with a duration.
|
||||
type StreamSession struct {
|
||||
ID int64 `json:"id"`
|
||||
StreamID int64 `json:"stream_id"`
|
||||
StreamName string `json:"stream_name,omitempty"`
|
||||
StreamTitle string `json:"stream_title,omitempty"`
|
||||
StartedAt string `json:"started_at"`
|
||||
EndedAt string `json:"ended_at,omitempty"`
|
||||
DurationS float64 `json:"duration_s"`
|
||||
EndReason string `json:"end_reason,omitempty"`
|
||||
HealthAtEnd string `json:"health_at_end,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Store) migrateSessions() error {
|
||||
_, err := s.DB.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS stream_sessions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
stream_id INTEGER NOT NULL REFERENCES streams(id) ON DELETE CASCADE,
|
||||
started_at TEXT NOT NULL,
|
||||
ended_at TEXT NOT NULL DEFAULT '',
|
||||
duration_s REAL NOT NULL DEFAULT 0,
|
||||
end_reason TEXT NOT NULL DEFAULT '',
|
||||
health_at_end TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_stream_started ON stream_sessions(stream_id, started_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_ended ON stream_sessions(ended_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_open ON stream_sessions(stream_id, ended_at);
|
||||
`)
|
||||
return err
|
||||
}
|
||||
|
||||
// OpenSession records a new run for streamID. Any previously open row for the
|
||||
// same stream is closed as orphaned (e.g. process crash without a clean stop).
|
||||
func (s *Store) OpenSession(streamID int64) (int64, error) {
|
||||
if streamID == 0 {
|
||||
return 0, fmt.Errorf("stream id required")
|
||||
}
|
||||
_ = s.CloseOpenSessions(streamID, SessionOrphaned, "stopped")
|
||||
started := now()
|
||||
res, err := s.DB.Exec(`
|
||||
INSERT INTO stream_sessions(stream_id, started_at)
|
||||
VALUES(?, ?)`, streamID, started)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.LastInsertId()
|
||||
}
|
||||
|
||||
// CloseSession ends one open session by id. Already-closed rows are a no-op.
|
||||
func (s *Store) CloseSession(id int64, reason, healthAtEnd string) error {
|
||||
if id == 0 {
|
||||
return nil
|
||||
}
|
||||
var started string
|
||||
var ended string
|
||||
err := s.DB.QueryRow(`SELECT started_at, ended_at FROM stream_sessions WHERE id=?`, id).
|
||||
Scan(&started, &ended)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ended != "" {
|
||||
return nil
|
||||
}
|
||||
endedAt := now()
|
||||
dur := durationSeconds(started, endedAt)
|
||||
if reason == "" {
|
||||
reason = SessionStopped
|
||||
}
|
||||
_, err = s.DB.Exec(`
|
||||
UPDATE stream_sessions
|
||||
SET ended_at=?, duration_s=?, end_reason=?, health_at_end=?
|
||||
WHERE id=? AND ended_at=''`,
|
||||
endedAt, dur, reason, healthAtEnd, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// CloseOpenSessions ends every open session for a stream.
|
||||
func (s *Store) CloseOpenSessions(streamID int64, reason, healthAtEnd string) error {
|
||||
if streamID == 0 {
|
||||
return nil
|
||||
}
|
||||
rows, err := s.DB.Query(`
|
||||
SELECT id FROM stream_sessions WHERE stream_id=? AND ended_at=''`, streamID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
var ids []int64
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
for _, id := range ids {
|
||||
if err := s.CloseSession(id, reason, healthAtEnd); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListRecentSessions returns the newest closed runs across all streams.
|
||||
func (s *Store) ListRecentSessions(limit int) ([]StreamSession, error) {
|
||||
if limit <= 0 {
|
||||
limit = 50
|
||||
}
|
||||
if limit > 200 {
|
||||
limit = 200
|
||||
}
|
||||
rows, err := s.DB.Query(`
|
||||
SELECT ss.id, ss.stream_id, COALESCE(st.name,''), COALESCE(st.title,''),
|
||||
ss.started_at, ss.ended_at, ss.duration_s, ss.end_reason, ss.health_at_end
|
||||
FROM stream_sessions ss
|
||||
LEFT JOIN streams st ON st.id = ss.stream_id
|
||||
WHERE ss.ended_at <> ''
|
||||
ORDER BY ss.ended_at DESC, ss.id DESC
|
||||
LIMIT ?`, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []StreamSession
|
||||
for rows.Next() {
|
||||
var ss StreamSession
|
||||
if err := rows.Scan(
|
||||
&ss.ID, &ss.StreamID, &ss.StreamName, &ss.StreamTitle,
|
||||
&ss.StartedAt, &ss.EndedAt, &ss.DurationS, &ss.EndReason, &ss.HealthAtEnd,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, ss)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// ListStreamSessions returns recent closed runs for one stream.
|
||||
func (s *Store) ListStreamSessions(streamID int64, limit int) ([]StreamSession, error) {
|
||||
if limit <= 0 {
|
||||
limit = 50
|
||||
}
|
||||
rows, err := s.DB.Query(`
|
||||
SELECT ss.id, ss.stream_id, COALESCE(st.name,''), COALESCE(st.title,''),
|
||||
ss.started_at, ss.ended_at, ss.duration_s, ss.end_reason, ss.health_at_end
|
||||
FROM stream_sessions ss
|
||||
LEFT JOIN streams st ON st.id = ss.stream_id
|
||||
WHERE ss.stream_id=? AND ss.ended_at <> ''
|
||||
ORDER BY ss.ended_at DESC, ss.id DESC
|
||||
LIMIT ?`, streamID, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []StreamSession
|
||||
for rows.Next() {
|
||||
var ss StreamSession
|
||||
if err := rows.Scan(
|
||||
&ss.ID, &ss.StreamID, &ss.StreamName, &ss.StreamTitle,
|
||||
&ss.StartedAt, &ss.EndedAt, &ss.DurationS, &ss.EndReason, &ss.HealthAtEnd,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, ss)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// PruneSessions drops closed sessions older than SessionRetention.
|
||||
func (s *Store) PruneSessions() error {
|
||||
cutoff := time.Now().UTC().Add(-SessionRetention).Format(time.RFC3339)
|
||||
_, err := s.DB.Exec(`
|
||||
DELETE FROM stream_sessions
|
||||
WHERE ended_at <> '' AND ended_at < ?`, cutoff)
|
||||
return err
|
||||
}
|
||||
|
||||
func durationSeconds(startedAt, endedAt string) float64 {
|
||||
start, err1 := time.Parse(time.RFC3339, startedAt)
|
||||
end, err2 := time.Parse(time.RFC3339, endedAt)
|
||||
if err1 != nil || err2 != nil {
|
||||
return 0
|
||||
}
|
||||
d := end.Sub(start).Seconds()
|
||||
if d < 0 {
|
||||
return 0
|
||||
}
|
||||
return d
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue