Make sure kill and flush works

This commit is contained in:
Wildan M 2025-11-03 23:05:36 +07:00
parent e4403cb3d9
commit 5e8a162965
3 changed files with 33 additions and 9 deletions

View File

@ -6,6 +6,7 @@ use cookbook::cook::fetch::{fetch, fetch_offline};
use cookbook::cook::fs::{create_target_dir, run_command};
use cookbook::cook::package::package;
use cookbook::cook::pty::{PtyOut, UnixSlavePty, flush_pty, setup_pty};
use cookbook::cook::script::KILL_ALL_PID;
use cookbook::cook::tree::{display_tree_entry, format_size};
use cookbook::log_to_pty;
use cookbook::recipe::{BuildKind, CookRecipe};
@ -861,7 +862,7 @@ fn run_tui_cook(
);
if let Some(log_path) = cooker_config.logs_dir.as_ref() {
if let Err(err_ctx) = &handler {
log_to_pty!(&logger, "{:?}", err_ctx)
log_to_pty!(&logger, "\n{:?}", err_ctx)
}
flush_pty(&mut logger);
let log_path = log_path.join(format!("{}.log", name.as_str()));
@ -955,7 +956,7 @@ fn run_tui_cook(
&& handler.is_err()
{
if let Err(err_ctx) = &handler {
log_to_pty!(&logger, "{:?}", err_ctx)
log_to_pty!(&logger, "\n{:?}", err_ctx)
}
flush_pty(&mut logger);
let log_path = log_path.join(format!("{}.log", name.as_str()));
@ -1308,12 +1309,11 @@ fn handle_main_event(app: &mut TuiApp, event: &Event) {
Key::Char('c') => {
// as compilers still running, we use this way to stop it
let pid = std::process::id();
Command::new("pkill")
.arg("-9")
.arg("-P")
.arg(pid.to_string())
Command::new("bash")
.arg("-c")
.arg(KILL_ALL_PID.replace("$PID", &pid.to_string()))
.spawn()
.expect("unable to spawn pkill");
.expect("unable to spawn kill");
}
Key::Up => {
app.auto_scroll = false;

View File

@ -6,6 +6,7 @@ use std::os::fd::FromRawFd;
use std::os::unix::io::AsRawFd;
use std::os::unix::process::CommandExt;
use std::process::Child;
use std::time::Duration;
use std::{io, mem, ptr};
use std::{
io::{PipeReader, PipeWriter},
@ -60,8 +61,11 @@ pub fn flush_pty(logger: &mut PtyOut) {
let Some((pty, file)) = logger else {
return;
};
// Not sure if flush actually working
let _ = pty.flush();
std::thread::sleep(Duration::from_millis(100));
let _ = file.flush();
std::thread::sleep(Duration::from_millis(100));
}
pub fn spawn_to_pipe(command: &mut Command, stdout_pipe: &PtyOut) -> Result<Child, Error> {
@ -289,6 +293,10 @@ impl PtyFd {
Ok(child)
}
fn flush(&mut self) -> std::io::Result<()> {
self.0.flush()
}
}
/// Represents the master end of a pty.
@ -326,8 +334,8 @@ impl UnixSlavePty {
fn spawn_command(&self, builder: &mut Command) -> Result<std::process::Child, Error> {
Ok(self.fd.spawn_command(builder)?)
}
fn flush(&self) -> Result<(), anyhow::Error> {
Ok(self.fd.as_file()?.flush()?)
fn flush(&mut self) -> Result<(), anyhow::Error> {
Ok(self.fd.flush()?)
}
}

View File

@ -352,3 +352,19 @@ if [ "$(git rev-parse HEAD)" != "$(git rev-parse $ORIGIN_BRANCH)" ]
then
git checkout -B "$(echo "$ORIGIN_BRANCH" | cut -d / -f 2-)" "$ORIGIN_BRANCH"
fi"#;
pub static KILL_ALL_PID: &str = r#"
THISPID=$$
CHILDREN=$(ps -o pid= --ppid $PID | grep -v $THISPID);
ALL_DESCENDANTS='';
while [ -n "$CHILDREN" ]; do
ALL_DESCENDANTS="$ALL_DESCENDANTS $CHILDREN";
CHILDREN=$(ps -o pid= --ppid $(echo $CHILDREN) | tr '\n' ' ');
done;
if [ -n "$ALL_DESCENDANTS" ]; then
kill -9 $ALL_DESCENDANTS;
fi
"#;