Add logging for api local simple & pty processes

pull/137/head
Chip Senkbeil 2 years ago
parent 30548cdbfb
commit ec95f573b9
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -6,6 +6,7 @@ use crate::{
constants::{MAX_PIPE_CHUNK_SIZE, READ_PAUSE_MILLIS}, constants::{MAX_PIPE_CHUNK_SIZE, READ_PAUSE_MILLIS},
data::Environment, data::Environment,
}; };
use log::*;
use portable_pty::{CommandBuilder, MasterPty, PtySize as PortablePtySize}; use portable_pty::{CommandBuilder, MasterPty, PtySize as PortablePtySize};
use std::{ use std::{
ffi::OsStr, ffi::OsStr,
@ -41,6 +42,8 @@ impl PtyProcess {
I: IntoIterator<Item = S2>, I: IntoIterator<Item = S2>,
S2: AsRef<OsStr>, S2: AsRef<OsStr>,
{ {
let id = rand::random();
// Establish our new pty for the given size // Establish our new pty for the given size
let pty_system = portable_pty::native_pty_system(); let pty_system = portable_pty::native_pty_system();
let pty_pair = pty_system let pty_pair = pty_system
@ -111,6 +114,10 @@ impl PtyProcess {
loop { loop {
match (child.try_wait(), kill_rx.try_recv()) { match (child.try_wait(), kill_rx.try_recv()) {
(Ok(Some(status)), _) => { (Ok(Some(status)), _) => {
trace!(
"Pty process {id} has exited: success = {}",
status.success()
);
// TODO: Keep track of io error // TODO: Keep track of io error
let _ = wait_tx let _ = wait_tx
.send(ExitStatus { .send(ExitStatus {
@ -121,11 +128,13 @@ impl PtyProcess {
break; break;
} }
(_, Ok(_)) => { (_, Ok(_)) => {
trace!("Pty process {id} received kill request");
// TODO: Keep track of io error // TODO: Keep track of io error
let _ = wait_tx.kill().await; let _ = wait_tx.kill().await;
break; break;
} }
(Err(x), _) => { (Err(x), _) => {
trace!("Pty process {id} failed to wait");
// TODO: Keep track of io error // TODO: Keep track of io error
let _ = wait_tx.send(x).await; let _ = wait_tx.send(x).await;
break; break;
@ -140,7 +149,7 @@ impl PtyProcess {
}); });
Ok(Self { Ok(Self {
id: rand::random(), id,
pty_master: PtyProcessMaster(Arc::new(Mutex::new(pty_master))), pty_master: PtyProcessMaster(Arc::new(Mutex::new(pty_master))),
stdin: Some(Box::new(stdin_tx)), stdin: Some(Box::new(stdin_tx)),
stdout: Some(Box::new(stdout_rx)), stdout: Some(Box::new(stdout_rx)),

@ -3,6 +3,7 @@ use super::{
ProcessKiller, WaitRx, ProcessKiller, WaitRx,
}; };
use crate::data::Environment; use crate::data::Environment;
use log::*;
use std::{ffi::OsStr, path::PathBuf, process::Stdio}; use std::{ffi::OsStr, path::PathBuf, process::Stdio};
use tokio::{io, process::Command, sync::mpsc, task::JoinHandle}; use tokio::{io, process::Command, sync::mpsc, task::JoinHandle};
@ -34,6 +35,7 @@ impl SimpleProcess {
I: IntoIterator<Item = S2>, I: IntoIterator<Item = S2>,
S2: AsRef<OsStr>, S2: AsRef<OsStr>,
{ {
let id = rand::random();
let mut child = { let mut child = {
let mut command = Command::new(program); let mut command = Command::new(program);
@ -65,15 +67,34 @@ impl SimpleProcess {
tokio::spawn(async move { tokio::spawn(async move {
tokio::select! { tokio::select! {
_ = kill_rx.recv() => { _ = kill_rx.recv() => {
trace!("Pty process {id} received kill request");
let status = match child.kill().await { let status = match child.kill().await {
Ok(_) => ExitStatus::killed(), Ok(_) => ExitStatus::killed(),
Err(x) => ExitStatus::from(x), Err(x) => ExitStatus::from(x),
}; };
trace!(
"Simple process {id} has exited: success = {}, code = {}",
status.success,
status.code.map(|code| code.to_string())
.unwrap_or_else(|| "<terminated>".to_string()),
);
// TODO: Keep track of io error // TODO: Keep track of io error
let _ = wait_tx.send(status).await; let _ = wait_tx.send(status).await;
} }
status = child.wait() => { status = child.wait() => {
match &status {
Ok(status) => trace!(
"Simple process {id} has exited: success = {}, code = {}",
status.success(),
status.code()
.map(|code| code.to_string())
.unwrap_or_else(|| "<terminated>".to_string()),
),
Err(_) => trace!("Simple process {id} failed to wait"),
}
// TODO: Keep track of io error // TODO: Keep track of io error
let _ = wait_tx.send(status).await; let _ = wait_tx.send(status).await;
} }
@ -81,7 +102,7 @@ impl SimpleProcess {
}); });
Ok(Self { Ok(Self {
id: rand::random(), id,
stdin: Some(Box::new(stdin_ch)), stdin: Some(Box::new(stdin_ch)),
stdout: Some(Box::new(stdout_ch)), stdout: Some(Box::new(stdout_ch)),
stderr: Some(Box::new(stderr_ch)), stderr: Some(Box::new(stderr_ch)),

Loading…
Cancel
Save