-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make krun-server wait for child processes
Continue to accept and handle incoming connections, until the server is idle and all child processes have exited.
- Loading branch information
1 parent
d0179f3
commit 397b12b
Showing
7 changed files
with
132 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,96 @@ | ||
use std::net::TcpListener; | ||
use std::os::fd::AsRawFd; | ||
use std::panic; | ||
use std::os::unix::process::ExitStatusExt as _; | ||
use std::process::Command; | ||
use std::sync::mpsc::{channel, TryRecvError}; | ||
use std::sync::{Arc, Condvar, Mutex}; | ||
|
||
use anyhow::{Context, Result}; | ||
use anyhow::Result; | ||
use krun_server::cli_options::options; | ||
use krun_server::server::start_server; | ||
use nix::sys::socket::{shutdown, Shutdown}; | ||
use log::debug; | ||
|
||
fn main() -> Result<()> { | ||
env_logger::init(); | ||
|
||
let options = options().run(); | ||
|
||
let listener = TcpListener::bind(format!("0.0.0.0:{}", options.server_port))?; | ||
let listener_fd = listener.as_raw_fd(); | ||
let (child_tx, child_rx) = channel(); | ||
let idle_pair = Arc::new((Mutex::new(true), Condvar::new())); | ||
|
||
let server_thread = start_server(listener); | ||
start_server(listener, child_tx, Arc::clone(&idle_pair)); | ||
|
||
Command::new(&options.command) | ||
match Command::new(&options.command) | ||
.args(options.command_args) | ||
.status() | ||
.with_context(|| format!("Failed to execute command {:?}", options.command))?; | ||
|
||
shutdown(listener_fd, Shutdown::Both)?; | ||
if let Err(err) = server_thread.join() { | ||
panic::resume_unwind(err); | ||
{ | ||
Ok(status) => { | ||
if !status.success() { | ||
if let Some(code) = status.code() { | ||
eprintln!( | ||
"{:?} process exited with status code: {code}", | ||
options.command | ||
); | ||
} else { | ||
eprintln!( | ||
"{:?} process terminated by signal: {}", | ||
options.command, | ||
status | ||
.signal() | ||
.expect("either one of status code or signal should be set") | ||
); | ||
} | ||
} | ||
}, | ||
Err(err) => { | ||
eprintln!( | ||
"Failed to execute {:?} as child process: {err}", | ||
options.command | ||
); | ||
}, | ||
} | ||
|
||
Ok(()) | ||
println!("Waiting for other commands launched through this krun server to exit..."); | ||
println!("Press Ctrl+C to force quit"); | ||
|
||
let (idle_lock, idle_cvar) = &*idle_pair; | ||
|
||
loop { | ||
let mut child = { | ||
let _guard = idle_cvar | ||
.wait_while(idle_lock.lock().unwrap(), |idle| !*idle) | ||
.unwrap(); | ||
match child_rx.try_recv() { | ||
Ok(child) => child, | ||
Err(TryRecvError::Empty) => { | ||
// Server is idle and no more child processes. We're done. | ||
return Ok(()); | ||
}, | ||
Err(TryRecvError::Disconnected) => { | ||
panic!("child_tx in server thread should not be gone"); | ||
}, | ||
} | ||
}; | ||
let child_pid = child.id(); | ||
match child.wait() { | ||
Ok(status) => { | ||
debug!("child process {child_pid} exited"); | ||
if !status.success() { | ||
if let Some(code) = status.code() { | ||
eprintln!("child process {child_pid} exited with status code: {code}"); | ||
} else { | ||
eprintln!( | ||
"child process {child_pid} terminated by signal: {}", | ||
status | ||
.signal() | ||
.expect("either one of status code or signal should be set") | ||
); | ||
} | ||
} | ||
}, | ||
Err(err) => { | ||
eprintln!("Failed to wait for child process {child_pid} to exit: {err}"); | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters