feedsignal/crates/web/src/server/jobs/feed_polling.rs

30 lines
997 B
Rust
Raw Normal View History

use anyhow::Result;
use feedsignal_db::Db;
/// Re-fetches every subscribed feed and inserts any articles published since
/// the last run. `articles.url` is unique, so re-fetching the same feed and
/// inserting its (mostly already-seen) entries is safe — `insert_article`
/// silently skips ones already in the database, leaving only genuinely new
/// articles behind.
pub async fn run(db: &Db) -> Result<()> {
let feeds = db.list_feeds().await?;
tracing::info!(count = feeds.len(), "polling subscribed feeds");
for (feed_id, url) in feeds {
let (_title, articles) = match feedsignal_feeds::fetch_feed(&url, feed_id).await {
Ok(result) => result,
Err(err) => {
tracing::warn!(?err, %url, "failed to poll feed, skipping");
continue;
}
};
for article in &articles {
db.insert_article(article).await?;
}
db.mark_feed_fetched(feed_id).await?;
}
Ok(())
}