Gracefully restart after 3 seconds on error

timeout_old
Tassilo Horn 4 years ago
parent f14d70a4f0
commit d7f062e0ae
  1. 74
      src/demon.rs

@ -14,44 +14,60 @@ use std::time::{SystemTime, UNIX_EPOCH};
use swayipc as s; use swayipc as s;
use swayipc::reply as r; use swayipc::reply as r;
fn connect_and_subscribe() -> s::Fallible<s::EventIterator> {
s::Connection::new()?
.subscribe(&[s::EventType::Window, s::EventType::Workspace])
}
pub fn monitor_sway_events( pub fn monitor_sway_events(
extra_props: Arc<RwLock<HashMap<i64, ipc::ExtraProps>>>, extra_props: Arc<RwLock<HashMap<i64, ipc::ExtraProps>>>,
) { ) {
'reset: loop { 'reset: loop {
println!("Connecting to sway for subscribing to events..."); println!("Connecting to sway for subscribing to events...");
let iter = s::Connection::new() match connect_and_subscribe() {
.expect("Could not connect!") Err(err) => {
.subscribe(&[s::EventType::Window, s::EventType::Workspace]) eprintln!("Could not connect and subscribe: {}", err);
.expect("Could not subscribe to window and workspace events."); std::thread::sleep(std::time::Duration::from_secs(3));
break 'reset;
for ev_result in iter { }
let handled; Ok(iter) => {
match ev_result { for ev_result in iter {
Ok(ev) => match ev { let handled;
r::Event::Window(win_ev) => { match ev_result {
let extra_props_clone = extra_props.clone(); Ok(ev) => match ev {
handled = r::Event::Window(win_ev) => {
handle_window_event(win_ev, extra_props_clone); let extra_props_clone = extra_props.clone();
handled = handle_window_event(
win_ev,
extra_props_clone,
);
}
r::Event::Workspace(ws_ev) => {
let extra_props_clone = extra_props.clone();
handled = handle_workspace_event(
ws_ev,
extra_props_clone,
);
}
_ => handled = false,
},
Err(e) => {
eprintln!("Error while receiving events: {}", e);
std::thread::sleep(std::time::Duration::from_secs(
3,
));
eprintln!("Resetting!");
break 'reset;
}
} }
r::Event::Workspace(ws_ev) => { if handled {
let extra_props_clone = extra_props.clone(); println!(
handled = "New extra_props state:\n{:#?}",
handle_workspace_event(ws_ev, extra_props_clone); *extra_props.read().unwrap()
);
} }
_ => handled = false,
},
Err(e) => {
eprintln!("Error while receiving events: {}", e);
eprintln!("Resetting!");
break 'reset;
} }
} }
if handled {
println!(
"New extra_props state:\n{:#?}",
*extra_props.read().unwrap()
);
}
} }
} }
} }

Loading…
Cancel
Save