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

Redirect stdio #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
63 changes: 61 additions & 2 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,34 @@ impl Runnable for Process {
thread::sleep(time::Duration::from_millis(10));

let mut p = lockable.lock().unwrap();
// p is a MutexGuard<_Process>, so each access to fields of _Process calls
// deref / deref_mut to get the underlying _Process. To avoid borrowing
// conflicts, get a reference to the underlying _Process struct once.
let p = &mut *p;

// Handle stdio
if let Some(ref mut stdout) = p.stdout_reader {
for line in stdout.lines() {
info!("{:?}", line)
p.stdout_sender
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After fubarnetes/fubarnetes#1 is solved, this should ideally be some PUB/SUB or similar message queue.

.send(line.unwrap())
.expect("Could not send stdout");
}
}

if let Some(ref mut stderr) = p.stderr_reader {
for line in stderr.lines() {
error!("{:?}", line)
p.stderr_sender
.send(line.unwrap())
.expect("Could not send stderr");
}
}

if let Some(ref mut stdin) = p.stdin_writer {
if let Ok(input) = p.stdin_receiver.try_recv() {
info!("received: {}", input);
stdin.write_all(input.as_bytes())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this probably a PULL or equivalent.

.expect("Could not write to stdin");
}
}

// Check if the process has already exited
Expand Down Expand Up @@ -235,3 +248,49 @@ impl Runnable for Process {
self.lock().unwrap().handle = handle;
}
}


#[cfg(test)]
mod tests {
use std::time;
use super::*;
extern crate pretty_env_logger;

macro_rules! sleep {
( $ms:expr ) => ({
let duration = time::Duration::from_millis($ms);
thread::sleep(duration);
})
}

#[test]
fn test_run_ls_max_retries() {
let interval = 1000;
let p = from_argv!(["ls", "-la"], "/", interval);

p.clone().launch();

sleep!(interval*p.lock().unwrap().max_restart_count);
sleep!(interval);

info!("{:?}",p);
}

#[test]
fn test_exitstatus() {
let p = from_argv!(["false"]);
p.lock().unwrap().max_restart_count = 0;
p.clone().launch();
sleep!(500);
assert_eq!(p.lock().unwrap().exit_status, Some(1));
}

#[test]
fn test_echo() {
let p = from_argv!(["echo", "test"], "/");
p.lock().unwrap().max_restart_count = 0;
p.clone().launch();
sleep!(500);
assert_eq!(p.lock().unwrap().stdout.recv(), Ok("test".to_string()));
}
}