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
+31 -29
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,46 +18,47 @@ 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 .await
.run() .expect("Failed to spawn terminal server")
.await
.expect("Failed to run 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 })
let mut refresh = tokio::time::interval(tokio::time::Duration::from_secs(5)); .await
.expect("Failed to spawn broadcaster")
}
loop { pub async fn run_broadcaster(&self) -> tokio::io::Result<()> {
refresh.tick().await; let mut refresh = tokio::time::interval(tokio::time::Duration::from_secs(5));
let watch_list = &config.lock().await.watchlist.symbols; loop {
refresh.tick().await;
match crate::fetch::fetch_watch_list(watch_list).await { let watch_list = &self.config.lock().await.watchlist.symbols;
Ok(watch_list) => {
if let Err(error) = terminal_server match crate::fetch::fetch_watch_list(watch_list).await {
.broadcast( Ok(watch_list) => {
pulse_wire::terminal::TerminalServerMessage::WatchListUpdated( if let Err(error) = self
watch_list, .terminal_server
), .broadcast(
) pulse_wire::terminal::TerminalServerMessage::WatchListUpdated(
.await watch_list,
{ ),
eprintln!("Failed to broadcast Hyperliquid watch list: {error}"); )
} .await
{
eprintln!("Failed to broadcast Hyperliquid watch list: {error}");
} }
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?;