Improved error handling and spawn structure

This commit is contained in:
2026-07-24 16:56:16 +02:00
parent ae37c02adc
commit f036d04d7b
2 changed files with 33 additions and 31 deletions
+15 -13
View File
@@ -4,6 +4,7 @@ use tokio::sync::Mutex;
use crate::{config::Config, terminal::TerminalServer}; use crate::{config::Config, terminal::TerminalServer};
#[derive(Debug, Clone)]
pub struct Engine { pub struct Engine {
pub terminal_server: Arc<TerminalServer>, pub terminal_server: Arc<TerminalServer>,
pub config: Arc<Mutex<Config>>, pub config: Arc<Mutex<Config>>,
@@ -17,32 +18,34 @@ impl Engine {
}) })
} }
pub fn spawn_terminal_server(&self) { pub async fn spawn_terminal_server(&self) -> tokio::io::Result<()> {
let terminal_server = self.terminal_server.clone(); let terminal_server = self.terminal_server.clone();
tokio::spawn(async move { tokio::spawn(async move { terminal_server.run().await })
terminal_server
.run()
.await .await
.expect("Failed to run terminal server"); .expect("Failed to spawn terminal server")
});
} }
pub fn spawn_broadcaster(&self) { pub async fn spawn_broadcaster(&self) -> tokio::io::Result<()> {
let terminal_server = self.terminal_server.clone(); let s = self.clone();
let config = self.config.clone();
tokio::spawn(async move { tokio::spawn(async move { s.run_broadcaster().await })
.await
.expect("Failed to spawn broadcaster")
}
pub async fn run_broadcaster(&self) -> tokio::io::Result<()> {
let mut refresh = tokio::time::interval(tokio::time::Duration::from_secs(5)); let mut refresh = tokio::time::interval(tokio::time::Duration::from_secs(5));
loop { loop {
refresh.tick().await; refresh.tick().await;
let watch_list = &config.lock().await.watchlist.symbols; let watch_list = &self.config.lock().await.watchlist.symbols;
match crate::fetch::fetch_watch_list(watch_list).await { match crate::fetch::fetch_watch_list(watch_list).await {
Ok(watch_list) => { Ok(watch_list) => {
if let Err(error) = terminal_server if let Err(error) = self
.terminal_server
.broadcast( .broadcast(
pulse_wire::terminal::TerminalServerMessage::WatchListUpdated( pulse_wire::terminal::TerminalServerMessage::WatchListUpdated(
watch_list, watch_list,
@@ -56,7 +59,6 @@ impl Engine {
Err(error) => eprintln!("Failed to refresh Hyperliquid watch list: {error}"), Err(error) => eprintln!("Failed to refresh Hyperliquid watch list: {error}"),
} }
} }
});
} }
pub async fn run_engine(&self) -> tokio::io::Result<()> { pub async fn run_engine(&self) -> tokio::io::Result<()> {
+2 -2
View File
@@ -7,9 +7,9 @@ pub mod terminal;
async fn main() -> tokio::io::Result<()> { async fn main() -> tokio::io::Result<()> {
let engine = engine::Engine::new().await?; let engine = engine::Engine::new().await?;
engine.spawn_terminal_server(); engine.spawn_terminal_server().await?;
engine.spawn_broadcaster(); engine.spawn_broadcaster().await?;
engine.run_engine().await?; engine.run_engine().await?;