Skip to content

Commit

Permalink
handle_worker_close() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj authored and FlorentinDUBOIS committed Jul 13, 2022
1 parent da3cdc0 commit d74ff84
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
64 changes: 35 additions & 29 deletions bin/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,40 @@ impl CommandServer {
}
Ok(())
}

async fn handle_worker_close(&mut self, id: u32) {
// put all this in a method handle_worker_close
info!("removing worker {}", id);

if let Some(w) = self.workers.iter_mut().filter(|w| w.id == id).next() {
// In case a worker crashes and should be restarted
if self.config.worker_automatic_restart && w.run_state == RunState::Running {
info!("Automatically restarting worker {}", id);
match self.restart_worker(id).await {
Ok(()) => info!("Worker {} has automatically restarted!", id),
Err(e) => error!("Could not restart worker {}: {}", id, e),
}
return;
}

info!("Closing the worker {}.", w.id);
if !w.the_pid_is_alive() {
info!("Worker {} is dead, setting to Stopped.", w.id);
w.run_state = RunState::Stopped;
return;
}

info!("Worker {} is not dead but should be. Let's kill it.", w.id);

match kill(Pid::from_raw(w.pid), Signal::SIGKILL) {
Ok(()) => {
info!("Worker {} was successfuly killed", id);
w.run_state = RunState::Stopped;
}
Err(e) => error!("failed to kill the worker process: {:?}", e),
}
}
}
}

pub fn start(
Expand Down Expand Up @@ -643,35 +677,7 @@ impl CommandServer {
}
}
CommandMessage::WorkerClose { id } => {
// put all this in a method handle_worker_close
info!("removing worker {}", id);
if let Some(w) = self.workers.iter_mut().filter(|w| w.id == id).next() {
// In case a worker crashes and should be restarted
if self.config.worker_automatic_restart && w.run_state == RunState::Running
{
match self.restart_worker(id).await {
Ok(()) => {}
Err(e) => error!("Could not restart worker {}: {}", id, e),
}

// is this wise?
return;
}

info!("Closing the worker {}.", w.id);

if w.the_pid_is_alive() {
info!("Worker {} is not dead but should be. Let's kill it.", w.id);

match kill(Pid::from_raw(w.pid), Signal::SIGKILL) {
Ok(()) => info!("Successfully killed the worker process {}", w.id),
Err(e) => error!("failed to kill the worker process: {}", e),
}
} else {
info!("Worker {} is dead, setting to Stopped.", w.id);
}
w.run_state = RunState::Stopped;
}
self.handle_worker_close(id).await;
}
CommandMessage::WorkerResponse { id, message } => {
debug!("worker {} sent back {:?}", id, message);
Expand Down
4 changes: 1 addition & 3 deletions bin/src/command/orders.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use anyhow::{bail, Context};
use anyhow::Context;
use futures::channel::mpsc::*;
use futures::{SinkExt, StreamExt};
use nix::sys::signal::{kill, Signal};
use nix::unistd::Pid;
use nom::{Err, HexDisplay, IResult, Offset};
use serde_json;
use std::collections::{BTreeMap, HashSet};
Expand Down

0 comments on commit d74ff84

Please sign in to comment.