From 2f527d353284e12779409dfd6fb1388f533cdf9e Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 24 May 2018 20:53:10 -0600 Subject: [PATCH] debugging heartbeat and buggy worker --- Chapter09/Cargo.toml | 8 ++++++++ Chapter09/debugging_buggy_worker.rs | 10 +++++++++ Chapter09/debugging_heartbeat.rs | 32 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 Chapter09/debugging_buggy_worker.rs create mode 100644 Chapter09/debugging_heartbeat.rs diff --git a/Chapter09/Cargo.toml b/Chapter09/Cargo.toml index 562f5b3..fa96ea8 100644 --- a/Chapter09/Cargo.toml +++ b/Chapter09/Cargo.toml @@ -71,3 +71,11 @@ path = "performance_reference.rs" [[bin]] name = "debugging_result" path = "debugging_result.rs" + +[[bin]] +name = "debugging_heartbeat" +path = "debugging_heartbeat.rs" + +[[bin]] +name = "debugging_buggy_worker" +path = "debugging_buggy_worker.rs" diff --git a/Chapter09/debugging_buggy_worker.rs b/Chapter09/debugging_buggy_worker.rs new file mode 100644 index 0000000..c03e812 --- /dev/null +++ b/Chapter09/debugging_buggy_worker.rs @@ -0,0 +1,10 @@ +use std::{thread,time,process}; + +fn main() { + let life_expectancy = process::id() % 8; + let t = time::Duration::from_millis(1000); + for _ in 0..life_expectancy { + thread::sleep(t); + } + println!("process {} dies unexpectedly.", process::id()); +} diff --git a/Chapter09/debugging_heartbeat.rs b/Chapter09/debugging_heartbeat.rs new file mode 100644 index 0000000..836d5ca --- /dev/null +++ b/Chapter09/debugging_heartbeat.rs @@ -0,0 +1,32 @@ +use std::process::Command; +use std::env::current_exe; +use std::{thread,time}; + +fn main() { + let path = current_exe() + .expect("could not find current executable"); + let path = path.with_file_name("debugging_buggy_worker"); + + let mut children = Vec::new(); + for _ in 0..3 { + children.push( + Command::new(path.as_os_str()) + .spawn() + .expect("failed to spawn child") + ); + } + + let t = time::Duration::from_millis(1000); + loop { + thread::sleep(t); + for ci in 0..children.len() { + let is_dead = children[ci].try_wait().expect("failed to try_wait"); + if let Some(_exit_code) = is_dead { + children[ci] = Command::new(path.as_os_str()) + .spawn() + .expect("failed to spawn child"); + println!("starting a new process from parent."); + } + } + } +}