Skip to content

Commit e7b4ee8

Browse files
committed
Allow a max number of retries after a block fails
if `max_retries` is not set then no limit is used (same as previous behavior). Fixes #2152
1 parent 115fed0 commit e7b4ee8

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/blocks.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! `error_format` | Overrides global `error_format` | None
1313
//! `error_fullscreen_format` | Overrides global `error_fullscreen_format` | None
1414
//! `error_interval` | How long to wait until restarting the block after an error occurred. | `5`
15+
//! `max_retries` | How many times should a block be restarted the block after an error occurred. If no limit is specified none will be enforced. | `None`
1516
//! `[block.theme_overrides]` | Same as the top-level config option, but for this block only. Refer to `Themes and Icons` below. | None
1617
//! `[block.icons_overrides]` | Same as the top-level config option, but for this block only. Refer to `Themes and Icons` below. | None
1718
//! `[[block.click]]` | Set or override click action for the block. See below for details. | Block default / None
@@ -87,14 +88,25 @@ macro_rules! define_blocks {
8788
$(#[cfg(feature = $feat)])?
8889
#[allow(deprecated)]
8990
Self::$block(config) => futures.push(async move {
90-
while let Err(err) = $block::run(&config, &api).await {
91+
let mut error_count: u8 = 0;
92+
while let Err(mut err) = $block::run(&config, &api).await {
93+
let should_retry = api
94+
.max_retries
95+
.map_or(true, |max_retries| error_count < max_retries);
96+
if !should_retry {
97+
err = Error {
98+
message: Some("Block failed too many times, giving up".into()),
99+
cause: Some(Arc::new(err)),
100+
};
101+
}
91102
if api.set_error(err).is_err() {
92103
return;
93104
}
94105
tokio::select! {
95-
_ = tokio::time::sleep(api.error_interval) => (),
106+
_ = tokio::time::sleep(api.error_interval), if should_retry => (),
96107
_ = api.wait_for_update_request() => (),
97108
}
109+
error_count = error_count.saturating_add(1);
98110
}
99111
}.boxed_local()),
100112
)*
@@ -210,6 +222,7 @@ pub struct CommonApi {
210222
pub(crate) request_sender: mpsc::UnboundedSender<Request>,
211223
pub(crate) error_interval: Duration,
212224
pub(crate) geolocator: Arc<Geolocator>,
225+
pub(crate) max_retries: Option<u8>,
213226
}
214227

215228
impl CommonApi {

src/blocks/packages.rs

+11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
//! [[block]]
8383
//! block = "packages"
8484
//! interval = 1800
85+
//! error_interval = 300
86+
//! max_retries = 5
8587
//! package_manager = ["apt"]
8688
//! format = " $icon $apt updates available"
8789
//! format_singular = " $icon One update available "
@@ -103,6 +105,8 @@
103105
//! block = "packages"
104106
//! package_manager = ["pacman"]
105107
//! interval = 600
108+
//! error_interval = 300
109+
//! max_retries = 5
106110
//! format = " $icon $pacman updates available "
107111
//! format_singular = " $icon $pacman update available "
108112
//! format_up_to_date = " $icon system up to date "
@@ -124,6 +128,7 @@
124128
//! package_manager = ["pacman", "aur"]
125129
//! interval = 600
126130
//! error_interval = 300
131+
//! max_retries = 5
127132
//! format = " $icon $pacman + $aur = $total updates available "
128133
//! format_singular = " $icon $total update available "
129134
//! format_up_to_date = " $icon system up to date "
@@ -139,6 +144,8 @@
139144
//! block = "packages"
140145
//! package_manager = ["dnf"]
141146
//! interval = 1800
147+
//! error_interval = 300
148+
//! max_retries = 5
142149
//! format = " $icon $dnf.eng(w:1) updates available "
143150
//! format_singular = " $icon One update available "
144151
//! format_up_to_date = " $icon system up to date "
@@ -156,6 +163,8 @@
156163
//! block = "packages"
157164
//! package_manager = ["xbps"]
158165
//! interval = 1800
166+
//! error_interval = 300
167+
//! max_retries = 5
159168
//! format = " $icon $xbps.eng(w:1) updates available "
160169
//! format_singular = " $icon One update available "
161170
//! format_up_to_date = " $icon system up to date "
@@ -174,6 +183,8 @@
174183
//! block = "packages"
175184
//! package_manager = ["apt", "pacman", "aur", "dnf", "xbps"]
176185
//! interval = 1800
186+
//! error_interval = 300
187+
//! max_retries = 5
177188
//! format = " $icon $apt + $pacman + $aur + $dnf + $xbps = $total updates available "
178189
//! format_singular = " $icon One update available "
179190
//! format_up_to_date = " $icon system up to date "

src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub struct CommonBlockConfig {
108108
pub error_interval: u64,
109109
pub error_format: FormatConfig,
110110
pub error_fullscreen_format: FormatConfig,
111+
pub max_retries: Option<u8>,
111112

112113
pub if_command: Option<String>,
113114
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl BarState {
270270
request_sender: self.request_sender.clone(),
271271
error_interval: Duration::from_secs(block_config.common.error_interval),
272272
geolocator: self.config.geolocator.clone(),
273+
max_retries: block_config.common.max_retries,
273274
};
274275

275276
let error_format = block_config

0 commit comments

Comments
 (0)