* feat(nodeconfig): harden config watcher for integrated-mode use - RequestReload(): non-blocking, coalescing reload nudge that runs on the poll goroutine, so concurrent requests can never swap a stale snapshot over a newer one (unlike ForceReload from request handlers) - Skip OnChange callbacks when the reloaded config is deep-equal to the previous one, so the 60s poll doesn't fire rebuild/log callbacks on no-op reloads - Add RedisURL to BootstrapOverrides; previously a reload clobbered an env-provided Redis URL in the live config - Split reload into fetchSettings/applySettings and add unit tests Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(config): hot-reload config watcher in integrated mode Start nodeconfig.Watcher in integrated/api mode (previously only proxy/ transcode worker modes hot-reloaded). Expose the live config to the API and jellycompat routers via func-typed LiveConfig/OnConfigChange fields with nil fallbacks to the startup snapshot, and wire the admin settings update hook to RequestReload so same-process changes apply immediately even without Redis. No consumer reads the live config yet — conversions land separately. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(admin): truthful restart-required banner for settings saves The settings UI showed 'restart required' after every save regardless of the key. The backend now classifies each key via a central registry (internal/config/restart_keys.go) and PUT /admin/settings/{key} reports restart_required per key; useSettingsForm only raises the banner when a saved key actually needs a restart (and keeps it raised until restart). The registry is conservative: every currently startup-frozen key is marked restart-required; subsequent hot-reload conversions shrink it. Settings read live from the settings repo (branding, overlays, markers, download.*, ...) default to no-restart. DownloadSettings/OverlaySettings drop their hardcoded restartRequired={false} special-casing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(logging): hot-reload server.log_level and server.log_quiet Share one slog.LevelVar across the handler chain and make logfilter.Handler's quiet-prefix list an atomic pointer shared with WithAttrs/WithGroup clones (New previously returned the inner handler unwrapped when the quiet list was empty, leaving nothing to update). The integrated-mode config watcher now applies both settings live; their keys leave the restart-required registry. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(auth): hot-reload access/refresh token expiries JWTService stores expiries as atomics with a SetExpiries hook; all three instances (main API, ABS compat, jellycompat) re-apply them on config reload. Applies to newly issued tokens; outstanding tokens keep their original expiry. The JWT secret stays fixed for the process lifetime. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(playback): read transcode config live at session start The playback and stream handlers pull ffmpeg path / hwaccel / transcode dir from the live config when starting a transcode or extracting subtitles, instead of values frozen at router construction. Each session snapshots the config once so its output dir and binary stay consistent. Also fixes a real bug: playback.hw_device was parsed into the config but never wired into the integrated-mode handler, so local transcodes always ran with an empty HWDevice while transcode nodes honored it. playback.transcode_dir leaves the restart-required registry (the handler is its only consumer); ffmpeg_path/hw_accel stay restart-required until scanner/chapterthumbs/audiobook consumers convert. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(jellycompat): read compat identity settings live per request System/Auth handlers take a config provider instead of the startup snapshot, so jellyfin_compat.public_url, .server_name, and .emulated_server_version apply without restart. server_id stays restart-required (generate-once, baked into the resource mapper), as do the session-store TTLs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(scanner,metadata,mdblist): hot-reload worker pools and API key scanner.workers, matcher.workers/batch_size, metadata.cache_images, and mdblist.api_key convert to atomic fields with setters wired to the config watcher. Worker counts apply on the next scan/match cycle (the loops read them per cycle); the MDBList key applies to the next request. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(ai): hot-reload AI connection, models, toggles, and quotas The shared llm.Client holds its config behind an atomic pointer (UpdateConfig; each request snapshots once), and the subtitle/metadata AI services gain UpdateConfig plus setters on the translator (batching) and Whisper transcriber (ffmpeg path, chunk seconds). The router derives their configs from shared helpers used both at construction and in OnConfigChange callbacks, re-evaluating the chat-only-gateway transcribe guard on each reload and warning only when it newly fires. Everything on the AI Services page now applies without restart except ai.max_concurrent_jobs (fixed-capacity dispatch semaphore). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(playback): wire transcode_enabled; remove dead playback/scanner knobs playback.transcode_enabled was parsed into the config but the resolver always received a hardcoded true — the admin toggle did nothing. It now reads the live config per playback start, so disabling transcodes applies without restart. Remove settings that were wired to nothing so 'save + restart' stops pretending: playback.allow_hevc_encoding (resolver field never assigned), playback.transcode_ahead_segments and playback.segment_duration (parsed, never consumed — segment duration is per-session from the client), scanner.file_removal_grace (DeleteMissing is never called). UI fields removed and the config struct fields pruned so they don't resurrect; YAML import still tolerates the legacy keys. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
229 lines
6.8 KiB
Go
229 lines
6.8 KiB
Go
package nodeconfig
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"reflect"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/Silo-Server/silo-server/internal/cache"
|
|
"github.com/Silo-Server/silo-server/internal/config"
|
|
"github.com/Silo-Server/silo-server/internal/secret"
|
|
)
|
|
|
|
// BootstrapOverrides holds values from env/CLI that must survive config
|
|
// reloads. These are set once at startup and re-applied after every
|
|
// LoadFromDB call.
|
|
type BootstrapOverrides struct {
|
|
Listen string // from PORT env
|
|
Mode string // from MODE env
|
|
DatabaseURL string // from DATABASE_URL env
|
|
JFListen string // from JF_PORT env
|
|
RedisURL string // from REDIS_URL env
|
|
}
|
|
|
|
// Watcher watches for configuration changes in the database and
|
|
// automatically reloads the Config when changes are detected.
|
|
type Watcher struct {
|
|
mu sync.RWMutex
|
|
cfg *config.Config
|
|
pool *pgxpool.Pool
|
|
cipher *secret.Cipher
|
|
eventBus cache.EventBus
|
|
bootstrap BootstrapOverrides
|
|
onChange []func(old, updated *config.Config)
|
|
reloadCh chan struct{} // buffered(1), event bus writes here
|
|
}
|
|
|
|
// NewWatcher creates a new config watcher. Call Start to begin watching. The
|
|
// cipher decrypts sensitive server_settings values (read here via raw SQL)
|
|
// before they reach config.LoadFromDB, so a hot reload never feeds ciphertext
|
|
// into the live config (which would, e.g., break JWT validation).
|
|
func NewWatcher(pool *pgxpool.Pool, cipher *secret.Cipher, eventBus cache.EventBus, bootstrap BootstrapOverrides) *Watcher {
|
|
return &Watcher{
|
|
pool: pool,
|
|
cipher: cipher,
|
|
eventBus: eventBus,
|
|
bootstrap: bootstrap,
|
|
reloadCh: make(chan struct{}, 1),
|
|
}
|
|
}
|
|
|
|
// Config returns the current config. Safe for concurrent use.
|
|
// Returns nil if Start has not been called.
|
|
func (w *Watcher) Config() *config.Config {
|
|
w.mu.RLock()
|
|
defer w.mu.RUnlock()
|
|
return w.cfg
|
|
}
|
|
|
|
// OnChange registers a callback invoked after a config swap whose new value
|
|
// differs from the old one. The callback receives the old and new config.
|
|
// Safe to call before or after Start; callbacks registered after Start only
|
|
// see reloads that happen after registration.
|
|
func (w *Watcher) OnChange(fn func(old, updated *config.Config)) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
w.onChange = append(w.onChange, fn)
|
|
}
|
|
|
|
// Start performs the initial config load from the database, subscribes to
|
|
// EventSettingsChanged on the admin channel, and starts the background
|
|
// poll goroutine. Returns an error if the initial load fails.
|
|
func (w *Watcher) Start(ctx context.Context) error {
|
|
if err := w.reload(ctx); err != nil {
|
|
return fmt.Errorf("initial config load: %w", err)
|
|
}
|
|
|
|
// Subscribe to settings change events for immediate reload.
|
|
if err := w.eventBus.Subscribe(ctx, cache.ChannelAdmin, func(event cache.Event) {
|
|
if event.Type == cache.EventSettingsChanged {
|
|
select {
|
|
case w.reloadCh <- struct{}{}:
|
|
default:
|
|
// Already pending — coalesce.
|
|
}
|
|
}
|
|
}); err != nil {
|
|
slog.Warn("config watcher: subscribe to admin channel failed, using poll-only mode", "error", err)
|
|
}
|
|
|
|
go w.poll(ctx)
|
|
return nil
|
|
}
|
|
|
|
// ForceReload triggers an immediate config reload from the database.
|
|
func (w *Watcher) ForceReload(ctx context.Context) error {
|
|
return w.reload(ctx)
|
|
}
|
|
|
|
// RequestReload asks the poll goroutine to reload soon. Non-blocking and
|
|
// coalescing — safe to call from request handlers. Unlike ForceReload, the
|
|
// reload runs on the poll goroutine, so concurrent requests can never swap a
|
|
// stale snapshot over a newer one.
|
|
func (w *Watcher) RequestReload() {
|
|
select {
|
|
case w.reloadCh <- struct{}{}:
|
|
default:
|
|
// Already pending — coalesce.
|
|
}
|
|
}
|
|
|
|
// SetConfigForTest sets the config directly without loading from DB.
|
|
// This is intended for use in tests only.
|
|
func (w *Watcher) SetConfigForTest(cfg *config.Config) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
w.cfg = cfg
|
|
}
|
|
|
|
// reload fetches all settings from the database, builds a new Config,
|
|
// applies bootstrap overrides, and atomically swaps the config pointer.
|
|
func (w *Watcher) reload(ctx context.Context) error {
|
|
m, err := w.fetchSettings(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return w.applySettings(m)
|
|
}
|
|
|
|
// fetchSettings reads all server_settings rows and decrypts sensitive values.
|
|
func (w *Watcher) fetchSettings(ctx context.Context) (map[string]string, error) {
|
|
rows, err := w.pool.Query(ctx, "SELECT key, value FROM server_settings")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query server_settings: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
m := make(map[string]string)
|
|
for rows.Next() {
|
|
var k, v string
|
|
if err := rows.Scan(&k, &v); err != nil {
|
|
return nil, fmt.Errorf("scan server_settings row: %w", err)
|
|
}
|
|
// Decrypt sensitive keys (read-path contract: legacy plaintext passes
|
|
// through, enc:v1: values decrypt, corrupt ciphertext errors) so
|
|
// LoadFromDB always sees plaintext.
|
|
decrypted, derr := w.cipher.DecryptIfEncrypted(v, secret.SettingsAAD(k))
|
|
if derr != nil {
|
|
return nil, fmt.Errorf("decrypt server_settings %q: %w", k, derr)
|
|
}
|
|
m[k] = decrypted
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterate server_settings: %w", err)
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// applySettings builds a Config from a plaintext settings map, re-applies
|
|
// bootstrap overrides, swaps the config pointer, and notifies OnChange
|
|
// callbacks when the config actually changed.
|
|
func (w *Watcher) applySettings(m map[string]string) error {
|
|
newCfg, err := config.LoadFromDB(m)
|
|
if err != nil {
|
|
return fmt.Errorf("parse config: %w", err)
|
|
}
|
|
|
|
// Re-apply bootstrap overrides — these are immutable for the process lifetime.
|
|
if w.bootstrap.Listen != "" {
|
|
newCfg.Server.Listen = w.bootstrap.Listen
|
|
}
|
|
if w.bootstrap.Mode != "" {
|
|
newCfg.Server.Mode = w.bootstrap.Mode
|
|
}
|
|
if w.bootstrap.DatabaseURL != "" {
|
|
newCfg.Database.URL = w.bootstrap.DatabaseURL
|
|
}
|
|
if w.bootstrap.JFListen != "" {
|
|
newCfg.JellyfinCompat.Listen = w.bootstrap.JFListen
|
|
}
|
|
if w.bootstrap.RedisURL != "" {
|
|
newCfg.Redis.URL = w.bootstrap.RedisURL
|
|
}
|
|
|
|
w.mu.Lock()
|
|
old := w.cfg
|
|
w.cfg = newCfg
|
|
callbacks := make([]func(old, updated *config.Config), len(w.onChange))
|
|
copy(callbacks, w.onChange)
|
|
w.mu.Unlock()
|
|
|
|
// The poll path reloads every 60s regardless of whether anything changed;
|
|
// don't fire callbacks (which may rebuild clients or log) on no-op swaps.
|
|
if old != nil && reflect.DeepEqual(*old, *newCfg) {
|
|
return nil
|
|
}
|
|
|
|
for _, fn := range callbacks {
|
|
fn(old, newCfg)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// poll runs the background loop that reloads config on timer or event.
|
|
func (w *Watcher) poll(ctx context.Context) {
|
|
ticker := time.NewTicker(60 * time.Second)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
if err := w.reload(ctx); err != nil {
|
|
slog.Warn("config poll reload failed", "error", err)
|
|
}
|
|
case <-w.reloadCh:
|
|
if err := w.reload(ctx); err != nil {
|
|
slog.Warn("config event reload failed", "error", err)
|
|
}
|
|
}
|
|
}
|
|
}
|