Skip to content

Introduce a retry mechanism when the model download fails. #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

51 changes: 48 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use crate::data::store::*;
use crate::landing::model_files_item::ModelFileItemAction;
use crate::shared::actions::{ChatAction, DownloadAction};
use crate::shared::download_notification_popup::{
DownloadNotificationPopupAction, DownloadNotificationPopupWidgetRefExt, DownloadResult,
PopupAction,
DownloadNotificationPopupAction, DownloadNotificationPopupRef, DownloadNotificationPopupWidgetRefExt, DownloadResult, PopupAction
};
use crate::shared::popup_notification::PopupNotificationWidgetRefExt;
use moly_protocol::data::{File, FileID};

use makepad_widgets::*;
use markdown::MarkdownAction;
use moly_mofa::MofaServerId;
Expand Down Expand Up @@ -142,6 +143,15 @@ pub struct App {

#[rust]
store: Store,

#[rust]
timer: Timer,

#[rust]
download_retry_attempts: usize,

#[rust]
file_id: Option<FileID>,
}

impl LiveRegister for App {
Expand All @@ -159,6 +169,16 @@ impl LiveRegister for App {

impl AppMain for App {
fn handle_event(&mut self, cx: &mut Cx, event: &Event) {

// It triggers when the timer expires.
if self.timer.is_event(event).is_some() {
if let Some(file_id) = &self.file_id {
let (model, file) = self.store.get_model_and_file_download(&file_id);
self.store.downloads.download_file(model, file);
self.ui.redraw(cx);
}
}

let scope = &mut Scope::with_data(&mut self.store);
self.ui.handle_event(cx, event, scope);
self.match_event(cx, event);
Expand Down Expand Up @@ -281,11 +301,36 @@ impl App {
cx.action(ModelSelectorListAction::AddedOrDeletedModel);
}
DownloadPendingNotification::DownloadErrored(file) => {
popup.set_data(&file, DownloadResult::Failure);
self.file_id = Some((file.id).clone());
self.start_retry_timeout(cx, popup, file);
}
}

self.ui.popup_notification(id!(popup_notification)).open(cx);
}
}

fn start_retry_timeout(&mut self, cx: &mut Cx, mut popup: DownloadNotificationPopupRef, file: File) {
match self.download_retry_attempts {
0 => {
self.timer = cx.start_timeout(15.0);
self.download_retry_attempts += 1;
popup.set_retry_data();
},
1 => {
self.timer = cx.start_timeout(30.0);
self.download_retry_attempts += 1;
popup.set_retry_data();
},
2 => {
self.timer = cx.start_timeout(60.0);
self.download_retry_attempts += 1;
popup.set_retry_data();
},
_ => {
popup.set_data(&file, DownloadResult::Failure);
self.download_retry_attempts = 0;
}
}
}
}
39 changes: 39 additions & 0 deletions src/shared/download_notification_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ pub struct DownloadNotificationPopup {
file_id: Option<FileID>,
#[rust]
filename: String,
#[rust]
count: usize
}

impl Widget for DownloadNotificationPopup {
Expand Down Expand Up @@ -313,6 +315,37 @@ impl DownloadNotificationPopup {
)),
);
}

pub fn show_retry_content(&mut self) {

let content = self.label(id!(summary));
self.view(id!(success_icon)).set_visible(false);
self.view(id!(failure_icon)).set_visible(true);

self.view(id!(success_actions)).set_visible(false);
self.view(id!(failure_actions)).set_visible(false);

self.label(id!(title))
.set_text("Retry");

match self.count {
0 => {
content.set_text("Download interrupted. Will resume in 15 seconds.");
self.count += 1;
},
1 => {
content.set_text("Download interrupted. Will resume in 30 seconds.");
self.count += 1;
},
2 => {
content.set_text("Download interrupted. Will resume in 60 seconds.");
self.count += 1;
},
_ => {
self.count = 0;
}
}
}
}

impl DownloadNotificationPopupRef {
Expand All @@ -325,4 +358,10 @@ impl DownloadNotificationPopupRef {
inner.update_content();
}
}

pub fn set_retry_data(&mut self) {
if let Some(mut inner) = self.borrow_mut() {
inner.show_retry_content();
}
}
}