fix: bypass saved lyrics cooldown for manual runs

This commit is contained in:
ab committed 2026-11-23 02:29:44 +02:00
1 parent 6036fcf336
commit a296af3b37
3 files changed
+61 -3

No files matched your search

+4
View File
@@ -43,6 +43,10 @@ exponential backoff. A durable service cooldown stops the run on such errors;
overlapping enrichment runs. Cancelled runs release the lock, and completed
lookup results survive interruption before attachment to content.
Manual runs bypass saved global cooldown and individual retry delays, while
still skipping completed searches. A fresh API failure stops the run and saves
backoff for subsequent automatic runs; request throttling remains in effect.
## Manual editing and playback
The track editor in Admin → Library supports adding, editing and removing
+19 -3
View File
@@ -40,7 +40,7 @@ const CANDIDATES: &str = r#"WITH candidates AS (
LEFT JOIN furumusic__lyrics_binding b ON b.audio_sha256=c.hash
LEFT JOIN furumusic__lyrics_lookup l ON l.signature=c.signature
WHERE (b.signature IS DISTINCT FROM c.signature OR l.signature IS NULL OR l.status='retry')
AND (l.retry_at IS NULL OR l.retry_at<=now())
AND ($2 OR l.retry_at IS NULL OR l.retry_at<=now())
ORDER BY c.hash LIMIT $1"#;
#[async_trait::async_trait]
@@ -80,11 +80,14 @@ impl Job for LyricsBackfillJob {
}
async fn enrich(ctx: &JobContext, log: &mut JobLog) -> Result<()> {
let cooldown: bool = sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM furumusic__lyrics_lookup WHERE signature='_lrclib_cooldown' AND retry_at>now())").fetch_one(&ctx.pool).await?;
if cooldown {
let manual = is_manual_run(&ctx.pool, ctx.run_id).await?;
if cooldown_active(&ctx.pool, manual).await? {
log.info("LRCLIB retry cooldown is active.");
return Ok(());
}
if manual {
log.info("Manual run: bypassing saved LRCLIB cooldown and retry delays.");
}
let client = Client::builder()
.timeout(std::time::Duration::from_secs(25))
.user_agent(concat!(
@@ -99,6 +102,7 @@ async fn enrich(ctx: &JobContext, log: &mut JobLog) -> Result<()> {
loop {
let batch = sqlx::query_as::<_, Candidate>(CANDIDATES)
.bind(BATCH_SIZE)
.bind(manual)
.fetch_all(&ctx.pool)
.await?;
if batch.is_empty() {
@@ -164,6 +168,18 @@ async fn enrich(ctx: &JobContext, log: &mut JobLog) -> Result<()> {
Ok(())
}
async fn is_manual_run(pool: &sqlx::PgPool, run_id: i64) -> Result<bool> {
Ok(sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM furumusic__job_run WHERE id=$1 AND job_name='lyrics_backfill' AND trigger='manual')")
.bind(run_id).fetch_one(pool).await?)
}
async fn cooldown_active(pool: &sqlx::PgPool, manual: bool) -> Result<bool> {
if manual {
return Ok(false);
}
Ok(sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM furumusic__lyrics_lookup WHERE signature='_lrclib_cooldown' AND retry_at>now())").fetch_one(pool).await?)
}
async fn save_retry(pool: &sqlx::PgPool, signature: &str, delay: u64) -> Result<()> {
let seconds = delay as f64
* (1.0 + f64::from(chrono::Utc::now().timestamp_subsec_millis()) / 1000.0 * 0.1);
+38
View File
@@ -69,6 +69,8 @@ async fn postgres_cache_retry_content_binding_and_manual_versions() {
CREATE TABLE furumusic__track(id BIGINT PRIMARY KEY,title TEXT,release_id BIGINT,audio_file_id BIGINT,duration_seconds DOUBLE PRECISION,is_hidden BOOLEAN DEFAULT false);
CREATE TABLE furumusic__track_artist(track_id BIGINT,artist_id BIGINT,role TEXT,position INTEGER);
CREATE TABLE furumusic__federation_content_id_cache(media_file_id BIGINT,sha256_hash TEXT,content_id TEXT);
CREATE TABLE furumusic__job_run(id BIGINT PRIMARY KEY,job_name TEXT,trigger TEXT);
INSERT INTO furumusic__job_run VALUES(1,'lyrics_backfill','manual'),(2,'lyrics_backfill','scheduled'),(3,'lyrics_backfill','programmatic'),(4,'other','manual');
INSERT INTO furumusic__release VALUES(1,'Album',false);
INSERT INTO furumusic__artist VALUES(1,'Artist');
INSERT INTO furumusic__media_file VALUES(1,'audio-a'),(2,'audio-b'),(3,'audio-a');
@@ -82,8 +84,13 @@ async fn postgres_cache_retry_content_binding_and_manual_versions() {
let candidates = || {
sqlx::query_as::<_, Candidate>(CANDIDATES)
.bind(200)
.bind(false)
.fetch_all(&pool)
};
assert!(is_manual_run(&pool, 1).await.unwrap());
for id in [2, 3, 4, 999] {
assert!(!is_manual_run(&pool, id).await.unwrap());
}
let rows = candidates().await.unwrap();
assert_eq!(rows.len(), 2, "identical bytes have one lookup binding");
assert_eq!(
@@ -125,6 +132,26 @@ async fn postgres_cache_retry_content_binding_and_manual_versions() {
candidates().await.unwrap().is_empty(),
"a miss is never looked up again"
);
assert!(
sqlx::query_as::<_, Candidate>(CANDIDATES)
.bind(200)
.bind(true)
.fetch_all(&pool)
.await
.unwrap()
.is_empty(),
"manual runs still skip completed lookups"
);
save_retry(&pool, "_lrclib_cooldown", 300).await.unwrap();
assert!(cooldown_active(&pool, false).await.unwrap());
assert!(
!cooldown_active(&pool, true).await.unwrap(),
"manual runs bypass global cooldown"
);
assert!(
cooldown_active(&pool, false).await.unwrap(),
"manual bypass preserves automatic cooldown"
);
save_retry(&pool, &signature, 120_000).await.unwrap();
save_retry(&pool, &signature, 120_000).await.unwrap();
let seconds:f64=sqlx::query_scalar("SELECT EXTRACT(EPOCH FROM retry_at-now())::float8 FROM furumusic__lyrics_lookup WHERE signature=$1").bind(&signature).fetch_one(&pool).await.unwrap();
@@ -142,6 +169,17 @@ async fn postgres_cache_retry_content_binding_and_manual_versions() {
candidates().await.unwrap().is_empty(),
"temporary failure honors cooldown"
);
let manual_candidates = sqlx::query_as::<_, Candidate>(CANDIDATES)
.bind(200)
.bind(true)
.fetch_all(&pool)
.await
.unwrap();
assert_eq!(
manual_candidates.len(),
2,
"manual runs bypass per-content retry delays"
);
sqlx::query("UPDATE furumusic__lyrics_lookup SET retry_at=now()-interval '1 second'")
.execute(&pool)
.await