Users see the kind of failure only; resolved addresses and transport details go to the logs, without URLs. Feed and audio requests are limited to the web ports. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
2.6 KiB
Rust
74 lines
2.6 KiB
Rust
//! Background refresh of subscribed podcast feeds.
|
|
//!
|
|
//! Every run takes the podcasts whose next check is due (see
|
|
//! [`crate::podcasts::runner`]): healthy feeds are checked about hourly with
|
|
//! conditional requests, failing ones back off exponentially up to a day.
|
|
//! One request per feed URL serves every account subscribed to it, a
|
|
//! bounded number of requests run concurrently, and a failing feed never
|
|
//! blocks the others or clears the stored catalog.
|
|
|
|
use crate::podcasts::{FeedFetcher, PodcastStore, now_ms, runner};
|
|
use crate::scheduler::{Job, JobContext, JobLog};
|
|
|
|
pub struct PodcastRefreshJob;
|
|
|
|
/// Session-level advisory lock: one run at a time across processes.
|
|
const ADVISORY_LOCK_ID: i64 = 0x504f_4443_5246_5348; // "PODCRFSH"
|
|
|
|
/// Failures listed individually in the run log.
|
|
const LOGGED_FAILURES: usize = 20;
|
|
|
|
#[async_trait::async_trait]
|
|
impl Job for PodcastRefreshJob {
|
|
fn name(&self) -> &'static str {
|
|
"podcast_refresh"
|
|
}
|
|
|
|
fn description(&self) -> &'static str {
|
|
"Check subscribed podcast feeds for new episodes (metadata only, no audio)"
|
|
}
|
|
|
|
fn default_cron(&self) -> &'static str {
|
|
// Every five minutes; each feed is checked only when due.
|
|
"0 */5 * * * *"
|
|
}
|
|
|
|
async fn run(&self, ctx: &JobContext, log: &mut JobLog) -> anyhow::Result<()> {
|
|
let mut conn = ctx.pool.acquire().await?;
|
|
// A session lock must never return to the pool, including on
|
|
// cancellation; the store below uses other pool connections.
|
|
conn.close_on_drop();
|
|
let locked: bool = sqlx::query_scalar("SELECT pg_try_advisory_lock($1)")
|
|
.bind(ADVISORY_LOCK_ID)
|
|
.fetch_one(&mut *conn)
|
|
.await?;
|
|
if !locked {
|
|
log.info("Podcast refresh is already running; skipping this run");
|
|
return Ok(());
|
|
}
|
|
let store = PodcastStore::new(ctx.pool.clone());
|
|
let fetcher = FeedFetcher::new()?;
|
|
let result = runner::refresh_due(
|
|
&store,
|
|
&fetcher,
|
|
now_ms(),
|
|
runner::RUN_LIMIT,
|
|
runner::RUN_CONCURRENCY,
|
|
)
|
|
.await;
|
|
conn.close().await?;
|
|
let summary = result?;
|
|
for (podcast_id, error) in summary.failures.iter().take(LOGGED_FAILURES) {
|
|
log.warn(&format!("podcast {podcast_id}: {error}"));
|
|
}
|
|
if summary.failures.len() > LOGGED_FAILURES {
|
|
log.warn(&format!(
|
|
"... and {} more failed feeds",
|
|
summary.failures.len() - LOGGED_FAILURES
|
|
));
|
|
}
|
|
log.info(&format!("Podcast refresh: {summary}"));
|
|
Ok(())
|
|
}
|
|
}
|