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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -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<String>, Option<String>, 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<crate::analysis::Work>| 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;
|
||||
|
||||
Reference in New Issue
Block a user