Skip to content

Commit 4587b13

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 86e8112 commit 4587b13

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

src/blocks.rs

Lines changed: 8 additions & 1 deletion
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
@@ -86,14 +87,19 @@ macro_rules! define_blocks {
8687
$(#[cfg(feature = $feat)])?
8788
#[allow(deprecated)]
8889
Self::$block(config) => futures.push(async move {
90+
let mut error_count: u8 = 0;
8991
while let Err(err) = $block::run(&config, &api).await {
9092
if api.set_error(err).is_err() {
9193
return;
9294
}
95+
let should_retry = api
96+
.max_retries
97+
.map_or(true, |max_retries| error_count < max_retries);
9398
tokio::select! {
94-
_ = tokio::time::sleep(api.error_interval) => (),
99+
_ = tokio::time::sleep(api.error_interval), if should_retry => (),
95100
_ = api.wait_for_update_request() => (),
96101
}
102+
error_count = error_count.saturating_add(1);
97103
}
98104
}.boxed_local()),
99105
)*
@@ -208,6 +214,7 @@ pub struct CommonApi {
208214
pub(crate) update_request: Arc<Notify>,
209215
pub(crate) request_sender: mpsc::UnboundedSender<Request>,
210216
pub(crate) error_interval: Duration,
217+
pub(crate) max_retries: Option<u8>,
211218
}
212219

213220
impl CommonApi {

src/blocks/packages.rs

Lines changed: 11 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub struct CommonBlockConfig {
103103
pub error_interval: u64,
104104
pub error_format: FormatConfig,
105105
pub error_fullscreen_format: FormatConfig,
106+
pub max_retries: Option<u8>,
106107

107108
pub if_command: Option<String>,
108109
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ impl BarState {
268268
update_request: update_request.clone(),
269269
request_sender: self.request_sender.clone(),
270270
error_interval: Duration::from_secs(block_config.common.error_interval),
271+
max_retries: block_config.common.max_retries,
271272
};
272273

273274
let error_format = block_config

0 commit comments

Comments
 (0)