Skip to content

Commit

Permalink
Fix retrying
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowRZ committed Dec 3, 2023
1 parent 846c4a0 commit 3138214
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ impl FuukaBot {

/// Run this bot.
pub async fn run(self) -> anyhow::Result<()> {
self.client.add_event_handler_context(self.context.clone());
let task: JoinHandle<()> = tokio::spawn(async move {
tokio::select! {
_ = async {
while let Err(e) = self.sync().await {
let mut initial = true;
while let Err(e) = self.sync(initial).await {
use tokio::time::{sleep, Duration};
tracing::error!("Unexpected error happened, retrying in 10s: {e:?}");
sleep(Duration::from_secs(10)).await;
initial = false;
}
} => {},
_ = self.cts.cancelled() => {},
Expand All @@ -100,19 +103,26 @@ impl FuukaBot {
Ok(task.await?)
}

async fn sync(&self) -> anyhow::Result<()> {
self.client.add_event_handler_context(self.context.clone());
async fn sync(&self, initial: bool) -> anyhow::Result<()> {
let next_batch = self.initial_sync(initial).await?;
let settings = SyncSettings::default().token(next_batch);
self.client.sync(settings).await?;
Ok(())
}

async fn initial_sync(&self, register_handler: bool) -> anyhow::Result<String> {
tracing::info!("Initial sync beginning...");
let response = self.client.sync_once(SyncSettings::default()).await?;
tracing::info!("Initial sync completed.");
self.client

if register_handler {
self.client
.add_event_handler(crate::handler::on_sync_message);
self.client
.add_event_handler(crate::handler::on_stripped_member);
let settings = SyncSettings::default().token(response.next_batch);
self.client.sync(settings).await?;
self.client
.add_event_handler(crate::handler::on_stripped_member);
}

Ok(())
Ok(response.next_batch)
}

/// Registers the graceful shutdown handler.
Expand Down

0 comments on commit 3138214

Please sign in to comment.