Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Action handlers that effect changes:
use botcore::engine::Engine;
use botcore::types::{Collector, Strategy, Executor};
use botcore::Result;
use tracing::{error, info};

// 1. Define your types
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -139,8 +140,23 @@ async fn main() -> Result<()> {
engine.add_strategy(Box::new(MyStrategy));
engine.add_executor(Box::new(MyExecutor));

let join_set = engine.run().await?;
join_set.await;
match engine.run().await {
Ok(mut set) => {
while let Some(res) = set.join_next().await {
match res {
Ok(res) => {
info!("res: {:?}", res);
}
Err(e) => {
info!("error: {:?}", e);
}
}
}
}
Err(e) => {
error!("Engine run error: {:?}", e);
}
}
Ok(())
}
```
Expand Down Expand Up @@ -176,7 +192,6 @@ The engine can be configured through builder methods:
let engine = Engine::new()
.with_event_channel_capacity(1024)
.with_action_channel_capacity(1024)
.with_metrics_enabled(true);
```

## Error Handling
Expand Down
19 changes: 18 additions & 1 deletion examples/block_trader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use botcore::types::{Collector, CollectorStream, Executor, Strategy};
use botcore::Result;
use std::time::Duration;
use tokio::time;
use tracing::{error, info};

/// A simple event containing block information
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -132,7 +133,23 @@ async fn main() -> Result<()> {
engine.add_executor(Box::new(TradeExecutor));

// Run the engine
let _join_set = engine.run().await?;
match engine.run().await {
Ok(mut set) => {
while let Some(res) = set.join_next().await {
match res {
Ok(res) => {
info!("res: {:?}", res);
}
Err(e) => {
info!("error: {:?}", e);
}
}
}
}
Err(e) => {
error!("Engine run error: {:?}", e);
}
}

// Keep the engine running for a while
time::sleep(Duration::from_secs(30)).await;
Expand Down
23 changes: 17 additions & 6 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::metrics::METRICS;
/// use botcore::types::{Collector, CollectorStream, Strategy, Executor};
/// use async_trait::async_trait;
/// use tokio_stream;
/// use tracing::{info, error};
///
/// #[derive(Debug, Clone)]
/// struct BlockEvent;
Expand Down Expand Up @@ -82,12 +83,22 @@ use crate::metrics::METRICS;
/// engine.add_strategy(Box::new(TradingStrategy));
/// engine.add_executor(Box::new(TradeExecutor));
///
/// // Run the engine
/// let mut join_set = engine.run().await?;
///
/// // Wait for all tasks to complete
/// while join_set.join_next().await.is_some() {}
/// Ok(())
/// // Run the engine and handle task results
/// match engine.run().await {
/// Ok(mut set) => {
/// while let Some(res) = set.join_next().await {
/// match res {
/// Ok(res) => info!("Task completed: {:?}", res),
/// Err(e) => info!("Task error: {:?}", e),
/// }
/// }
/// Ok(())
/// }
/// Err(e) => {
/// error!("Engine run error: {:?}", e);
/// Err(e)
/// }
/// }
/// }
/// ```
pub struct Engine<E, A> {
Expand Down
Loading