Add restart flag

This commit is contained in:
elkowar 2021-08-14 15:06:19 +02:00
parent 47e1301180
commit 398717b782
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F
3 changed files with 13 additions and 4 deletions

View file

@ -38,7 +38,7 @@ async fn handle_connection(mut stream: tokio::net::UnixStream, evt_send: Unbound
evt_send.send(command)?; evt_send.send(command)?;
if let Some(mut response_recv) = maybe_response_recv { if let Some(mut response_recv) = maybe_response_recv {
log::info!("Waiting for response for IPC client"); log::debug!("Waiting for response for IPC client");
if let Ok(Some(response)) = tokio::time::timeout(Duration::from_millis(100), response_recv.recv()).await { if let Ok(Some(response)) = tokio::time::timeout(Duration::from_millis(100), response_recv.recv()).await {
let response = bincode::serialize(&response)?; let response = bincode::serialize(&response)?;
let result = &stream_write.write_all(&response).await; let result = &stream_write.write_all(&response).await;

View file

@ -66,6 +66,11 @@ fn main() {
// a running daemon is necessary for this command // a running daemon is necessary for this command
opts::Action::WithServer(action) if action.can_start_daemon() => { opts::Action::WithServer(action) if action.can_start_daemon() => {
if opts.restart {
let _ = handle_server_command(&paths, &ActionWithServer::KillServer, 1);
std::thread::sleep(std::time::Duration::from_millis(200));
}
// attempt to just send the command to a running daemon // attempt to just send the command to a running daemon
if let Err(err) = handle_server_command(&paths, &action, 5) { if let Err(err) = handle_server_command(&paths, &action, 5) {
// connecting to the daemon failed. Thus, start the daemon here! // connecting to the daemon failed. Thus, start the daemon here!
@ -78,7 +83,6 @@ fn main() {
let (command, response_recv) = action.into_daemon_command(); let (command, response_recv) = action.into_daemon_command();
// start the daemon and give it the command // start the daemon and give it the command
let fork_result = server::initialize_server(paths.clone(), Some(command))?; let fork_result = server::initialize_server(paths.clone(), Some(command))?;
let is_parent = fork_result == ForkResult::Parent; let is_parent = fork_result == ForkResult::Parent;
if let (Some(recv), true) = (response_recv, is_parent) { if let (Some(recv), true) = (response_recv, is_parent) {

View file

@ -15,6 +15,7 @@ use crate::{
pub struct Opt { pub struct Opt {
pub log_debug: bool, pub log_debug: bool,
pub show_logs: bool, pub show_logs: bool,
pub restart: bool,
pub config_path: Option<std::path::PathBuf>, pub config_path: Option<std::path::PathBuf>,
pub action: Action, pub action: Action,
} }
@ -33,6 +34,10 @@ struct RawOpt {
#[structopt(long = "logs", global = true)] #[structopt(long = "logs", global = true)]
show_logs: bool, show_logs: bool,
/// Restart the daemon completely before running the command
#[structopt(long = "restart", global = true)]
restart: bool,
#[structopt(subcommand)] #[structopt(subcommand)]
action: Action, action: Action,
} }
@ -148,8 +153,8 @@ impl Opt {
impl From<RawOpt> for Opt { impl From<RawOpt> for Opt {
fn from(other: RawOpt) -> Self { fn from(other: RawOpt) -> Self {
let RawOpt { action, log_debug, show_logs, config } = other; let RawOpt { action, log_debug, show_logs, config, restart } = other;
Opt { action, log_debug, show_logs, config_path: config } Opt { action, log_debug, show_logs, config_path: config, restart }
} }
} }