Refactors main function logic for clarity
Extracts core application setup, brain management, and OS signal handling into dedicated functions. Improves readability and modularity of the main execution flow. Removes an unused variable and updates the build number.
This commit is contained in:
parent
4acf07f389
commit
b8137feea5
2 changed files with 41 additions and 29 deletions
|
|
@ -1 +1 @@
|
||||||
133
|
135
|
||||||
|
|
@ -5,6 +5,7 @@ use tracing::info;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
|
use std::sync::mpsc::{Receiver, Sender};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
|
@ -12,57 +13,68 @@ use crate::brain::Brain;
|
||||||
use crate::lifecycle::{LifeState, LifecycleCommand};
|
use crate::lifecycle::{LifeState, LifecycleCommand};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _semantic_version = env!("CARGO_PKG_VERSION").to_string();
|
|
||||||
let shutdown_requested = Arc::new(AtomicBool::new(false));
|
let shutdown_requested = Arc::new(AtomicBool::new(false));
|
||||||
let s = shutdown_requested.clone();
|
|
||||||
|
|
||||||
setup_logging();
|
setup_logging();
|
||||||
info!("RustyLee: {}", env!("FULL_VERSION"));
|
info!("RustyLee: {}", env!("FULL_VERSION"));
|
||||||
|
|
||||||
ctrlc::set_handler(move || {
|
setup_os_signal_handler(shutdown_requested.clone());
|
||||||
info!("[Signal] Shutdown signal caught!");
|
let (tx, rx) = spawn_brain();
|
||||||
s.store(true, Ordering::SeqCst);
|
let_there_be_life(&tx, &rx);
|
||||||
}).expect("Error setting signal handler");
|
wait_for_death(tx, rx);
|
||||||
|
|
||||||
// 1. Setup the Divine Nerves
|
info!("God: Brain has been buried. Shutting down.");
|
||||||
let (to_brain_tx, to_brain_rx) = mpsc::channel::<LifecycleCommand>();
|
}
|
||||||
let (from_brain_tx, from_brain_rx) = mpsc::channel::<String>();
|
|
||||||
|
|
||||||
// 2. Spawn the Brain thread
|
fn wait_for_death(tx: Sender<LifecycleCommand>, rx: Receiver<String>) {
|
||||||
thread::spawn(move || {
|
info!("God: Resting for 10 seconds...");
|
||||||
let mut brain = Brain::new(to_brain_rx, from_brain_tx);
|
thread::sleep(Duration::from_secs(10));
|
||||||
brain.run();
|
|
||||||
|
info!("God: Commanding Burial...");
|
||||||
|
let _ = tx.send(LifecycleCommand {
|
||||||
|
required_state: LifeState::Buried,
|
||||||
|
command_time: Instant::now(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if let Ok(reply) = rx.recv() {
|
||||||
|
info!("Brain: {}", reply);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn let_there_be_life(tx: &Sender<LifecycleCommand>, rx: &Receiver<String>) {
|
||||||
// 3. Command: Genisys
|
// 3. Command: Genisys
|
||||||
info!("God: Commanding Genisys...");
|
info!("God: Commanding Genisys...");
|
||||||
let _ = to_brain_tx.send(LifecycleCommand {
|
let _ = tx.send(LifecycleCommand {
|
||||||
required_state: LifeState::Genisys,
|
required_state: LifeState::Genisys,
|
||||||
command_time: Instant::now(),
|
command_time: Instant::now(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait for Brain's "OK"
|
// Wait for Brain's "OK"
|
||||||
if let Ok(reply) = from_brain_rx.recv() {
|
if let Ok(reply) = rx.recv() {
|
||||||
info!("Brain: {}", reply);
|
info!("Brain: {}", reply);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 4. Wait two seconds
|
fn spawn_brain() -> (Sender<LifecycleCommand>, Receiver<String>) {
|
||||||
info!("God: Resting for 2 seconds...");
|
let (to_brain_tx, to_brain_rx) = mpsc::channel::<LifecycleCommand>();
|
||||||
thread::sleep(Duration::from_secs(2));
|
let (from_brain_tx, from_brain_rx) = mpsc::channel::<String>();
|
||||||
|
|
||||||
// 5. Command: Buried
|
// The Brain takes one half of each pair
|
||||||
info!("God: Commanding Burial...");
|
thread::spawn(move || {
|
||||||
let _ = to_brain_tx.send(LifecycleCommand {
|
let mut brain = Brain::new(to_brain_rx, from_brain_tx);
|
||||||
required_state: LifeState::Buried,
|
brain.run();
|
||||||
command_time: Instant::now(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait for Brain's final "OK"
|
// Main gets the remaining halves (the "Remote Control")
|
||||||
if let Ok(reply) = from_brain_rx.recv() {
|
(to_brain_tx, from_brain_rx)
|
||||||
info!("Brain: {}", reply);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
info!("God: Brain has been buried. Shutting down.");
|
fn setup_os_signal_handler(s: Arc<AtomicBool>) {
|
||||||
|
info!("Setting up SIGTERM/SIGINT handling.");
|
||||||
|
ctrlc::set_handler(move || {
|
||||||
|
info!("[Signal] Shutdown signal caught!");
|
||||||
|
s.store(true, Ordering::SeqCst);
|
||||||
|
}).expect("Error setting signal handler.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup_logging() {
|
fn setup_logging() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue