@@ -45,6 +45,8 @@ use crate::geolocator::{Geolocator, IPAddressInfo};
45
45
use crate :: widget:: Widget ;
46
46
use crate :: { BoxedFuture , Request , RequestCmd } ;
47
47
48
+ pub ( super ) const RESTART_BLOCK_BTN : & str = "restart_block_btn" ;
49
+
48
50
macro_rules! define_blocks {
49
51
{
50
52
$(
@@ -90,20 +92,32 @@ macro_rules! define_blocks {
90
92
Self :: $block( config) => futures. push( async move {
91
93
let mut error_count: u8 = 0 ;
92
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
+ }
93
101
let should_retry = api
94
102
. max_retries
95
103
. map_or( true , |max_retries| error_count < max_retries) ;
96
104
if !should_retry {
97
105
err = Error {
98
- message: Some ( "Block failed too many times, giving up " . into( ) ) ,
106
+ message: Some ( "Block terminated " . into( ) ) ,
99
107
cause: Some ( Arc :: new( err) ) ,
100
108
} ;
101
109
}
102
- if api. set_error ( err) . is_err( ) {
110
+ if api. set_error_with_restartable ( err, !should_retry ) . is_err( ) {
103
111
return ;
104
112
}
105
113
tokio:: select! {
106
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
+ } ,
107
121
_ = api. wait_for_update_request( ) => ( ) ,
108
122
}
109
123
error_count = error_count. saturating_add( 1 ) ;
@@ -246,12 +260,17 @@ impl CommonApi {
246
260
. error ( "Failed to send Request" )
247
261
}
248
262
249
- /// Sends the error to be displayed.
263
+ /// Sends the error to be displayed, no restart button will be shown .
250
264
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 < ( ) > {
251
270
self . request_sender
252
271
. send ( Request {
253
272
block_id : self . id ,
254
- cmd : RequestCmd :: SetError ( error) ,
273
+ cmd : RequestCmd :: SetError { error, restartable } ,
255
274
} )
256
275
. error ( "Failed to send Request" )
257
276
}
0 commit comments