fix: trigger reconnects when errors occur during idle and fetch

This commit is contained in:
dignifiedquire
2020-06-03 15:40:54 +02:00
parent 6ce27a7f87
commit 86c6b09814
2 changed files with 20 additions and 49 deletions

View File

@@ -295,6 +295,8 @@ impl Imap {
match login_res { match login_res {
Ok(session) => { Ok(session) => {
// needs to be set here to ensure it is set on reconnects.
self.connected = true;
self.session = Some(session); self.session = Some(session);
Ok(()) Ok(())
} }

View File

@@ -77,13 +77,14 @@ async fn inbox_loop(ctx: Context, started: Sender<()>, inbox_handlers: ImapConne
let ctx1 = ctx.clone(); let ctx1 = ctx.clone();
let fut = async move { let fut = async move {
started.send(()).await;
let ctx = ctx1; let ctx = ctx1;
if let Err(err) = connection.connect_configured(&ctx).await { if let Err(err) = connection.connect_configured(&ctx).await {
error!(ctx, "{}", err); error!(ctx, "{}", err);
return; return;
} }
started.send(()).await;
// track number of continously executed jobs // track number of continously executed jobs
let mut jobs_loaded = 0; let mut jobs_loaded = 0;
let mut probe_network = false; let mut probe_network = false;
@@ -102,7 +103,8 @@ async fn inbox_loop(ctx: Context, started: Sender<()>, inbox_handlers: ImapConne
} }
None => { None => {
jobs_loaded = 0; jobs_loaded = 0;
probe_network = fetch_idle(&ctx, &mut connection).await; probe_network =
fetch_idle(&ctx, &mut connection, "configured_inbox_folder").await;
} }
} }
} }
@@ -122,12 +124,10 @@ async fn fetch(ctx: &Context, connection: &mut Imap) {
match get_watch_folder(&ctx, "configured_inbox_folder").await { match get_watch_folder(&ctx, "configured_inbox_folder").await {
Some(watch_folder) => { Some(watch_folder) => {
// fetch // fetch
connection if let Err(err) = connection.fetch(&ctx, &watch_folder).await {
.fetch(&ctx, &watch_folder) connection.trigger_reconnect();
.await error!(ctx, "{}", err);
.unwrap_or_else(|err| { }
error!(ctx, "{}", err);
});
} }
None => { None => {
warn!(ctx, "Can not fetch inbox folder, not set"); warn!(ctx, "Can not fetch inbox folder, not set");
@@ -136,16 +136,14 @@ async fn fetch(ctx: &Context, connection: &mut Imap) {
} }
} }
async fn fetch_idle(ctx: &Context, connection: &mut Imap) -> bool { async fn fetch_idle(ctx: &Context, connection: &mut Imap, folder: &str) -> bool {
match get_watch_folder(&ctx, "configured_inbox_folder").await { match get_watch_folder(&ctx, folder).await {
Some(watch_folder) => { Some(watch_folder) => {
// fetch // fetch
connection if let Err(err) = connection.fetch(&ctx, &watch_folder).await {
.fetch(&ctx, &watch_folder) connection.trigger_reconnect();
.await error!(ctx, "{}", err);
.unwrap_or_else(|err| { }
error!(ctx, "{}", err);
});
// idle // idle
if connection.can_idle() { if connection.can_idle() {
@@ -153,6 +151,7 @@ async fn fetch_idle(ctx: &Context, connection: &mut Imap) -> bool {
.idle(&ctx, Some(watch_folder)) .idle(&ctx, Some(watch_folder))
.await .await
.unwrap_or_else(|err| { .unwrap_or_else(|err| {
connection.trigger_reconnect();
error!(ctx, "{}", err); error!(ctx, "{}", err);
false false
}) })
@@ -185,46 +184,16 @@ async fn simple_imap_loop(
let ctx1 = ctx.clone(); let ctx1 = ctx.clone();
let fut = async move { let fut = async move {
started.send(()).await;
let ctx = ctx1; let ctx = ctx1;
if let Err(err) = connection.connect_configured(&ctx).await { if let Err(err) = connection.connect_configured(&ctx).await {
error!(ctx, "{}", err); error!(ctx, "{}", err);
return; return;
} }
loop { started.send(()).await;
match get_watch_folder(&ctx, folder.as_ref()).await {
Some(watch_folder) => {
// fetch
connection
.fetch(&ctx, &watch_folder)
.await
.unwrap_or_else(|err| {
error!(ctx, "{}", err);
});
// idle loop {
if connection.can_idle() { fetch_idle(&ctx, &mut connection, folder.as_ref()).await;
connection
.idle(&ctx, Some(watch_folder))
.await
.unwrap_or_else(|err| {
error!(ctx, "{}", err);
false
});
} else {
connection.fake_idle(&ctx, Some(watch_folder)).await;
}
}
None => {
warn!(
&ctx,
"No watch folder found for {}, skipping",
folder.as_ref()
);
connection.fake_idle(&ctx, None).await;
}
}
} }
}; };