Skip to main content

runtime_control_story/
runtime_control_story.rs

1//! Demonstrates an operator control flow against a running supervisor.
2
3// Import the YAML configuration loader.
4use rust_supervisor::config::loader::load_config_state;
5// Import the command result type.
6use rust_supervisor::control::command::CommandResult;
7// Import child identifiers and supervisor paths.
8use rust_supervisor::id::types::{ChildId, SupervisorPath};
9// Import the supervisor runtime entry point.
10use rust_supervisor::runtime::supervisor::Supervisor;
11
12// Define the shared example result type.
13type ExampleResult = Result<(), rust_supervisor::error::types::SupervisorError>;
14
15// Use the Tokio runtime for the asynchronous example.
16#[tokio::main]
17// Return typed supervisor errors from the example.
18/// Runs the runtime control story example.
19async fn main() -> ExampleResult {
20    // Load centralized YAML configuration.
21    let state = load_config_state("examples/config/supervisor.yaml")?;
22    // Derive the supervisor specification from configuration.
23    let spec = state.to_supervisor_spec()?;
24    // Start the supervisor runtime from the specification.
25    let handle = Supervisor::start(spec).await?;
26    // Subscribe to runtime event text.
27    let mut events = handle.subscribe_events();
28    // Build the child identifier used by operator commands.
29    let child_id = ChildId::new("market_feed");
30
31    // Send an add child command through the control handle.
32    let add = handle
33        // Add a manifest under the root supervisor.
34        .add_child(
35            // Target the root supervisor.
36            SupervisorPath::root(),
37            // Provide the child manifest text.
38            "id=market_feed kind=AsyncWorker readiness=Explicit",
39            // Provide the requesting actor.
40            "operator",
41            // Provide the audit reason.
42            "attach market feed during incident rehearsal",
43            // Finish the add child arguments.
44        )
45        // Wait for the add child command result.
46        .await?;
47    // Print the add child result.
48    print_result("add_child", add);
49
50    // Print the pause child result.
51    print_result(
52        // Label the pause result.
53        "pause_child",
54        // Send the pause child command.
55        handle
56            // Pause automatic governance.
57            .pause_child(child_id.clone(), "operator", "stop automatic restart")
58            // Wait for the pause result.
59            .await?,
60        // Finish the pause result print call.
61    );
62    // Print the resume child result.
63    print_result(
64        // Label the resume result.
65        "resume_child",
66        // Send the resume child command.
67        handle
68            // Resume lifecycle governance.
69            .resume_child(child_id.clone(), "operator", "resume lifecycle governance")
70            // Wait for the resume result.
71            .await?,
72        // Finish the resume result print call.
73    );
74    // Print the quarantine child result.
75    print_result(
76        // Label the quarantine result.
77        "quarantine_child",
78        // Send the quarantine child command.
79        handle
80            // Quarantine the child for manual investigation.
81            .quarantine_child(child_id, "operator", "manual investigation")
82            // Wait for the quarantine result.
83            .await?,
84        // Finish the quarantine result print call.
85    );
86    // Print the current state result.
87    print_result("current_state", handle.current_state().await?);
88
89    // Drain already available runtime events.
90    while let Ok(event) = events.try_recv() {
91        // Print one runtime event.
92        println!("event={event}");
93        // Finish the event drain loop.
94    }
95
96    // Print the shutdown result.
97    print_result(
98        // Label the shutdown result.
99        "shutdown_tree",
100        // Send the shutdown command.
101        handle
102            // Request tree shutdown.
103            .shutdown_tree("operator", "runtime control story complete")
104            // Wait for the shutdown result.
105            .await?,
106        // Finish the shutdown result print call.
107    );
108
109    // Finish the example successfully.
110    Ok(())
111    // End the runtime control example.
112}
113
114// Print a command result with a label.
115/// Prints one labeled command result.
116fn print_result(label: &str, result: CommandResult) {
117    // Print the structured command result.
118    println!("{label}={result:#?}");
119    // End the print helper.
120}