Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kill process #148

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 44 additions & 53 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 56 additions & 32 deletions src/client/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ use crate::{
networking::get_available_sockets,
server::FaucetServerConfig,
};
use std::{ffi::OsStr, net::SocketAddr, path::Path, sync::atomic::AtomicBool, time::Duration};
use std::{
ffi::OsStr,
net::SocketAddr,
path::Path,
sync::atomic::{AtomicBool, AtomicU32, Ordering},
time::Duration,
};
use tokio::{process::Child, task::JoinHandle};
use tokio_stream::StreamExt;
use tokio_util::codec::{FramedRead, LinesCodec};
Expand Down Expand Up @@ -221,56 +227,74 @@ async fn check_if_online(addr: SocketAddr) -> bool {
const RECHECK_INTERVAL: Duration = Duration::from_millis(250);

pub struct WorkerChild {
_handle: JoinHandle<FaucetResult<()>>,
handle: Option<JoinHandle<FaucetResult<()>>>,
stopper: tokio::sync::mpsc::Sender<()>,
}

impl WorkerChild {
pub fn kill(&self) {
pub async fn kill(&mut self) {
let _ = self.stopper.try_send(());
self.wait_until_done().await;
}
pub async fn wait_until_done(&mut self) {
if let Some(handle) = self.handle.take() {
let _ = handle.await;
}
}
}

fn spawn_worker_task(config: WorkerConfig) -> WorkerChild {
let (stopper, mut rx) = tokio::sync::mpsc::channel(1);
let handle = tokio::spawn(async move {
let pid = AtomicU32::new(0);
loop {
let mut child = config.spawn_process(config);
let pid = child.id().expect("Failed to get plumber worker PID");
log::info!(target: "faucet", "Starting process {pid} for {target} on port {port}", port = config.addr.port(), target = config.target);
loop {
// Try to connect to the socket
let check_status = check_if_online(config.addr).await;
// If it's online, we can break out of the loop and start serving connections
if check_status {
log::info!(target: "faucet", "{target} is online and ready to serve connections", target = config.target);
config
.is_online
.store(check_status, std::sync::atomic::Ordering::SeqCst);
break;
}
// If it's not online but the child process has exited, we should break out of the loop
// and restart the process
if child.try_wait()?.is_some() {
break;
let child_manage_closure = || async {
let mut child = config.spawn_process(config);
pid.store(
child.id().expect("Failed to get plumber worker PID"),
Ordering::SeqCst,
);
log::info!(target: "faucet", "Starting process {pid} for {target} on port {port}", port = config.addr.port(), target = config.target, pid = pid.load(Ordering::SeqCst));
loop {
// Try to connect to the socket
let check_status = check_if_online(config.addr).await;
// If it's online, we can break out of the loop and start serving connections
if check_status {
log::info!(target: "faucet", "{target} is online and ready to serve connections", target = config.target);
config.is_online.store(check_status, Ordering::SeqCst);
break;
}
// If it's not online but the child process has exited, we should break out of the loop
// and restart the process
if child.try_wait()?.is_some() {
break;
}

tokio::time::sleep(RECHECK_INTERVAL).await;
}

tokio::time::sleep(RECHECK_INTERVAL).await;
}
child.wait().await
};

tokio::select! {
_ = child.wait() => (),
_ = rx.recv() => return FaucetResult::Ok(()),
status = child_manage_closure() => {
config
.is_online
.store(false, std::sync::atomic::Ordering::SeqCst);
log::error!(target: "faucet", "{target}'s process ({}) exited with status {}", pid.load(Ordering::SeqCst), status?, target = config.target);
},
_ = rx.recv() => break,
}
}
match pid.load(Ordering::SeqCst) {
0 => (), // If PID is 0 that means the process has not even started
pid => {
log::info!(target: "faucet", "{target}'s process ({pid}) killed", target = config.target)
}
let status = child.wait().await?;
config
.is_online
.store(false, std::sync::atomic::Ordering::SeqCst);
log::error!(target: "faucet", "{target}'s process ({}) exited with status {}", pid, status, target = config.target);
}
FaucetResult::Ok(())
});
WorkerChild {
_handle: handle,
handle: Some(handle),
stopper,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum FaucetError {
InvalidHeaderValues(hyper::header::InvalidHeaderValue),
Http(hyper::http::Error),
MissingArgument(&'static str),
DuplicateRoute(&'static str),
DuplicateRoute(String),
Utf8Coding,
BufferCapacity(tokio_tungstenite::tungstenite::error::CapacityError),
ProtocolViolation(tokio_tungstenite::tungstenite::error::ProtocolError),
Expand Down
Loading