From bc0227042b10d494493d32f5a2d65099f56be5e7 Mon Sep 17 00:00:00 2001 From: feco Date: Thu, 19 Nov 2026 06:40:09 +0000 Subject: [PATCH] Worker: separate quick lane for summaries and answers Summaries and questions get their own slots (QUICK_CONCURRENCY, default 2), so they never wait behind detailed analyses (ANALYSIS_CONCURRENCY, default 4). A free slow slot also takes quick work; a lane of size 0 is off, so the lanes can run as separate deployments. Documented that several workers can share the queue. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL --- services/worker/README.md | 8 +- services/worker/src/analysis/mod.rs | 132 +++++++++++++++++++--------- services/worker/src/config.rs | 5 +- services/worker/src/main.rs | 12 ++- services/worker/src/tests.rs | 46 ++++++++-- 5 files changed, 150 insertions(+), 53 deletions(-) diff --git a/services/worker/README.md b/services/worker/README.md index e49cc40..c270144 100644 --- a/services/worker/README.md +++ b/services/worker/README.md @@ -21,6 +21,11 @@ Háttérmunkák a közös PostgreSQL-adatbázis fölött: e-mail-küldés, szöv **Szöveges elemzések** (`analyses`, `pending` → `running` → `done`/`failed`) - A backend létrehozza a `pending` sort, és levon egy elemzési egységet. Egy trigger (`NOTIFY analyses`) felébreszti a workert; egyszerre `ANALYSIS_CONCURRENCY` elemzés készül. +- **Két sáv**, saját helyekkel, hogy egy rövid szöveg ne várjon a hosszúak mögött: + - *gyors sáv* (`QUICK_CONCURRENCY`, alapból 2): előbb a kérdések, aztán a rövid összefoglalók; + - *lassú sáv* (`ANALYSIS_CONCURRENCY`, alapból 4): a részletes elemzések és az előrejelzések. Ha nincs ilyen, a szabad hely gyors munkát vesz fel, végül az admin próbafuttatásait. + - A 0 kikapcsolja a sávot, így a két sáv külön telepítésként is futhat (például Kubernetesen: egy gyors és egy lassú deployment). +- **Több worker** is dolgozhat ugyanabból a sorból: a sorokat `FOR UPDATE SKIP LOCKED` foglalja, egy munkát csak egy worker kap meg. Leálláskor mindegyik csak a saját félbehagyott munkáit adja vissza a sorba, az elakadt (15 percnél régebben futó) munkákat bármelyik visszateszi. Az e-mail-küldés és a takarítás is biztonságos több példányban. - A prompt a tárolt képletből készül (`src/analysis/compact.rs`). Ez egy tömör szöveg, amely csak a pozíciókat tartalmazza: jegyek, fokok, házak, fényszögek, elemek. **Név, születési dátum, idő, hely és koordináta nem megy ki.** Ismeretlen születési időnél nincsenek házak, Aszcendens és MC. - **5 fokos szabály:** ha egy pont legfeljebb 5°-kal (`CUSP_ORB`) a következő ház csúcsa előtt áll, a szöveg már abban a házban adja meg, és odaírja, hogy formálisan az előzőben van („house 11 (on the cusp: 2°00' before it, formally house 10)”). Így olvassák az asztrológusok. A számított ház, a képlet rajza és a táblázatok nem változnak. Ugyanez érvényes az összevetés házátfedéseire, a szolár Aszcendensre, az év urára és a fogyatkozásokra. - A modell OpenRouteren keresztül válaszol, azzal a modellel, amely a sor `model` mezőjében áll. A válasznak kötött JSON-sémát kell követnie (`crates/analysis-prompt`, `PROMPT_VERSION = v3`; ha a sor `prompt_version`-je `v3-r`, a system prompt az admin által szerkesztett `prompt_revisions` sorból jön): @@ -72,7 +77,8 @@ Ugyanazt a radix-elemzést több modellel is megírja, véletlenszerű betűkkel | `OPENROUTER_API_KEY` | – | OpenRouter-kulcs (compose-ban a `.env.secrets` fájlból); nélküle az elemzések várnak | | `OPENROUTER_BASE_URL` | `https://openrouter.ai/api/v1` | | | `PUBLIC_BASE_URL` | – | `HTTP-Referer` fejléc az OpenRouter felé | -| `ANALYSIS_CONCURRENCY` | `4` | egyszerre írt elemzések | +| `ANALYSIS_CONCURRENCY` | `4` | lassú sáv: egyszerre írt részletes elemzések és előrejelzések (0: kikapcsolva) | +| `QUICK_CONCURRENCY` | `2` | gyors sáv: egyszerre írt rövid összefoglalók és válaszok (0: kikapcsolva) | | `MAX_ANALYSIS_ATTEMPTS` | `3` | ennyi próbálkozás után sikertelen az elemzés (az egység visszajár) | | `BIND_ADDR`, `LOG_FORMAT`, `RUST_LOG` | `0.0.0.0:8082`, `text`, `info` | | diff --git a/services/worker/src/analysis/mod.rs b/services/worker/src/analysis/mod.rs index 170e1ff..c682f58 100644 --- a/services/worker/src/analysis/mod.rs +++ b/services/worker/src/analysis/mod.rs @@ -9,6 +9,13 @@ //! Admin test runs of an edited prompt (`prompt_previews`) go through the //! same loop when no analysis is due: one attempt, no credits, no retries. //! +//! Two lanes, each with its own slots, so a short text never waits behind +//! the long ones: the quick lane writes summaries and answers questions, the +//! slow lane writes the detailed analyses and forecasts (and the test runs). +//! A free slow slot also takes quick work. A lane of size 0 is off, so the +//! lanes can run as separate deployments; several workers may share the +//! queue, rows are claimed with `FOR UPDATE SKIP LOCKED`. +//! //! Fallback: when the model chosen on the admin page fails twice (or once with //! a permanent error), the default model (`fallback_model`) takes over with a //! fresh set of attempts; only when that fails too is the unit refunded. @@ -29,6 +36,7 @@ use uuid::Uuid; use self::openrouter::{Completion, LlmError, OpenRouter}; use self::prompt::{Kind, PROMPT_VERSION, Prompt}; +use self::questions::QuestionJob; /// A `running` analysis older than this was left behind by a stopped worker. const STUCK_AFTER_MINUTES: i32 = 15; @@ -101,11 +109,36 @@ pub struct PreviewJob { pub system: String, } +/// Which analyses a claim may take. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Lane { + /// The short summary of a chart (questions are claimed separately). + Quick, + /// The detailed analyses and the yearly forecast. + Slow, +} + +impl Lane { + fn kinds(self) -> &'static [&'static str] { + match self { + Lane::Quick => &["summary"], + Lane::Slow => &["natal", "synastry", "forecast"], + } + } +} + +/// Slots per lane; 0 turns a lane off. +#[derive(Debug, Clone, Copy)] +pub struct Lanes { + pub quick: usize, + pub slow: usize, +} + pub struct Analyzer { pub db: PgPool, pub llm: OpenRouter, pub max_attempts: i32, - pub concurrency: usize, + pub lanes: Lanes, /// Analyses and previews being written right now (released on shutdown). running: Mutex>, } @@ -117,19 +150,21 @@ pub fn backoff(attempts: i32) -> Duration { } impl Analyzer { - pub fn new(db: PgPool, llm: OpenRouter, max_attempts: i32, concurrency: usize) -> Self { - Self { db, llm, max_attempts, concurrency: concurrency.max(1), running: Mutex::default() } + pub fn new(db: PgPool, llm: OpenRouter, max_attempts: i32, lanes: Lanes) -> Self { + Self { db, llm, max_attempts, lanes, running: Mutex::default() } } - /// Takes the next due analysis, if any. - pub async fn claim(&self) -> Result, sqlx::Error> { + /// Takes the next due analysis of the lane, if any. + pub async fn claim(&self, lane: Lane) -> Result, sqlx::Error> { let job: Option = sqlx::query_as( "UPDATE analyses SET status = 'running', started_at = now(), attempts = attempts + 1 \ WHERE id = (SELECT id FROM analyses WHERE status = 'pending' AND next_attempt_at <= now() \ + AND kind = ANY($1) \ ORDER BY next_attempt_at LIMIT 1 FOR UPDATE SKIP LOCKED) \ RETURNING id, kind, chart_id, synastry_id, forecast_id, language, model, prompt_version, attempts, \ fallback_model, on_fallback, started_at", ) + .bind(lane.kinds()) .fetch_optional(&self.db) .await?; if let Some(job) = &job { @@ -530,8 +565,46 @@ impl Analyzer { } } +/// Work a free slot can take, in order of preference. +pub(crate) enum Work { + Analysis(Job), + Question(QuestionJob), + Preview(PreviewJob), +} + +impl Analyzer { + /// Quick work: a question first (someone is waiting for a short answer), + /// then a summary. + pub(crate) async fn claim_quick(&self) -> Result, sqlx::Error> { + if let Some(job) = self.claim_question().await? { + return Ok(Some(Work::Question(job))); + } + Ok(self.claim(Lane::Quick).await?.map(Work::Analysis)) + } + + /// Slow work: a detailed analysis, else quick work, else a test run. + pub(crate) async fn claim_slow(&self) -> Result, sqlx::Error> { + if let Some(job) = self.claim(Lane::Slow).await? { + return Ok(Some(Work::Analysis(job))); + } + if let Some(work) = self.claim_quick().await? { + return Ok(Some(work)); + } + Ok(self.claim_preview().await?.map(Work::Preview)) + } + + async fn work(&self, work: Work) { + match work { + Work::Analysis(job) => self.handle(job).await, + Work::Question(job) => self.handle_question(job).await, + Work::Preview(job) => self.handle_preview(job).await, + } + } +} + pub async fn run(analyzer: Arc, poll: Duration, stop: watch::Receiver) { - let slots = Arc::new(Semaphore::new(analyzer.concurrency)); + let quick = Arc::new(Semaphore::new(analyzer.lanes.quick)); + let slow = Arc::new(Semaphore::new(analyzer.lanes.slow)); let mut tasks = tokio::task::JoinSet::new(); let mut listener = crate::listen(&analyzer.db, "analyses").await; let mut last_requeue: Option = None; @@ -547,50 +620,27 @@ pub async fn run(analyzer: Arc, poll: Duration, stop: watch::Receiver< Err(e) => tracing::error!(error = %e, "requeueing analyses"), } } - // Fill the free slots. - while let Ok(permit) = slots.clone().try_acquire_owned() { - // Questions first: someone is waiting for a short answer. - match analyzer.claim_question().await { - Ok(Some(job)) => { - let analyzer = analyzer.clone(); - tasks.spawn(async move { - analyzer.handle_question(job).await; - drop(permit); - }); - continue; - } - Ok(None) => {} - Err(e) => { - tracing::error!(error = %e, "claiming a question"); - break; - } - } - match analyzer.claim().await { - Ok(Some(job)) => { - let analyzer = analyzer.clone(); - tasks.spawn(async move { - analyzer.handle(job).await; - drop(permit); - }); - } - // No analysis is due: a prompt test run, if one waits. - Ok(None) => match analyzer.claim_preview().await { - Ok(Some(job)) => { + // Fill the free slots: the quick lane first, then the slow one. + for lane in [Lane::Quick, Lane::Slow] { + let slots = if lane == Lane::Quick { &quick } else { &slow }; + while let Ok(permit) = slots.clone().try_acquire_owned() { + let claimed = match lane { + Lane::Quick => analyzer.claim_quick().await, + Lane::Slow => analyzer.claim_slow().await, + }; + match claimed { + Ok(Some(work)) => { let analyzer = analyzer.clone(); tasks.spawn(async move { - analyzer.handle_preview(job).await; + analyzer.work(work).await; drop(permit); }); } Ok(None) => break, Err(e) => { - tracing::error!(error = %e, "claiming a prompt preview"); + tracing::error!(error = %e, ?lane, "claiming work"); break; } - }, - Err(e) => { - tracing::error!(error = %e, "claiming an analysis"); - break; } } } diff --git a/services/worker/src/config.rs b/services/worker/src/config.rs index 3949841..5f98d8a 100644 --- a/services/worker/src/config.rs +++ b/services/worker/src/config.rs @@ -24,8 +24,10 @@ pub struct Config { pub openrouter_base_url: String, /// Frontend address, sent to OpenRouter as `HTTP-Referer`. pub public_base_url: Option, - /// Analyses written at the same time. + /// Detailed analyses and forecasts written at the same time (0: none). pub analysis_concurrency: usize, + /// Summaries and answers written at the same time (0: none). + pub quick_concurrency: usize, /// After this many failed attempts an analysis fails and is refunded. pub max_analysis_attempts: i32, } @@ -67,6 +69,7 @@ impl Config { .unwrap_or_else(|| crate::analysis::openrouter::DEFAULT_BASE_URL.into()), public_base_url: var("PUBLIC_BASE_URL"), analysis_concurrency: parsed("ANALYSIS_CONCURRENCY", 4)?, + quick_concurrency: parsed("QUICK_CONCURRENCY", 2)?, max_analysis_attempts: parsed("MAX_ANALYSIS_ATTEMPTS", 3)?, }) } diff --git a/services/worker/src/main.rs b/services/worker/src/main.rs index 2f07250..b86f050 100644 --- a/services/worker/src/main.rs +++ b/services/worker/src/main.rs @@ -104,10 +104,14 @@ async fn main() -> anyhow::Result<()> { Some(key) => { let llm = analysis::openrouter::OpenRouter::new(&config.openrouter_base_url, key, config.public_base_url.clone()); - let analyzer = - analysis::Analyzer::new(pool.clone(), llm, config.max_analysis_attempts, config.analysis_concurrency); - tracing::info!(concurrency = analyzer.concurrency, "analyses enabled"); - jobs.spawn(analysis::run(Arc::new(analyzer), config.poll_interval, stop.clone())); + let lanes = analysis::Lanes { quick: config.quick_concurrency, slow: config.analysis_concurrency }; + if lanes.quick + lanes.slow == 0 { + tracing::warn!("QUICK_CONCURRENCY and ANALYSIS_CONCURRENCY are both 0: this worker writes no analyses"); + } else { + let analyzer = analysis::Analyzer::new(pool.clone(), llm, config.max_analysis_attempts, lanes); + tracing::info!(quick = lanes.quick, slow = lanes.slow, "analyses enabled"); + jobs.spawn(analysis::run(Arc::new(analyzer), config.poll_interval, stop.clone())); + } } None => tracing::warn!("OPENROUTER_API_KEY is not set: analyses stay pending"), } diff --git a/services/worker/src/tests.rs b/services/worker/src/tests.rs index 2079009..a53aecc 100644 --- a/services/worker/src/tests.rs +++ b/services/worker/src/tests.rs @@ -235,7 +235,7 @@ mod analyses { use crate::analysis::openrouter::OpenRouter; use crate::analysis::prompt::{Kind, schema, validate}; - use crate::analysis::{Analyzer, backoff}; + use crate::analysis::{Analyzer, Lane, Lanes, backoff}; /// Scripted replies (then the last one forever) and the requests received. #[derive(Clone, Default)] @@ -337,11 +337,11 @@ mod analyses { } fn analyzer(db: &PgPool, base_url: &str) -> Analyzer { - Analyzer::new(db.clone(), OpenRouter::new(base_url, "test-key", None), 3, 2) + Analyzer::new(db.clone(), OpenRouter::new(base_url, "test-key", None), 3, Lanes { quick: 1, slow: 2 }) } async fn run_once(a: &Analyzer) -> bool { - match a.claim().await.unwrap() { + match a.claim(Lane::Slow).await.unwrap() { Some(job) => { a.handle(job).await; true @@ -464,10 +464,10 @@ mod analyses { let a = analyzer(&db, &url); // the analysis goes first; the preview only when nothing else is due - let job = a.claim().await.unwrap().expect("the analysis"); + let job = a.claim(Lane::Slow).await.unwrap().expect("the analysis"); assert_eq!(job.id, analysis); a.handle(job).await; - assert!(a.claim().await.unwrap().is_none()); + assert!(a.claim(Lane::Slow).await.unwrap().is_none()); let job = a.claim_preview().await.unwrap().expect("the preview"); a.handle_preview(job).await; let (status, title, model, credits): (String, Option, Option, i64) = sqlx::query_as( @@ -811,7 +811,7 @@ mod analyses { let id = request_analysis(&db, &s).await; let (_, url) = fake_openrouter(vec![reply(&content(Kind::Natal))]).await; let a = analyzer(&db, &url); - let job = a.claim().await.unwrap().expect("a job"); + let job = a.claim(Lane::Slow).await.unwrap().expect("a job"); // an admin restarts it while the worker is still writing it sqlx::query("UPDATE analyses SET status = 'pending', started_at = NULL, attempts = 0 WHERE id = $1") .bind(id) @@ -829,6 +829,40 @@ mod analyses { assert_eq!(status, "done"); } + #[sqlx::test(migrator = "db::MIGRATOR")] + async fn summaries_have_their_own_lane(db: PgPool) { + let s = setup(&db).await; + let natal = request_analysis(&db, &s).await; + let summary = |id: Uuid| { + sqlx::query( + "INSERT INTO analyses (id, kind, chart_id, requested_by, language, model, prompt_version, cache_key, \ + credits_charged) \ + VALUES ($1, 'summary', $2, $3, 'hu', 'test/model', 'v1', 'key', 0)", + ) + .bind(id) + .bind(s.chart) + .bind(s.user) + }; + let first = Uuid::now_v7(); + summary(first).execute(&db).await.unwrap(); + let a = analyzer(&db, "http://127.0.0.1:9"); + + // the quick lane takes the summary, never the detailed analysis + let id_of = |work: Option| match work { + Some(crate::analysis::Work::Analysis(job)) => Some(job.id), + Some(_) => panic!("not an analysis"), + None => None, + }; + assert_eq!(id_of(a.claim_quick().await.unwrap()), Some(first)); + assert_eq!(id_of(a.claim_quick().await.unwrap()), None); + // the slow lane prefers the detailed analysis, then helps with quick work + let second = Uuid::now_v7(); + summary(second).execute(&db).await.unwrap(); + assert_eq!(id_of(a.claim_slow().await.unwrap()), Some(natal)); + assert_eq!(id_of(a.claim_slow().await.unwrap()), Some(second)); + assert_eq!(id_of(a.claim_slow().await.unwrap()), None); + } + #[sqlx::test(migrator = "db::MIGRATOR")] async fn answers_a_question_about_a_relationship_with_both_charts(db: PgPool) { let s = setup(&db).await;