refactor(server): bundle worker pipeline into PipelineState
Group the four worker handles (analyze/transcribe x busy/tx) into a new PipelineState struct with a FromRef<AppState> impl. Convert heal_orphans_if_idle from a free function with 6 parameters into a method on PipelineState. The two web handlers (handle_my_cases, handle_case_detail) now take one State<PipelineState> instead of four separate State extractors. The original FromRef impls for AnalyzeBusy/Sender and TranscribeBusy/ Sender remain intact so handlers that need only one of them keep their narrower signature.
This commit is contained in:
@@ -9,14 +9,14 @@ use tracing::{info, warn};
|
||||
|
||||
use time::{format_description::well_known::Rfc3339, Date, OffsetDateTime};
|
||||
|
||||
use crate::analyze::{recovery as analyze_recovery, AnalyzeSender, ANALYSIS_INPUT_FILE, DOCUMENT_FILE};
|
||||
use crate::analyze::{recovery as analyze_recovery, ANALYSIS_INPUT_FILE, DOCUMENT_FILE};
|
||||
use crate::auth::AuthenticatedWebUser;
|
||||
use crate::case_id::{CaseId, CaseIdPath};
|
||||
use crate::config::Config;
|
||||
use crate::error::AppError;
|
||||
use crate::routes::web::{scan_recordings, RecordingView};
|
||||
use crate::transcribe::{recovery as transcribe_recovery, TranscribeSender};
|
||||
use crate::{AnalyzeBusy, TranscribeBusy};
|
||||
use crate::transcribe::recovery as transcribe_recovery;
|
||||
use crate::PipelineState;
|
||||
|
||||
struct UserCaseView {
|
||||
case_id: String,
|
||||
@@ -201,42 +201,27 @@ pub(crate) async fn any_document_exists(case_dir: &Path) -> bool {
|
||||
/// Self-heal: if a worker is idle but orphans exist on disk for this user,
|
||||
/// re-enqueue them. Page-load is the trigger; no cron, no periodic task.
|
||||
/// Runs for both pipelines so a page-load on either view cleans both.
|
||||
async fn heal_orphans_if_idle(
|
||||
user_root: &Path,
|
||||
slug: &str,
|
||||
analyze_busy: &AnalyzeBusy,
|
||||
analyze_tx: &AnalyzeSender,
|
||||
transcribe_busy: &TranscribeBusy,
|
||||
transcribe_tx: &TranscribeSender,
|
||||
) {
|
||||
if !analyze_busy.0.load(Ordering::Acquire) {
|
||||
analyze_recovery::enqueue_pending_for_user(user_root, analyze_tx).await;
|
||||
}
|
||||
if !transcribe_busy.0.load(Ordering::Acquire) {
|
||||
transcribe_recovery::enqueue_pending_for_user(user_root, slug, transcribe_tx).await;
|
||||
impl PipelineState {
|
||||
pub async fn heal_orphans_if_idle(&self, user_root: &Path, slug: &str) {
|
||||
if !self.analyze_busy.0.load(Ordering::Acquire) {
|
||||
analyze_recovery::enqueue_pending_for_user(user_root, &self.analyze_tx).await;
|
||||
}
|
||||
if !self.transcribe_busy.0.load(Ordering::Acquire) {
|
||||
transcribe_recovery::enqueue_pending_for_user(user_root, slug, &self.transcribe_tx)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_my_cases(
|
||||
user: AuthenticatedWebUser,
|
||||
State(config): State<Arc<Config>>,
|
||||
State(analyze_busy): State<AnalyzeBusy>,
|
||||
State(analyze_tx): State<AnalyzeSender>,
|
||||
State(transcribe_busy): State<TranscribeBusy>,
|
||||
State(transcribe_tx): State<TranscribeSender>,
|
||||
State(pipeline): State<PipelineState>,
|
||||
) -> Result<Html<String>, AppError> {
|
||||
let user_root = config.data_path.join(&user.slug);
|
||||
heal_orphans_if_idle(
|
||||
&user_root,
|
||||
&user.slug,
|
||||
&analyze_busy,
|
||||
&analyze_tx,
|
||||
&transcribe_busy,
|
||||
&transcribe_tx,
|
||||
)
|
||||
.await;
|
||||
pipeline.heal_orphans_if_idle(&user_root, &user.slug).await;
|
||||
|
||||
let busy = analyze_busy.0.load(Ordering::Acquire);
|
||||
let busy = pipeline.analyze_busy.0.load(Ordering::Acquire);
|
||||
let cases = scan_user_cases(&config, &user.slug, busy).await;
|
||||
let total = cases.len();
|
||||
let groups = group_by_utc_date(cases);
|
||||
@@ -260,23 +245,12 @@ pub async fn handle_my_cases(
|
||||
pub async fn handle_case_detail(
|
||||
user: AuthenticatedWebUser,
|
||||
State(config): State<Arc<Config>>,
|
||||
State(analyze_busy): State<AnalyzeBusy>,
|
||||
State(analyze_tx): State<AnalyzeSender>,
|
||||
State(transcribe_busy): State<TranscribeBusy>,
|
||||
State(transcribe_tx): State<TranscribeSender>,
|
||||
State(pipeline): State<PipelineState>,
|
||||
CaseIdPath(case_id): CaseIdPath,
|
||||
) -> Result<Html<String>, AppError> {
|
||||
// IDOR guard: case_dir must live under the session's user_slug.
|
||||
let user_root = config.data_path.join(&user.slug);
|
||||
heal_orphans_if_idle(
|
||||
&user_root,
|
||||
&user.slug,
|
||||
&analyze_busy,
|
||||
&analyze_tx,
|
||||
&transcribe_busy,
|
||||
&transcribe_tx,
|
||||
)
|
||||
.await;
|
||||
pipeline.heal_orphans_if_idle(&user_root, &user.slug).await;
|
||||
|
||||
let case_dir = locate_case_or_404(
|
||||
&user_root,
|
||||
@@ -295,8 +269,8 @@ pub async fn handle_case_detail(
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty());
|
||||
|
||||
let a_busy = analyze_busy.0.load(Ordering::Acquire);
|
||||
let t_busy = transcribe_busy.0.load(Ordering::Acquire);
|
||||
let a_busy = pipeline.analyze_busy.0.load(Ordering::Acquire);
|
||||
let t_busy = pipeline.transcribe_busy.0.load(Ordering::Acquire);
|
||||
let flags = compute_flags(&case_dir, &recordings, config.llm_configured(), a_busy).await;
|
||||
let case_id_short = case_id_str.chars().take(8).collect();
|
||||
let is_admin = user.is_admin();
|
||||
|
||||
Reference in New Issue
Block a user