mecha10_runtime/node.rs
1//! Node runner trait and types
2
3use crate::health::HealthStatus;
4use anyhow::Result;
5use async_trait::async_trait;
6
7/// Trait for runnable nodes
8///
9/// Implement this trait for each node type to integrate with the runtime supervisor.
10///
11/// # Example
12///
13/// ```rust
14/// use mecha10_runtime::prelude::*;
15///
16/// struct CameraNode {
17/// frame_count: u64,
18/// }
19///
20/// #[async_trait]
21/// impl NodeRunner for CameraNode {
22/// fn name(&self) -> &str {
23/// "camera"
24/// }
25///
26/// async fn run(&mut self) -> anyhow::Result<()> {
27/// loop {
28/// self.frame_count += 1;
29/// tokio::time::sleep(tokio::time::Duration::from_millis(33)).await;
30/// }
31/// }
32///
33/// async fn health_check(&self) -> HealthStatus {
34/// if self.frame_count > 0 {
35/// HealthStatus::Healthy
36/// } else {
37/// HealthStatus::Unhealthy {
38/// reason: "No frames captured".to_string()
39/// }
40/// }
41/// }
42/// }
43/// ```
44#[async_trait]
45pub trait NodeRunner: Send + Sync {
46 /// Get the node name
47 fn name(&self) -> &str;
48
49 /// Run the node
50 ///
51 /// This method should contain the main logic for the node.
52 /// It will be called by the supervisor and should run until completion or error.
53 async fn run(&mut self) -> Result<()>;
54
55 /// Perform a health check
56 ///
57 /// Override this method to provide custom health checking logic.
58 /// By default, nodes are considered healthy.
59 async fn health_check(&self) -> HealthStatus {
60 HealthStatus::Healthy
61 }
62
63 /// Handle shutdown signal
64 ///
65 /// Override this method to perform cleanup before shutdown.
66 /// By default, no special cleanup is performed.
67 async fn shutdown(&mut self) -> Result<()> {
68 Ok(())
69 }
70}