Skip to content

Commit 59eefca

Browse files
authored
Merge pull request #2157 from bim9262/max_retries
Allow a max number of retries after a block fails
2 parents b5f4f65 + 397737d commit 59eefca

File tree

12 files changed

+91
-20
lines changed

12 files changed

+91
-20
lines changed

files/icons/awesome4.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pomodoro_break = "\uf0f4" # fa-coffee
5858
pomodoro_paused = "\uf04c" # fa-pause
5959
pomodoro_started = "\uf04b" # fa-play
6060
pomodoro_stopped = "\uf04d" # fa-stop
61+
refresh = "\uf021" # fa-refresh
6162
resolution = "\uf096" # fa-square-o
6263
scratchpad = "\uf2d2" # fa-window-restore
6364
tasks = "\uf0ae" # fa-tasks

files/icons/awesome5.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pomodoro_break = "\uf0f4" # fa-coffee
5858
pomodoro_paused = "\uf04c" # fa-pause
5959
pomodoro_started = "\uf04b" # fa-play
6060
pomodoro_stopped = "\uf04d" # fa-stop
61+
refresh = "\uf021" # fa-sync
6162
resolution = "\uf096" # fa-square-o
6263
scratchpad = "\uf2d2" # fa-window-restore
6364
tasks = "\uf0ae"

files/icons/awesome6.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pomodoro_break = "\uf0f4" # fa-coffee
6262
pomodoro_paused = "\uf04c" # fa-pause
6363
pomodoro_started = "\uf04b" # fa-play
6464
pomodoro_stopped = "\uf04d" # fa-stop
65+
refresh = "\uf021" # fa-arrows-rotate
6566
resolution = "\uf096" # fa-square-o
6667
scratchpad = "\uf2d2" # fa-window-restore
6768
tasks = "\uf0ae"

files/icons/emoji.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pomodoro_break = "☕"
5454
pomodoro_paused = "⏸️"
5555
pomodoro_started = "▶️"
5656
pomodoro_stopped = "⏹️"
57+
refresh = "🔄"
5758
resolution = "🔳"
5859
scratchpad = "🗔"
5960
tasks = ""

files/icons/material-nf.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pomodoro_break = "\U000f0176" # nf-md-coffee
9191
pomodoro_paused = "\U000f03e4" # nf-md-pause
9292
pomodoro_started = "\U000f040a" # nf-md-play
9393
pomodoro_stopped = "\U000f04db" # nf-md-stop
94+
refresh = "\U000f0450" # nf-md-refresh
9495
resolution = "\U000f0293" # nf-md-fullscreen
9596
scratchpad = "\U000f05b2" # nf-md-window_restore
9697
tasks = "\U000f05c7" # nf-md-playlist_check

files/icons/material.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pomodoro_break = "\uefef" # coffee
7575
pomodoro_paused = "\ue034" # pause
7676
pomodoro_started = "\ue037" # play_arrow
7777
pomodoro_stopped = "\uef6a" # play_disabled
78+
refresh = "\ue5d5" # refresh
7879
resolution = "\ue3c6" # crop_square
7980
scratchpad = "\ue883" # flip_to_front
8081
tasks = "\ue8f9" # work

src/blocks.rs

Lines changed: 37 additions & 5 deletions
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
@@ -44,6 +45,8 @@ use crate::geolocator::{Geolocator, IPAddressInfo};
4445
use crate::widget::Widget;
4546
use crate::{BoxedFuture, Request, RequestCmd};
4647

48+
pub(super) const RESTART_BLOCK_BTN: &str = "restart_block_btn";
49+
4750
macro_rules! define_blocks {
4851
{
4952
$(
@@ -87,14 +90,37 @@ macro_rules! define_blocks {
8790
$(#[cfg(feature = $feat)])?
8891
#[allow(deprecated)]
8992
Self::$block(config) => futures.push(async move {
90-
while let Err(err) = $block::run(&config, &api).await {
91-
if api.set_error(err).is_err() {
93+
let mut error_count: u8 = 0;
94+
while let Err(mut err) = $block::run(&config, &api).await {
95+
let Ok(mut actions) = api.get_actions() else { return };
96+
if api.set_default_actions(&[
97+
(MouseButton::Left, Some(RESTART_BLOCK_BTN), "error_count_reset"),
98+
]).is_err() {
99+
return;
100+
}
101+
let should_retry = api
102+
.max_retries
103+
.map_or(true, |max_retries| error_count < max_retries);
104+
if !should_retry {
105+
err = Error {
106+
message: Some("Block terminated".into()),
107+
cause: Some(Arc::new(err)),
108+
};
109+
}
110+
if api.set_error_with_restartable(err, !should_retry).is_err() {
92111
return;
93112
}
94113
tokio::select! {
95-
_ = tokio::time::sleep(api.error_interval) => (),
114+
_ = tokio::time::sleep(api.error_interval), if should_retry => (),
115+
Some(action) = actions.recv(), if !should_retry => match action.as_ref(){
116+
"error_count_reset" => {
117+
error_count = 0;
118+
},
119+
_ => (),
120+
},
96121
_ = api.wait_for_update_request() => (),
97122
}
123+
error_count = error_count.saturating_add(1);
98124
}
99125
}.boxed_local()),
100126
)*
@@ -210,6 +236,7 @@ pub struct CommonApi {
210236
pub(crate) request_sender: mpsc::UnboundedSender<Request>,
211237
pub(crate) error_interval: Duration,
212238
pub(crate) geolocator: Arc<Geolocator>,
239+
pub(crate) max_retries: Option<u8>,
213240
}
214241

215242
impl CommonApi {
@@ -233,12 +260,17 @@ impl CommonApi {
233260
.error("Failed to send Request")
234261
}
235262

236-
/// Sends the error to be displayed.
263+
/// Sends the error to be displayed, no restart button will be shown.
237264
pub fn set_error(&self, error: Error) -> Result<()> {
265+
self.set_error_with_restartable(error, false)
266+
}
267+
268+
/// Sends the error to be displayed.
269+
pub fn set_error_with_restartable(&self, error: Error, restartable: bool) -> Result<()> {
238270
self.request_sender
239271
.send(Request {
240272
block_id: self.id,
241-
cmd: RequestCmd::SetError(error),
273+
cmd: RequestCmd::SetError { error, restartable },
242274
})
243275
.error("Failed to send Request")
244276
}

src/blocks/custom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async fn update_bar(
140140
});
141141
widget.state = input.state;
142142
}
143-
Err(error) => return api.set_error(error),
143+
Err(error) => return Err(error),
144144
}
145145
} else {
146146
text_empty = stdout.is_empty();

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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ impl Default for SharedConfig {
6060
}
6161

6262
fn default_error_format() -> FormatConfig {
63-
" {$short_error_message|X} ".parse().unwrap()
63+
" {$restart_block_icon |}{$short_error_message|X} "
64+
.parse()
65+
.unwrap()
6466
}
6567

6668
fn default_error_fullscreen() -> FormatConfig {
67-
" $full_error_message ".parse().unwrap()
69+
" {$restart_block_icon |}$full_error_message "
70+
.parse()
71+
.unwrap()
6872
}
6973

7074
fn default_icons_format() -> Arc<String> {
@@ -108,6 +112,7 @@ pub struct CommonBlockConfig {
108112
pub error_interval: u64,
109113
pub error_format: FormatConfig,
110114
pub error_fullscreen_format: FormatConfig,
115+
pub max_retries: Option<u8>,
111116

112117
pub if_command: Option<String>,
113118
}

0 commit comments

Comments
 (0)