summaryrefslogtreecommitdiffstats
path: root/src/shell/history/job.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/shell/history/job.rs')
-rw-r--r--src/shell/history/job.rs148
1 files changed, 1 insertions, 147 deletions
diff --git a/src/shell/history/job.rs b/src/shell/history/job.rs
index d3d112a..5c73f4d 100644
--- a/src/shell/history/job.rs
+++ b/src/shell/history/job.rs
@@ -1,10 +1,6 @@
use crate::shell::prelude::*;
-pub struct Job {
- state: std::sync::Arc<std::sync::Mutex<State>>,
- start_time: time::OffsetDateTime,
- start_instant: std::time::Instant,
-}
+pub struct Job {}
impl Job {
pub fn new(
@@ -13,8 +9,6 @@ impl Job {
pts: &pty_process::Pts,
event_w: crate::shell::event::Writer,
) -> Result<Self> {
- let start_time = time::OffsetDateTime::now_utc();
- let start_instant = std::time::Instant::now();
let (child, fh) = spawn_command(cmdline, &env, pts)?;
let state = std::sync::Arc::new(std::sync::Mutex::new(
State::Running((0, 0)),
@@ -66,144 +60,4 @@ impl Job {
}
});
}
-
- async fn task(
- mut child: tokio::process::Child,
- fh: std::fs::File,
- state: std::sync::Arc<std::sync::Mutex<State>>,
- env: Env,
- event_w: crate::shell::event::Writer,
- ) {
- enum Res {
- Read(crate::runner::Event),
- Exit(std::io::Result<std::process::ExitStatus>),
- }
-
- let (read_w, read_r) = tokio::sync::mpsc::unbounded_channel();
- tokio::task::spawn_blocking(move || loop {
- let event = bincode::deserialize_from(&fh);
- match event {
- Ok(event) => {
- read_w.send(event).unwrap();
- }
- Err(e) => {
- match &*e {
- bincode::ErrorKind::Io(io_e) => {
- assert!(
- io_e.kind()
- == std::io::ErrorKind::UnexpectedEof
- );
- }
- e => {
- panic!("{}", e);
- }
- }
- break;
- }
- }
- });
-
- let mut stream: futures_util::stream::SelectAll<_> = [
- tokio_stream::wrappers::UnboundedReceiverStream::new(read_r)
- .map(Res::Read)
- .boxed(),
- futures_util::stream::once(child.wait())
- .map(Res::Exit)
- .boxed(),
- ]
- .into_iter()
- .collect();
- let mut exit_status = None;
- let mut new_env = None;
- while let Some(res) = stream.next().await {
- match res {
- Res::Read(event) => match event {
- crate::runner::Event::RunPipeline(new_span) => {
- // we could just update the span in place here, but we
- // do this as an event so that we can also trigger a
- // refresh
- event_w.send(Event::ChildRunPipeline(
- env.idx(),
- new_span,
- ));
- }
- crate::runner::Event::Suspend => {
- event_w.send(Event::ChildSuspend(env.idx()));
- }
- crate::runner::Event::Exit(env) => {
- new_env = Some(env);
- }
- },
- Res::Exit(status) => {
- exit_status = Some(status.unwrap());
- }
- }
- }
- *state.lock().unwrap() =
- State::Exited(ExitInfo::new(exit_status.unwrap()));
- event_w.send(Event::ChildExit(env.idx(), new_env));
- }
-}
-
-pub enum State {
- Running((usize, usize)),
- Exited(ExitInfo),
-}
-
-impl State {
- pub fn exit_info(&self) -> Option<&ExitInfo> {
- match self {
- Self::Running(_) => None,
- Self::Exited(exit_info) => Some(exit_info),
- }
- }
-
- pub fn running(&self) -> bool {
- self.exit_info().is_none()
- }
-}
-
-pub struct ExitInfo {
- status: std::process::ExitStatus,
- instant: std::time::Instant,
-}
-
-impl ExitInfo {
- fn new(status: std::process::ExitStatus) -> Self {
- Self {
- status,
- instant: std::time::Instant::now(),
- }
- }
-
- pub fn status(&self) -> std::process::ExitStatus {
- self.status
- }
-
- pub fn instant(&self) -> &std::time::Instant {
- &self.instant
- }
-}
-
-fn spawn_command(
- cmdline: &str,
- env: &Env,
- pts: &pty_process::Pts,
-) -> Result<(tokio::process::Child, std::fs::File)> {
- let mut cmd = pty_process::Command::new(std::env::current_exe()?);
- cmd.args(&["-c", cmdline, "--status-fd", "3"]);
- env.apply(&mut cmd);
- let (from_r, from_w) = nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC)?;
- // Safety: from_r was just opened above and is not used anywhere else
- let fh = unsafe { std::fs::File::from_raw_fd(from_r) };
- // Safety: dup2 is an async-signal-safe function
- unsafe {
- cmd.pre_exec(move || {
- nix::unistd::dup2(from_w, 3)?;
- Ok(())
- });
- }
- let child = cmd.spawn(pts)?;
- nix::unistd::close(from_w)?;
- Ok((child, fh))
}