|
12 | 12 | //! `error_format` | Overrides global `error_format` | None |
13 | 13 | //! `error_fullscreen_format` | Overrides global `error_fullscreen_format` | None |
14 | 14 | //! `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` |
15 | 16 | //! `[block.theme_overrides]` | Same as the top-level config option, but for this block only. Refer to `Themes and Icons` below. | None |
16 | 17 | //! `[block.icons_overrides]` | Same as the top-level config option, but for this block only. Refer to `Themes and Icons` below. | None |
17 | 18 | //! `[[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 { |
87 | 88 | $(#[cfg(feature = $feat)])? |
88 | 89 | #[allow(deprecated)] |
89 | 90 | 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 | + } |
91 | 102 | if api.set_error(err).is_err() { |
92 | 103 | return; |
93 | 104 | } |
94 | 105 | tokio::select! { |
95 | | - _ = tokio::time::sleep(api.error_interval) => (), |
| 106 | + _ = tokio::time::sleep(api.error_interval), if should_retry => (), |
96 | 107 | _ = api.wait_for_update_request() => (), |
97 | 108 | } |
| 109 | + error_count = error_count.saturating_add(1); |
98 | 110 | } |
99 | 111 | }.boxed_local()), |
100 | 112 | )* |
@@ -210,6 +222,7 @@ pub struct CommonApi { |
210 | 222 | pub(crate) request_sender: mpsc::UnboundedSender<Request>, |
211 | 223 | pub(crate) error_interval: Duration, |
212 | 224 | pub(crate) geolocator: Arc<Geolocator>, |
| 225 | + pub(crate) max_retries: Option<u8>, |
213 | 226 | } |
214 | 227 |
|
215 | 228 | impl CommonApi { |
|
0 commit comments