diff --git a/src/rustylee/build_number.txt b/src/rustylee/build_number.txt index d55f9f7..c2807f7 100644 --- a/src/rustylee/build_number.txt +++ b/src/rustylee/build_number.txt @@ -1 +1 @@ -136 \ No newline at end of file +140 \ No newline at end of file diff --git a/src/rustylee/src/brain.rs b/src/rustylee/src/brain.rs index 349e564..c3180ac 100644 --- a/src/rustylee/src/brain.rs +++ b/src/rustylee/src/brain.rs @@ -1,7 +1,7 @@ -use tracing::info; +use tracing::{debug, info}; use std::sync::mpsc::{Receiver, Sender}; use crate::lifecycle::{LifeState, LifecycleCommand, LifecycleCommandResponse, LifecycleReceipt}; -use crate::lifecycle::LifeState::{Buried, Genisys}; +use crate::lifecycle::LifeState::{Dying, Buried, Genisys}; pub struct Brain { state:LifeState, @@ -36,14 +36,22 @@ impl Brain { } fn rest() { - std::thread::sleep(std::time::Duration::from_millis(1)) + debug!("Brain is resting."); + std::thread::sleep(std::time::Duration::from_millis(100)) } fn handle_divine_command(&mut self, command: LifecycleCommand) { info!("God has commanded {:?}", command); if self.can_transition_lifecycle(&command) { - self.set_lifecycle_state(&command); + if command.required_state == Dying + { + self.set_lifecycle_state(&command, Buried); + } else { + + self.set_lifecycle_state(&command, command.required_state); + } + return; } @@ -60,7 +68,7 @@ impl Brain { fn can_transition_lifecycle(&self, command: &LifecycleCommand) -> bool { - if (command.required_state == Buried) { + if command.required_state == Buried || command.required_state == Dying { return true } @@ -70,17 +78,19 @@ impl Brain { false } - fn set_lifecycle_state(&mut self, command: &LifecycleCommand) { - self.state = command.required_state; + fn set_lifecycle_state(&mut self, command: &LifecycleCommand, new_state: LifeState) { + self.state = new_state; self.report_to_god(LifecycleReceipt{ command: command.clone(), response: LifecycleCommandResponse::Ok, new_state: self.state }); } + fn report_to_god(&self, receipt: LifecycleReceipt) { info!("Reporting to God: Status = {:?}, NewState={:?}", receipt.response, receipt.new_state); let _ = self.divine_tx.send(receipt); } + } diff --git a/src/rustylee/src/main.rs b/src/rustylee/src/main.rs index 60fe977..d265926 100644 --- a/src/rustylee/src/main.rs +++ b/src/rustylee/src/main.rs @@ -1,27 +1,40 @@ pub mod lifecycle; pub mod brain; -use tracing::info; +use std::fmt::Alignment::Left; +use tracing::{info, debug}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::sync::mpsc; use std::sync::mpsc::{Receiver, Sender}; use std::thread; use std::time::{Duration, Instant}; - +use tracing_subscriber::registry; use crate::brain::Brain; use crate::lifecycle::{LifeState, LifecycleCommand, LifecycleReceipt}; fn main() { - let shutdown_requested = Arc::new(AtomicBool::new(false)); setup_logging(); info!("RustyLee: {}", env!("FULL_VERSION")); - setup_os_signal_handler(shutdown_requested.clone()); let (tx, rx) = spawn_brain(); + setup_os_signal_handler(tx.clone()); let_there_be_life(&tx, &rx); - wait_for_death(tx, rx); + + info!("God is watching."); + loop{ + if let Ok(receipt) = rx.recv() { + debug!("Brain status: {:?}", receipt.new_state); + + if receipt.new_state == LifeState::Dead || receipt.new_state == LifeState::Buried { + break; + } else { + thread::sleep(Duration::from_millis(100)); + } + } + } + // wait_for_death(tx, rx); info!("God: Brain has been buried. Shutting down."); } @@ -69,11 +82,14 @@ fn spawn_brain() -> (Sender, Receiver) { (to_brain_tx, from_brain_rx) } -fn setup_os_signal_handler(s: Arc) { +fn setup_os_signal_handler(tx: Sender) { info!("Setting up SIGTERM/SIGINT handling."); ctrlc::set_handler(move || { info!("[Signal] Shutdown signal caught!"); - s.store(true, Ordering::SeqCst); + let _ = tx.send(LifecycleCommand { + required_state: LifeState::Dying, + command_time: Instant::now(), + }); }).expect("Error setting signal handler."); }