From 04947d446a67097700f99ae2375487e0d152c4cd Mon Sep 17 00:00:00 2001 From: "crashkeys.dev" Date: Tue, 24 Mar 2026 21:08:52 +0100 Subject: [PATCH] actor bsky: session is recreated from login on resume failure. --- src/lib.rs | 12 +++--------- src/sink.rs | 36 +++++++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5dffd22..5166aed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,15 +22,9 @@ pub mod run { // quickly just to confirm that everything is functioning as it should. let use_bsky = std::env::var("USE_BLUESKY").unwrap_or("0".to_string()) == "1"; let bsky = if use_bsky { - Some(BskySink::spawn( - BskySink::new_session( - std::env::var("BLUESKY_USERNAME").expect("Bluesky username not supplied"), - std::env::var("BLUESKY_PASSWORD") - .expect("Bluesky application password not supplied"), - ) - .await - .expect("Could not connect to Bluesky with supplied credentials"), - )) + Some(BskySink::spawn(BskySink::new_from_env().await.expect( + "Could not connect to Bluesky with supplied credentials", + ))) } else { None }; diff --git a/src/sink.rs b/src/sink.rs index 53f62c2..7d418f7 100644 --- a/src/sink.rs +++ b/src/sink.rs @@ -71,6 +71,16 @@ impl BskySink { }) } + /// Helper to create a new session from hard-coded environment variables. + pub async fn new_from_env() -> Result { + // TODO(config): Remove this altogether, or make it generic over the variables... + Self::new_session( + std::env::var("BLUESKY_USERNAME").expect("Bluesky username not supplied"), + std::env::var("BLUESKY_PASSWORD").expect("Bluesky application password not supplied"), + ) + .await + } + async fn submit_post(&mut self, quote: Quote) -> Result<(), ()> { let post = bsky_sdk::api::app::bsky::feed::post::RecordData { text: quote.into(), @@ -84,13 +94,25 @@ impl BskySink { tags: None, }; - if let Err(e) = self - .bsky_agent - .resume_session(self.bsky_session.clone()) - .await - { - eprintln!("Failed to resume sessions due to following error: {e}"); - return Err(()); + // Let's attempt to resume our session or start a new one up to 5 times; + // if this fails, we call it quits. + const MAX_RETRIES: u8 = 5; + for idx in 1..=MAX_RETRIES { + let resume_result = self + .bsky_agent + .resume_session(self.bsky_session.clone()) + .await; + + // If no error occurred, we simply proceed as normal. + let Err(e) = resume_result else { break }; + + // Otherwise, we log the failed attempt... + eprintln!( + "[{idx}/{MAX_RETRIES}] Failed to resume sessions due to following error: {e}" + ); + + // ...and then try to log in again. + *self = Self::new_from_env().await? } match self.bsky_agent.create_record(post.clone()).await { -- 2.51.2