Skip to content

Commit 0f6fcb3

Browse files
committed
Add timeout and retries in query, fixing apps load
1 parent 7bb50e8 commit 0f6fcb3

File tree

4 files changed

+226
-89
lines changed

4 files changed

+226
-89
lines changed

src/container.rs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use std::cell::RefCell;
2020
use std::path::Path;
2121

2222
mod imp {
23+
use std::time::Duration;
24+
2325
use super::*;
2426

2527
// This contains all the container informations given by distrobox, plus an associated KnownDistro struct
@@ -36,10 +38,10 @@ mod imp {
3638
pub status_detail: RefCell<String>,
3739
#[property(get, set)]
3840
pub image: RefCell<String>,
39-
#[property(get, set)]
41+
#[property(get, set, nullable)]
4042
pub distro: RefCell<Option<KnownDistro>>,
41-
pub apps: Query<TypedListStore<glib::BoxedAnyObject>, anyhow::Error>,
42-
pub binaries: Query<TypedListStore<glib::BoxedAnyObject>, anyhow::Error>,
43+
pub apps: Query<TypedListStore<glib::BoxedAnyObject>>,
44+
pub binaries: Query<TypedListStore<glib::BoxedAnyObject>>,
4345
}
4446

4547
impl Default for Container {
@@ -51,11 +53,26 @@ mod imp {
5153
status_detail: RefCell::new(String::new()),
5254
image: RefCell::new(String::new()),
5355
distro: RefCell::new(None),
56+
57+
// Fetching apps often fails when the container is not running and distrobox has to start it,
58+
// so we add retries
5459
apps: Query::new("apps".into(), || async {
5560
Ok(TypedListStore::new())
61+
})
62+
.with_timeout(Duration::from_secs(1))
63+
.with_retry_strategy(|n| if n < 3 {
64+
Some(Duration::from_secs(n as u64))
65+
} else {
66+
None
5667
}),
5768
binaries: Query::new("binaries".into(), || async {
5869
Ok(TypedListStore::new())
70+
})
71+
.with_timeout(Duration::from_secs(1))
72+
.with_retry_strategy(|n| if n < 3 {
73+
Some(Duration::from_secs(n as u64))
74+
} else {
75+
None
5976
}),
6077
}
6178
}
@@ -79,23 +96,12 @@ impl Container {
7996
glib::Object::builder().build()
8097
}
8198
pub fn from_info(root_store: &RootStore, value: ContainerInfo) -> Self {
82-
let distro = known_distro_by_image(&value.image);
83-
84-
let (status_tag, status_detail) = match value.status {
85-
Status::Up(v) => ("up", v),
86-
Status::Created(v) => ("created", v),
87-
Status::Exited(v) => ("exited", v),
88-
Status::Other(v) => ("other", v),
89-
};
9099
let this: Self = glib::Object::builder()
91100
.property("root-store", root_store)
92-
.property("name", value.name)
93-
.property("image", value.image)
94-
.property("distro", distro)
95-
.property("status-tag", status_tag)
96-
.property("status-detail", status_detail)
97101
.build();
98102

103+
this.apply_container_info(value);
104+
99105
let this_clone = this.clone();
100106
this.apps().set_fetcher(move || {
101107
let this = this_clone.clone();
@@ -135,11 +141,28 @@ impl Container {
135141
this
136142
}
137143

138-
pub fn apps(&self) -> Query<TypedListStore<BoxedAnyObject>, anyhow::Error> {
144+
pub fn apply_container_info(&self, value: ContainerInfo) {
145+
let distro = known_distro_by_image(&value.image);
146+
147+
let (status_tag, status_detail) = match value.status {
148+
Status::Up(v) => ("up", v),
149+
Status::Created(v) => ("created", v),
150+
Status::Exited(v) => ("exited", v),
151+
Status::Other(v) => ("other", v),
152+
};
153+
154+
self.set_name(value.name);
155+
self.set_image(value.image);
156+
self.set_distro(distro);
157+
self.set_status_tag(status_tag.to_string());
158+
self.set_status_detail(status_detail);
159+
}
160+
161+
pub fn apps(&self) -> Query<TypedListStore<BoxedAnyObject>> {
139162
self.imp().apps.clone()
140163
}
141164

142-
pub fn binaries(&self) -> Query<TypedListStore<BoxedAnyObject>, anyhow::Error> {
165+
pub fn binaries(&self) -> Query<TypedListStore<BoxedAnyObject>> {
143166
self.imp().binaries.clone()
144167
}
145168

src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ mod terminal_combo_row;
3838
mod welcome_view;
3939
mod window;
4040
pub use store::root_store;
41+
use tracing::level_filters::LevelFilter;
4142
pub mod query;
4243

4344
use self::application::DistroShelfApplication;
@@ -48,13 +49,17 @@ use gettextrs::{bind_textdomain_codeset, bindtextdomain, textdomain};
4849
use gtk::prelude::*;
4950
use gtk::{gio, glib};
5051
use tracing::info;
51-
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
52+
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
5253

5354
fn main() -> glib::ExitCode {
5455
// Initialize tracing
5556
tracing_subscriber::registry()
5657
.with(fmt::layer())
57-
.with(EnvFilter::from_default_env())
58+
.with(
59+
EnvFilter::builder()
60+
.with_default_directive(LevelFilter::INFO.into())
61+
.from_env_lossy(),
62+
)
5863
.init();
5964

6065
info!("Starting DistroShelf application");

0 commit comments

Comments
 (0)