fix(arr): wire the command timeout to the settings row

This commit is contained in:
Miguel Palhas
2026-08-25 03:15:33 +01:00
parent 47c7ef6682
commit e338a76c66
4 changed files with 180 additions and 18 deletions
+79 -15
View File
@@ -18,7 +18,7 @@ mod tv_grab;
mod web;
use std::process::ExitCode;
use std::sync::Arc;
use std::sync::{atomic::AtomicU64, Arc};
use arr_api::{AppState, Upstreams};
use arr_compat::CompatState;
@@ -117,6 +117,7 @@ fn api_state(
config: &Config,
database: &Db,
jellyfin: arr_api::jellyfin::JellyfinClient,
translators: &Translators,
) -> Result<AppState, Error> {
let mut upstreams =
Upstreams::new(config.prowlarr_url.clone(), config.transmission_url.clone())
@@ -126,16 +127,20 @@ fn api_state(
upstreams = upstreams.with_tmdb_url(tmdb_url);
}
Ok(AppState::new(upstreams)?
let mut state = AppState::new(upstreams)?
.with_database(database.clone())
.with_subtitle_providers(subtitle_providers(
config.opensubtitles_api_key.clone(),
config.opensubtitles_username.clone(),
config.opensubtitles_password.clone(),
))
.with_translation_backends(translation_backends(config))
.with_translation_backends(translators.backends.clone())
.with_jellyfin(jellyfin)
.with_syncer(arr_subs::Syncer::new().with_binary(config.alass_path.clone())))
.with_syncer(arr_subs::Syncer::new().with_binary(config.alass_path.clone()));
if let Some(timeout) = &translators.command_timeout {
state = state.with_command_timeout(Arc::clone(timeout));
}
Ok(state)
}
async fn run() -> Result<(), Error> {
@@ -155,8 +160,19 @@ async fn run() -> Result<(), Error> {
};
let notifier = Notifier::new(config.ntfy_url.clone())?;
let api_jellyfin = jellyfin_client(&config)?;
let (reconcile, manual_grab, manual_tv) =
reconcile_loop(&database, &config, &transmission, tmdb.as_ref(), &notifier)?;
// Built once and shared: the API's translate handler and the reconcile
// lane must see the same backends, or the command translator's live
// timeout cell (#219) would fork.
let translators = translation_backends(&config);
seed_command_timeout(&database, &translators).await?;
let (reconcile, manual_grab, manual_tv) = reconcile_loop(
&database,
&config,
&transmission,
tmdb.as_ref(),
&notifier,
&translators,
)?;
// Issue #176: the on-demand half of the metadata lane needs its own
// handle — the sweep's `SeriesRefreshAction` is owned by `ReconcileLoop`,
// and the compat shim takes the other clone below.
@@ -169,7 +185,7 @@ async fn run() -> Result<(), Error> {
compat = compat.with_tmdb(tmdb);
}
let state = api_state(&config, &database, api_jellyfin)?;
let state = api_state(&config, &database, api_jellyfin, &translators)?;
let app = arr_api::router(state.clone())
.merge(arr_compat::router(compat))
@@ -247,6 +263,30 @@ async fn run() -> Result<(), Error> {
}
}
/// Seed the command translator's live timeout from the settings row, so a
/// restart does not fall back to the compiled-in default until the next
/// settings edit (issue #219). The row is guaranteed to exist and to carry a
/// positive value — migration `0025` seeds it and the column CHECK enforces
/// it.
async fn seed_command_timeout(database: &Db, translators: &Translators) -> Result<(), Error> {
let Some(cell) = &translators.command_timeout else {
return Ok(());
};
let seconds: i64 = sqlx::query_scalar!(
r#"SELECT remote_command_timeout_seconds AS "remote_command_timeout_seconds!: i64"
FROM subtitle_settings WHERE id = 1"#
)
.fetch_one(database.pool())
.await?;
cell.store(
u64::try_from(seconds)
.unwrap_or(u64::MAX)
.saturating_mul(1_000),
std::sync::atomic::Ordering::Relaxed,
);
Ok(())
}
/// Wire the reconcile lanes (DESIGN.md §8). Grab and RSS both need a
/// Prowlarr key and grab needs TMDB as well; a lane whose upstream is not
/// configured stays unregistered rather than failing every tick.
@@ -262,6 +302,7 @@ fn reconcile_loop(
transmission: &arr_dl::TransmissionClient,
tmdb: Option<&Arc<TmdbClient>>,
notifier: &Notifier,
translators: &Translators,
) -> Result<(ReconcileLoop, Option<GrabAction>, Option<TvGrabAction>), Error> {
let reconcile = ReconcileLoop::new(database.clone());
let seeding = SeedingRules::new(
@@ -334,7 +375,10 @@ fn reconcile_loop(
);
// §15: subtitle gaps are reconciled from the same rows the API writes.
reconcile = reconcile.register(Tick::Reconcile, subtitle_action(config, notifier)?);
reconcile = reconcile.register(
Tick::Reconcile,
subtitle_action(config, notifier, translators)?,
);
// §9.5 *needs a decision* and *broken* both go to the operator alone;
// without a topic configured there is nowhere to send them.
@@ -485,14 +529,18 @@ fn jellyfin_client(config: &Config) -> Result<arr_api::jellyfin::JellyfinClient,
/// — the compiled-in backends (#191#193) still need their bootstrap wiring
/// (model names, base URLs) — so the translate step records "not compiled"
/// and backs off rather than failing obscurely.
fn subtitle_action(config: &Config, notifier: &Notifier) -> Result<SubtitleAction, Error> {
fn subtitle_action(
config: &Config,
notifier: &Notifier,
translators: &Translators,
) -> Result<SubtitleAction, Error> {
let action = SubtitleAction::new(
subtitle_providers(
config.opensubtitles_api_key.clone(),
config.opensubtitles_username.clone(),
config.opensubtitles_password.clone(),
),
translation_backends(config),
translators.backends.clone(),
arr_subs::Syncer::new().with_binary(config.alass_path.clone()),
arr_probe::Extractor::new().with_binary(config.ffmpeg_path.clone()),
jellyfin_client(config)?,
@@ -538,14 +586,23 @@ fn subtitle_providers(
providers
}
/// The translation backends this deployment can offer (DESIGN.md §15,
/// issue #216).
/// The translation backends this deployment can offer, built once at startup
/// and shared by the API and the reconcile lane (DESIGN.md §15, issue #216).
///
/// Which cargo features this binary was built with decides what could ever
/// be here (`compiled_engines`); credentials decide what actually is, same
/// split `subtitle_providers` makes for search. Which one of these a
/// translation *uses* is the `translation_engine` database setting, read per
/// request (#198) — this only decides which ids exist to be picked.
///
/// When the remote-command backend is one of them, its live timeout cell
/// rides along (#219): the API writes it on every settings edit, so the row
/// reaches the running process without a restart.
struct Translators {
backends: Vec<std::sync::Arc<dyn arr_subs::Backend>>,
command_timeout: Option<Arc<AtomicU64>>,
}
#[cfg_attr(
not(any(
feature = "translate-openai",
@@ -555,8 +612,9 @@ fn subtitle_providers(
)),
allow(unused_variables, unused_mut)
)]
fn translation_backends(config: &Config) -> Vec<std::sync::Arc<dyn arr_subs::Backend>> {
fn translation_backends(config: &Config) -> Translators {
let mut backends: Vec<std::sync::Arc<dyn arr_subs::Backend>> = Vec::new();
let mut command_timeout: Option<Arc<AtomicU64>> = None;
#[cfg(feature = "translate-openai")]
{
@@ -619,7 +677,10 @@ fn translation_backends(config: &Config) -> Vec<std::sync::Arc<dyn arr_subs::Bac
timeout: arr_subs::COMMAND_DEFAULT_TIMEOUT,
};
match arr_subs::Command::new(command_config) {
Ok(backend) => backends.push(std::sync::Arc::new(backend)),
Ok(backend) => {
command_timeout = Some(backend.timeout_cell());
backends.push(std::sync::Arc::new(backend));
}
Err(error) => tracing::warn!(%error, "remote-command translator not available"),
}
} else {
@@ -627,5 +688,8 @@ fn translation_backends(config: &Config) -> Vec<std::sync::Arc<dyn arr_subs::Bac
}
}
backends
Translators {
backends,
command_timeout,
}
}