* feat(activity): report exact client app version, build, and channel
The admin Activity page could not name the build a session was streaming
from. Android already sent X-Silo-Client-Version and the server already
stored it intact, but playbackClientDisplayName routed it through
shortPlaybackClientVersion, which strips non-numeric runes, truncates to
two components, and drops a trailing ".0" — so a client reporting "1.0.0"
rendered as "Silo Android TV 1". The Apple clients sent no client name or
version at all and fell back to user-agent sniffing.
Adds two additive, opaque wire fields alongside the existing client
headers — X-Silo-Client-Build (<=64) and X-Silo-Client-Channel (<=32) —
with client_playback_context.app_build/app_channel as the v3 fallback,
which is also where the previously discarded app_version now gets used.
The server never parses, compares, or enum-validates either value: Apple
uses a per-platform TestFlight sequence and Android a per-marketing-
version counter, and keeping them opaque lets both coexist without a
shared scheme. Any future minimum-version gating belongs on
client_version, which is semver.
Only the named-client branch of playbackClientDisplayName stops
truncating; the user-agent branch keeps shortPlaybackClientVersion, so
browser labels stay "Chrome 120" rather than a full UA version string.
The compact session row is unchanged in width — it is shared with
AdminDashboard, AdminStats, and HouseholdStreamsPanel — and the exact
string lands in the row tooltip and a new Client card in the expanded
panel.
Diagnostic logs carry client_name/version/build/channel on both
"playback plan decided" lines and on session expiry. opslog stores an
open attrs JSONB, so this needs no migration. activity_log is
deliberately untouched: it is the highest-volume table and the value is
constant per device.
Jellyfin compat sessions keep an empty build — the MediaBrowser auth
header vocabulary has no build concept, and synthesizing one from a user
agent would be a guess.
Part of the client-version-visibility work spanning silo-android and
silo-apple.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* fix(activity): resolve client identity without polluting client_version
Review follow-up on the client build/channel work. Fourteen findings; the
substantive ones:
The v3 body fallback took client_playback_context.app_version whenever the
header was absent. The web player sends the literal "web" there and sends no
X-Silo-Client, so every browser session would have stamped client_version="web"
— the one field the contract promises is semver and the field a future
minimum-version gate has to key on. client_playback_context carries no app name,
so the body can never identify a nameless client anyway; the fallback now
applies only to a client that sent X-Silo-Client, and a test pins the "web"
case.
An over-long app_build or app_channel in the start body failed the whole request
with 400 while the same value in a header was silently clamped — an opaque
diagnostic label could refuse playback. validateCapabilitiesV3 now clamps both
with the same helper the header path uses, which is what the docs already
claimed.
Route events posted out of band resolved identity from headers only, so a client
reporting its build in the start body attributed plan_selected to a build and
every later event of the same attempt to none. They now fill empty fields from
the session, as the replan path already did.
playbackClientFullDisplayName discarded build and channel whenever the client
reported no name, so the new Client card could never show a build for a
user-agent-labelled session. It now qualifies whatever label the compact
formatter resolved, which also drops its duplicated name+version assembly.
normalizeClientMetadataValue truncated by bytes; a multi-byte header value cut
mid-rune yields invalid UTF-8, which Postgres rejects — and the per-node session
upserts share one transaction, so one malformed client string would fail that
whole node's sync. It now clamps on a rune boundary.
replan-request.schema.json never got app_build/app_channel even though
ReplanRequestV3 reuses ClientPlaybackContextV3 and validates the same bounds. A
new contract test asserts every $def the two request schemas share is identical,
so the copies cannot drift again.
Also: the four client log attrs move to ClientInfo.LogAttrs(), which is now
their single definition and omits fields the client did not report rather than
persisting empty keys into opslog; startPlannedPlaybackV3 takes the resolved
identity instead of re-parsing the headers; client_label_full is omitted when it
would repeat client_label; getSessionClientLabelFull delegates to
getSessionClientLabel instead of re-implementing it; the Activity search matches
the exact label so a build number is findable; and the web ClientPlaybackContextV3
type mirrors the two new optional fields.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* fix(activity): clamp client identity at the request boundary, by runes
Follow-up to bot review on the previous commit.
The 64/32 clamp for X-Silo-Client-Build / -Channel only ran where newSession
stamped its fields, but the resolved ClientInfo is written straight to the
plan-decision log and to playback_route_events. A client sending a header-sized
build reached both despite the published bound. ClientInfo.Normalized() is now
the single definition of those limits and runs at the request boundary —
playbackClientInfoFromRequest and playbackClientInfoForStartV3 — with newSession
still normalizing because identities also arrive from the Jellyfin and
Audiobookshelf compat surfaces.
normalizeClientMetadataValue now clamps by runes rather than bytes. The bounds
are published to clients as JSON Schema maxLength, which counts characters, so a
byte clamp cut values the contract calls valid — a 32-character emoji channel
was 128 bytes. It also scrubs invalid UTF-8 outright rather than only after a
mid-rune cut, since a header may carry bytes that were never valid UTF-8 and a
text column refuses them.
Two tests cover it: oversized headers clamp at the boundary, and a 40-rune
multi-byte channel lands on the 32-character bound as valid UTF-8.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* fix(activity): strip control characters from client identity
A JSON NUL escape in a v3 start body's app_version, app_build or app_channel
decodes to a real NUL. That is valid UTF-8, so the UTF-8 repair leaves it and
TrimSpace does not treat it as whitespace — but Postgres refuses NUL in a text
column. The per-node session upserts share one transaction, so a single such
start would stop every live session on that node from reconciling until the
offending session went away. Headers cannot carry it (net/http rejects bytes
below 0x20), which is why only the body path this PR added is exposed.
normalizeClientMetadataValue now strips control characters outright rather than
NUL alone: none of them belong in an identity label rendered in the admin UI and
written to structured logs.
Reported by Codex review on b43b7ef06.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* fix(activity): satisfy goconst and misspell on the changed log lines
CI's `golangci-lint --new-from-merge-base` failed on the previous commits: the
two decision-log calls were reformatted into slice literals, which brought their
"component" key inside the changed-lines window where goconst flags it against
the existing logComponentKey constant, and a doc comment used the British
"labelled". Both lines now use the constant, and the spelling is corrected here
and in docs/settings-api.md.
The file's other 16 "component" literals are left alone: CI only requires the
lines a branch touches to be clean, and rewriting them would bury this change in
unrelated churn.
Verified with the same command and version CI runs (golangci-lint v2.12.2,
--new-from-merge-base=origin/main): 0 issues.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* refactor(playback): require context-aware session starts
startPlannedPlaybackV3 probed for StartSessionWithFilesContext with a type
assertion and fell back to the context-free StartSessionWithFiles. The context
is how the reporting client's identity reaches the new session, so any
implementation missing the method would start sessions carrying no client name,
version, build or channel — silently, and now that build and channel ride the
same path, silently losing more.
SessionManagerInterface requires the method instead, so a non-conforming
implementation fails to compile rather than dropping the identity at run time.
The one test double gains a three-line method; production already implemented it.
Raised as a nitpick by CodeRabbit review; pre-existing, but it is this PR's data
that the fallback drops.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
533 lines
17 KiB
Go
533 lines
17 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/Silo-Server/silo-server/internal/cache"
|
|
evt "github.com/Silo-Server/silo-server/internal/events"
|
|
)
|
|
|
|
// SessionSync represents the data needed to sync a playback session to the
|
|
// playback_sessions_sync table in PostgreSQL.
|
|
type SessionSync struct {
|
|
SessionID string
|
|
UserID int
|
|
ProfileID string
|
|
MediaFileID int
|
|
RequestedMediaFileID int
|
|
PlayMethod string // current live transport method for admin session views
|
|
ReportingNode string
|
|
ClientIP string
|
|
ClientName string
|
|
ClientVersion string
|
|
ClientBuild string
|
|
ClientChannel string
|
|
ClientUserAgent string
|
|
AudioTrackIndex int
|
|
TranscodeAudio bool
|
|
StreamBitrateKbps int
|
|
TranscodeNodeURL string
|
|
TargetResolution string
|
|
TargetVideoCodec string
|
|
TargetAudioCodec string
|
|
TargetBitrateKbps int
|
|
TranscodeHWAccel string
|
|
StartedAt time.Time
|
|
UpdatedAt time.Time
|
|
PositionSeconds float64
|
|
IsPaused bool
|
|
HasWebSocket bool
|
|
IsJellyfinCompat bool
|
|
}
|
|
|
|
// AggregateData represents the aggregate counts for a single user that are
|
|
// synced to the user_aggregates table in PostgreSQL.
|
|
type AggregateData struct {
|
|
TotalWatched int
|
|
FavoritesCount int
|
|
WatchlistCount int
|
|
ActiveNode string
|
|
}
|
|
|
|
// SessionSyncProvider returns the current set of active sessions to reconcile.
|
|
// This is typically a closure that reads from the SessionManager.
|
|
type SessionSyncProvider func() []SessionSync
|
|
|
|
// PreSyncHook is called before each reconciliation cycle. Implementations
|
|
// can use it to expire idle sessions from the in-memory manager so they are
|
|
// no longer included in the snapshot sent to the database.
|
|
type PreSyncHook func()
|
|
|
|
// Reconciler performs background reconciliation of user data from per-user
|
|
// SQLite databases into the central PostgreSQL instance.
|
|
type Reconciler struct {
|
|
pool *pgxpool.Pool
|
|
nodeName string
|
|
sessionProvider SessionSyncProvider
|
|
interval time.Duration
|
|
stop chan struct{}
|
|
EventBus cache.EventBus
|
|
EventsHub *evt.Hub
|
|
PreSync PreSyncHook
|
|
// syncMu guards syncRunning/syncPending. Session syncs are coalesced onto a
|
|
// single owner so concurrent callers (the periodic tick plus request-path
|
|
// start/stop triggers) can never commit an older session snapshot after a
|
|
// newer one — which would resurrect stopped sessions or drop freshly
|
|
// started ones — and so request goroutines never queue behind a slow sync.
|
|
syncMu sync.Mutex
|
|
syncRunning bool
|
|
syncPending bool
|
|
}
|
|
|
|
// NewReconciler creates a new Reconciler with sensible defaults. The default
|
|
// reconciliation interval is 30 seconds. The sessionProvider may be nil if
|
|
// session sync is not needed (e.g. in tests).
|
|
func NewReconciler(pool *pgxpool.Pool, nodeName string, sp SessionSyncProvider) *Reconciler {
|
|
return &Reconciler{
|
|
pool: pool,
|
|
nodeName: strings.TrimSpace(nodeName),
|
|
sessionProvider: sp,
|
|
interval: 15 * time.Second,
|
|
stop: make(chan struct{}),
|
|
}
|
|
}
|
|
|
|
// ReconcileSessions upserts the given sessions into the playback_sessions_sync
|
|
// table. Each session is inserted or updated based on its session_id primary
|
|
// key, making the operation idempotent.
|
|
func (r *Reconciler) ReconcileSessions(ctx context.Context, sessions []SessionSync) error {
|
|
if len(sessions) == 0 {
|
|
return nil
|
|
}
|
|
|
|
grouped := make(map[string][]SessionSync)
|
|
for _, session := range sessions {
|
|
grouped[session.ReportingNode] = append(grouped[session.ReportingNode], session)
|
|
}
|
|
|
|
for reportingNode, nodeSessions := range grouped {
|
|
if err := r.ReconcileNodeSessions(ctx, reportingNode, nodeSessions); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ReconcileNodeSessions upserts the sessions currently active on one reporting
|
|
// node, then removes any stale rows for that same node that are no longer
|
|
// present in the provided snapshot.
|
|
func (r *Reconciler) ReconcileNodeSessions(ctx context.Context, reportingNode string, sessions []SessionSync) error {
|
|
reportingNode = strings.TrimSpace(reportingNode)
|
|
|
|
tx, err := r.pool.Begin(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("beginning transaction: %w", err)
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
|
|
currentSessions, err := loadNodeSessionsSnapshot(ctx, tx, reportingNode)
|
|
if err != nil {
|
|
return fmt.Errorf("loading existing sessions for node %s: %w", reportingNode, err)
|
|
}
|
|
normalizedIncoming := normalizeSessionSyncs(reportingNode, sessions)
|
|
changed := !sessionSnapshotsEqual(currentSessions, normalizedIncoming)
|
|
|
|
sessionIDs := make([]string, 0, len(sessions))
|
|
for _, s := range sessions {
|
|
sessionIDs = append(sessionIDs, s.SessionID)
|
|
sessionNode := strings.TrimSpace(s.ReportingNode)
|
|
if sessionNode == "" {
|
|
sessionNode = reportingNode
|
|
}
|
|
_, err := tx.Exec(ctx, `
|
|
INSERT INTO playback_sessions_sync
|
|
(session_id, user_id, profile_id, media_file_id, requested_media_file_id, play_method,
|
|
reporting_node, started_at, updated_at, last_sync_at, client_ip,
|
|
client_name, client_version, client_build, client_channel, client_user_agent,
|
|
audio_track_index, transcode_audio, stream_bitrate_kbps, transcode_node_url,
|
|
target_resolution, target_video_codec, target_audio_codec, target_bitrate_kbps,
|
|
transcode_hw_accel, position_seconds, is_paused, has_websocket, compat_origin)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, NOW(), $10::inet, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28)
|
|
ON CONFLICT (session_id) DO UPDATE SET
|
|
user_id = EXCLUDED.user_id,
|
|
profile_id = EXCLUDED.profile_id,
|
|
media_file_id = EXCLUDED.media_file_id,
|
|
requested_media_file_id = EXCLUDED.requested_media_file_id,
|
|
play_method = EXCLUDED.play_method,
|
|
reporting_node = EXCLUDED.reporting_node,
|
|
started_at = EXCLUDED.started_at,
|
|
updated_at = EXCLUDED.updated_at,
|
|
client_ip = EXCLUDED.client_ip,
|
|
client_name = EXCLUDED.client_name,
|
|
client_version = EXCLUDED.client_version,
|
|
client_build = EXCLUDED.client_build,
|
|
client_channel = EXCLUDED.client_channel,
|
|
client_user_agent = EXCLUDED.client_user_agent,
|
|
audio_track_index = EXCLUDED.audio_track_index,
|
|
transcode_audio = EXCLUDED.transcode_audio,
|
|
stream_bitrate_kbps = EXCLUDED.stream_bitrate_kbps,
|
|
transcode_node_url = EXCLUDED.transcode_node_url,
|
|
target_resolution = EXCLUDED.target_resolution,
|
|
target_video_codec = EXCLUDED.target_video_codec,
|
|
target_audio_codec = EXCLUDED.target_audio_codec,
|
|
target_bitrate_kbps = EXCLUDED.target_bitrate_kbps,
|
|
transcode_hw_accel = EXCLUDED.transcode_hw_accel,
|
|
position_seconds = EXCLUDED.position_seconds,
|
|
is_paused = EXCLUDED.is_paused,
|
|
has_websocket = EXCLUDED.has_websocket,
|
|
compat_origin = EXCLUDED.compat_origin,
|
|
last_sync_at = NOW()
|
|
`, s.SessionID, s.UserID, s.ProfileID, s.MediaFileID, nullableInt(s.RequestedMediaFileID), s.PlayMethod,
|
|
sessionNode, s.StartedAt, s.UpdatedAt, nullableIP(s.ClientIP),
|
|
nullableString(s.ClientName), nullableString(s.ClientVersion), nullableString(s.ClientBuild),
|
|
nullableString(s.ClientChannel), nullableString(s.ClientUserAgent),
|
|
s.AudioTrackIndex, s.TranscodeAudio, nullableInt(s.StreamBitrateKbps), nullableString(s.TranscodeNodeURL),
|
|
nullableString(s.TargetResolution), nullableString(s.TargetVideoCodec),
|
|
nullableString(s.TargetAudioCodec), nullableInt(s.TargetBitrateKbps),
|
|
nullableString(s.TranscodeHWAccel), normalizePositionSeconds(s.PositionSeconds),
|
|
s.IsPaused, s.HasWebSocket, s.IsJellyfinCompat)
|
|
if err != nil {
|
|
return fmt.Errorf("upserting session %s: %w", s.SessionID, err)
|
|
}
|
|
}
|
|
|
|
if len(sessionIDs) == 0 {
|
|
if _, err := tx.Exec(ctx, `
|
|
DELETE FROM playback_sessions_sync
|
|
WHERE COALESCE(reporting_node, '') = $1
|
|
`, reportingNode); err != nil {
|
|
return fmt.Errorf("deleting empty snapshot for node %s: %w", reportingNode, err)
|
|
}
|
|
} else {
|
|
if _, err := tx.Exec(ctx, `
|
|
DELETE FROM playback_sessions_sync
|
|
WHERE COALESCE(reporting_node, '') = $1
|
|
AND NOT (session_id = ANY($2))
|
|
`, reportingNode, sessionIDs); err != nil {
|
|
return fmt.Errorf("deleting missing sessions for node %s: %w", reportingNode, err)
|
|
}
|
|
}
|
|
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return fmt.Errorf("committing transaction: %w", err)
|
|
}
|
|
if changed && r.EventsHub != nil {
|
|
if err := r.EventsHub.PublishJSON(
|
|
ctx,
|
|
evt.ChannelSessions,
|
|
"sessions.replaced",
|
|
nil,
|
|
evt.PublishOptions{AdminOnly: true},
|
|
); err != nil {
|
|
log.Printf("reconciler: failed to publish session event for node %s: %v", reportingNode, err)
|
|
}
|
|
} else if changed && r.EventBus != nil {
|
|
if err := r.EventBus.Publish(ctx, cache.ChannelPlayback, cache.Event{
|
|
Type: cache.EventPlaybackSessionsChanged,
|
|
Payload: reportingNode,
|
|
}); err != nil {
|
|
log.Printf("reconciler: failed to publish playback invalidation event for node %s: %v", reportingNode, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func loadNodeSessionsSnapshot(ctx context.Context, tx pgx.Tx, reportingNode string) ([]SessionSync, error) {
|
|
rows, err := tx.Query(ctx, `
|
|
SELECT
|
|
session_id,
|
|
user_id,
|
|
COALESCE(profile_id, ''),
|
|
media_file_id,
|
|
COALESCE(requested_media_file_id, media_file_id, 0),
|
|
COALESCE(play_method, ''),
|
|
COALESCE(reporting_node, ''),
|
|
COALESCE(HOST(client_ip), ''),
|
|
COALESCE(client_name, ''),
|
|
COALESCE(client_version, ''),
|
|
COALESCE(client_build, ''),
|
|
COALESCE(client_channel, ''),
|
|
COALESCE(client_user_agent, ''),
|
|
COALESCE(audio_track_index, 0),
|
|
COALESCE(transcode_audio, FALSE),
|
|
COALESCE(stream_bitrate_kbps, 0),
|
|
COALESCE(transcode_node_url, ''),
|
|
COALESCE(target_resolution, ''),
|
|
COALESCE(target_video_codec, ''),
|
|
COALESCE(target_audio_codec, ''),
|
|
COALESCE(target_bitrate_kbps, 0),
|
|
COALESCE(transcode_hw_accel, ''),
|
|
started_at,
|
|
updated_at,
|
|
COALESCE(position_seconds, 0),
|
|
COALESCE(is_paused, FALSE),
|
|
COALESCE(has_websocket, FALSE),
|
|
COALESCE(compat_origin, FALSE)
|
|
FROM playback_sessions_sync
|
|
WHERE COALESCE(reporting_node, '') = $1
|
|
ORDER BY session_id
|
|
`, reportingNode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var sessions []SessionSync
|
|
for rows.Next() {
|
|
var s SessionSync
|
|
if err := rows.Scan(
|
|
&s.SessionID,
|
|
&s.UserID,
|
|
&s.ProfileID,
|
|
&s.MediaFileID,
|
|
&s.RequestedMediaFileID,
|
|
&s.PlayMethod,
|
|
&s.ReportingNode,
|
|
&s.ClientIP,
|
|
&s.ClientName,
|
|
&s.ClientVersion,
|
|
&s.ClientBuild,
|
|
&s.ClientChannel,
|
|
&s.ClientUserAgent,
|
|
&s.AudioTrackIndex,
|
|
&s.TranscodeAudio,
|
|
&s.StreamBitrateKbps,
|
|
&s.TranscodeNodeURL,
|
|
&s.TargetResolution,
|
|
&s.TargetVideoCodec,
|
|
&s.TargetAudioCodec,
|
|
&s.TargetBitrateKbps,
|
|
&s.TranscodeHWAccel,
|
|
&s.StartedAt,
|
|
&s.UpdatedAt,
|
|
&s.PositionSeconds,
|
|
&s.IsPaused,
|
|
&s.HasWebSocket,
|
|
&s.IsJellyfinCompat,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
sessions = append(sessions, s)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return sessions, nil
|
|
}
|
|
|
|
func normalizeSessionSyncs(reportingNode string, sessions []SessionSync) []SessionSync {
|
|
normalized := make([]SessionSync, len(sessions))
|
|
for i, s := range sessions {
|
|
cp := s
|
|
if strings.TrimSpace(cp.ReportingNode) == "" {
|
|
cp.ReportingNode = reportingNode
|
|
}
|
|
normalized[i] = cp
|
|
}
|
|
sort.Slice(normalized, func(i, j int) bool {
|
|
return normalized[i].SessionID < normalized[j].SessionID
|
|
})
|
|
return normalized
|
|
}
|
|
|
|
func sessionSnapshotsEqual(left, right []SessionSync) bool {
|
|
if len(left) != len(right) {
|
|
return false
|
|
}
|
|
for i := range left {
|
|
if left[i].SessionID != right[i].SessionID ||
|
|
left[i].UserID != right[i].UserID ||
|
|
left[i].ProfileID != right[i].ProfileID ||
|
|
left[i].MediaFileID != right[i].MediaFileID ||
|
|
left[i].RequestedMediaFileID != right[i].RequestedMediaFileID ||
|
|
left[i].PlayMethod != right[i].PlayMethod ||
|
|
left[i].ReportingNode != right[i].ReportingNode ||
|
|
left[i].ClientIP != right[i].ClientIP ||
|
|
left[i].ClientName != right[i].ClientName ||
|
|
left[i].ClientVersion != right[i].ClientVersion ||
|
|
left[i].ClientBuild != right[i].ClientBuild ||
|
|
left[i].ClientChannel != right[i].ClientChannel ||
|
|
left[i].ClientUserAgent != right[i].ClientUserAgent ||
|
|
left[i].AudioTrackIndex != right[i].AudioTrackIndex ||
|
|
left[i].TranscodeAudio != right[i].TranscodeAudio ||
|
|
left[i].StreamBitrateKbps != right[i].StreamBitrateKbps ||
|
|
left[i].TranscodeNodeURL != right[i].TranscodeNodeURL ||
|
|
left[i].TargetResolution != right[i].TargetResolution ||
|
|
left[i].TargetVideoCodec != right[i].TargetVideoCodec ||
|
|
left[i].TargetAudioCodec != right[i].TargetAudioCodec ||
|
|
left[i].TargetBitrateKbps != right[i].TargetBitrateKbps ||
|
|
left[i].TranscodeHWAccel != right[i].TranscodeHWAccel ||
|
|
!left[i].StartedAt.Equal(right[i].StartedAt) ||
|
|
!left[i].UpdatedAt.Equal(right[i].UpdatedAt) ||
|
|
normalizePositionSeconds(left[i].PositionSeconds) != normalizePositionSeconds(right[i].PositionSeconds) ||
|
|
left[i].IsPaused != right[i].IsPaused ||
|
|
left[i].HasWebSocket != right[i].HasWebSocket ||
|
|
left[i].IsJellyfinCompat != right[i].IsJellyfinCompat {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func normalizePositionSeconds(position float64) float64 {
|
|
if position < 0 {
|
|
return 0
|
|
}
|
|
return position
|
|
}
|
|
|
|
func nullableIP(ip string) any {
|
|
if strings.TrimSpace(ip) == "" {
|
|
return nil
|
|
}
|
|
return ip
|
|
}
|
|
|
|
func nullableInt(value int) any {
|
|
if value <= 0 {
|
|
return nil
|
|
}
|
|
return value
|
|
}
|
|
|
|
func nullableString(value string) any {
|
|
if strings.TrimSpace(value) == "" {
|
|
return nil
|
|
}
|
|
return value
|
|
}
|
|
|
|
// ReconcileAggregates upserts the aggregate counts for a single user into the
|
|
// user_aggregates table. The operation is idempotent.
|
|
func (r *Reconciler) ReconcileAggregates(ctx context.Context, userID int, totals AggregateData) error {
|
|
_, err := r.pool.Exec(ctx, `
|
|
INSERT INTO user_aggregates
|
|
(user_id, total_watched, favorites_count, watchlist_count, active_node, last_sync_at)
|
|
VALUES ($1, $2, $3, $4, $5, NOW())
|
|
ON CONFLICT (user_id) DO UPDATE SET
|
|
total_watched = EXCLUDED.total_watched,
|
|
favorites_count = EXCLUDED.favorites_count,
|
|
watchlist_count = EXCLUDED.watchlist_count,
|
|
active_node = EXCLUDED.active_node,
|
|
last_sync_at = NOW()
|
|
`, userID, totals.TotalWatched, totals.FavoritesCount, totals.WatchlistCount, totals.ActiveNode)
|
|
if err != nil {
|
|
return fmt.Errorf("upserting aggregates for user %d: %w", userID, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Start begins the background reconciliation loop. It runs until Stop is
|
|
// called. On each tick it syncs active playback sessions to PostgreSQL.
|
|
func (r *Reconciler) Start() {
|
|
go func() {
|
|
ticker := time.NewTicker(r.interval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-r.stop:
|
|
return
|
|
case <-ticker.C:
|
|
r.tick()
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
// tick runs one reconciliation cycle.
|
|
func (r *Reconciler) tick() {
|
|
if r.PreSync != nil {
|
|
r.PreSync()
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
if err := r.SyncNow(ctx); err != nil {
|
|
log.Printf("reconciler: session sync error: %v", err)
|
|
}
|
|
}
|
|
|
|
// SyncNow reconciles the current local session snapshot into the shared
|
|
// table. When nodeName is configured, an empty snapshot still clears any rows
|
|
// previously reported by that node.
|
|
//
|
|
// Syncs are coalesced: only one reconciliation runs at a time, and a call that
|
|
// arrives while one is in flight returns immediately after asking the running
|
|
// owner for one follow-up pass with a fresh snapshot. The follow-up capture
|
|
// happens after the caller's state change, so its effect is never lost, and
|
|
// snapshots always commit in capture order.
|
|
func (r *Reconciler) SyncNow(ctx context.Context) error {
|
|
if r.sessionProvider == nil {
|
|
return nil
|
|
}
|
|
|
|
r.syncMu.Lock()
|
|
if r.syncRunning {
|
|
// The in-flight sync may have captured a snapshot that predates this
|
|
// caller's state change; have the owner run one more pass.
|
|
r.syncPending = true
|
|
r.syncMu.Unlock()
|
|
return nil
|
|
}
|
|
r.syncRunning = true
|
|
// The fresh capture below supersedes any pass queued before ownership.
|
|
r.syncPending = false
|
|
r.syncMu.Unlock()
|
|
|
|
err := r.syncOnce(ctx)
|
|
for {
|
|
r.syncMu.Lock()
|
|
// Leave a queued pass for the next caller (the periodic tick at the
|
|
// latest) rather than burning it on an already-expired context.
|
|
if !r.syncPending || ctx.Err() != nil {
|
|
r.syncRunning = false
|
|
r.syncMu.Unlock()
|
|
return err
|
|
}
|
|
r.syncPending = false
|
|
r.syncMu.Unlock()
|
|
if passErr := r.syncOnce(ctx); err == nil {
|
|
err = passErr
|
|
}
|
|
}
|
|
}
|
|
|
|
// syncOnce captures one session snapshot and reconciles it.
|
|
func (r *Reconciler) syncOnce(ctx context.Context) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
sessions := r.sessionProvider()
|
|
if len(sessions) == 0 {
|
|
if r.nodeName == "" {
|
|
return nil
|
|
}
|
|
return r.ReconcileNodeSessions(ctx, r.nodeName, nil)
|
|
}
|
|
if r.nodeName != "" {
|
|
for i := range sessions {
|
|
if strings.TrimSpace(sessions[i].ReportingNode) == "" {
|
|
sessions[i].ReportingNode = r.nodeName
|
|
}
|
|
}
|
|
}
|
|
return r.ReconcileSessions(ctx, sessions)
|
|
}
|
|
|
|
// Stop signals the reconciliation loop to stop.
|
|
func (r *Reconciler) Stop() {
|
|
close(r.stop)
|
|
}
|