fix mycelium impl

This commit is contained in:
Timur Gordon
2025-09-01 16:34:24 +02:00
parent 44b1dd4249
commit 77b4c66a10
6 changed files with 667 additions and 338 deletions

View File

@@ -5,8 +5,7 @@
//! then pass it to SupervisorApp for runtime management.
use crate::Supervisor;
use crate::openrpc::start_openrpc_servers;
use crate::mycelium::MyceliumServer;
use crate::mycelium::MyceliumIntegration;
use log::{info, error, debug};
use std::sync::Arc;
use tokio::sync::Mutex;
@@ -14,24 +13,24 @@ use tokio::sync::Mutex;
/// Main supervisor application
pub struct SupervisorApp {
pub supervisor: Supervisor,
pub bind_address: String,
pub port: u16,
pub mycelium_url: String,
pub topic: String,
}
impl SupervisorApp {
/// Create a new supervisor application with a built supervisor
pub fn new(supervisor: Supervisor, bind_address: String, port: u16) -> Self {
pub fn new(supervisor: Supervisor, mycelium_url: String, topic: String) -> Self {
Self {
supervisor,
bind_address,
port,
mycelium_url,
topic,
}
}
/// Start the complete supervisor application
/// This method handles the entire application lifecycle:
/// - Starts all configured runners
/// - Launches the OpenRPC server
/// - Connects to Mycelium daemon for message transport
/// - Sets up graceful shutdown handling
/// - Keeps the application running
pub async fn start(&mut self) -> Result<(), Box<dyn std::error::Error>> {
@@ -40,11 +39,8 @@ impl SupervisorApp {
// Start all configured runners
self.start_all().await?;
// Start OpenRPC server
self.start_openrpc_server().await?;
// Start Mycelium server
self.start_mycelium_server().await?;
// Start Mycelium integration
self.start_mycelium_integration().await?;
// Set up graceful shutdown
self.setup_graceful_shutdown().await;
@@ -56,27 +52,33 @@ impl SupervisorApp {
Ok(())
}
/// Start the OpenRPC server
async fn start_openrpc_server(&self) -> Result<(), Box<dyn std::error::Error>> {
info!("Starting OpenRPC server...");
/// Start the Mycelium integration
async fn start_mycelium_integration(&self) -> Result<(), Box<dyn std::error::Error>> {
info!("Starting Mycelium integration...");
let supervisor_for_openrpc = Arc::new(Mutex::new(self.supervisor.clone()));
let bind_address = self.bind_address.clone();
let port = self.port;
let supervisor_for_mycelium = Arc::new(Mutex::new(self.supervisor.clone()));
let mycelium_url = self.mycelium_url.clone();
let topic = self.topic.clone();
// Start the OpenRPC server in a background task
let server_handle = tokio::spawn(async move {
if let Err(e) = start_openrpc_servers(supervisor_for_openrpc, &bind_address, port).await {
error!("OpenRPC server error: {}", e);
let mycelium_integration = MyceliumIntegration::new(
supervisor_for_mycelium,
mycelium_url,
topic,
);
// Start the Mycelium integration in a background task
let integration_handle = tokio::spawn(async move {
if let Err(e) = mycelium_integration.start().await {
error!("Mycelium integration error: {}", e);
}
});
// Give the server a moment to start
// Give the integration a moment to start
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
info!("OpenRPC server started successfully");
info!("Mycelium integration started successfully");
// Store the handle for potential cleanup (we could add this to the struct if needed)
std::mem::forget(server_handle); // For now, let it run in background
// Store the handle for potential cleanup
std::mem::forget(integration_handle); // For now, let it run in background
Ok(())
}
@@ -150,36 +152,6 @@ impl SupervisorApp {
Ok(())
}
/// Start the Mycelium server
async fn start_mycelium_server(&self) -> Result<(), Box<dyn std::error::Error>> {
info!("Starting Mycelium server...");
let supervisor_for_mycelium = Arc::new(Mutex::new(self.supervisor.clone()));
let mycelium_port = 8990; // Standard Mycelium port
let bind_address = "127.0.0.1".to_string();
let mycelium_server = MyceliumServer::new(
supervisor_for_mycelium,
bind_address,
mycelium_port,
);
// Start the Mycelium server in a background task
let server_handle = tokio::spawn(async move {
if let Err(e) = mycelium_server.start().await {
error!("Mycelium server error: {}", e);
}
});
// Give the server a moment to start
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
info!("Mycelium server started successfully on port {}", mycelium_port);
// Store the handle for potential cleanup
std::mem::forget(server_handle); // For now, let it run in background
Ok(())
}
/// Get status of all runners
pub async fn get_status(&self) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> {