use tokio::spawn for monitoring function to make it run concurrent in a correct way

This commit is contained in:
f321x
2024-07-08 09:06:42 +00:00
parent 08e21d6ce0
commit 648ed82c0c
3 changed files with 14 additions and 14 deletions

View File

@ -193,9 +193,9 @@ async fn poll_final_payout(
panic!("implement") panic!("implement")
} }
pub async fn api_server(coordinator: Coordinator) -> Result<()> { pub async fn api_server(coordinator: Arc<Coordinator>) -> Result<()> {
let database = coordinator.coordinator_db; let database = Arc::clone(&coordinator.coordinator_db);
let wallet = coordinator.coordinator_wallet; let wallet = Arc::clone(&coordinator.coordinator_wallet);
let app = Router::new() let app = Router::new()
.route("/create-offer", post(receive_order)) .route("/create-offer", post(receive_order))

View File

@ -35,9 +35,9 @@ async fn punish_trader(
Ok(()) Ok(())
} }
pub async fn monitor_bonds(coordinator: &Coordinator) -> Result<()> { pub async fn monitor_bonds(coordinator: Arc<Coordinator>) -> Result<()> {
let coordinator_db = &coordinator.coordinator_db; let coordinator_db = Arc::clone(&coordinator.coordinator_db);
let coordinator_wallet = &coordinator.coordinator_wallet; let coordinator_wallet = Arc::clone(&coordinator.coordinator_wallet);
loop { loop {
// fetch all bonds // fetch all bonds
@ -55,7 +55,7 @@ pub async fn monitor_bonds(coordinator: &Coordinator) -> Result<()> {
{ {
"1" => { "1" => {
dbg!("Punishing trader for bond violation: {:?}", e); dbg!("Punishing trader for bond violation: {:?}", e);
punish_trader(coordinator, bond.0, bond.1).await?; punish_trader(&coordinator, bond.0, bond.1).await?;
} }
"0" => { "0" => {
dbg!("Punishment disabled, ignoring bond violation: {:?}", e); dbg!("Punishment disabled, ignoring bond violation: {:?}", e);

View File

@ -15,8 +15,8 @@ use tokio::sync::Mutex;
use wallet::*; use wallet::*;
pub struct Coordinator { pub struct Coordinator {
pub coordinator_db: CoordinatorDB, pub coordinator_db: Arc<CoordinatorDB>,
pub coordinator_wallet: CoordinatorWallet, pub coordinator_wallet: Arc<CoordinatorWallet>,
} }
// populate .env with values before starting // populate .env with values before starting
@ -25,13 +25,13 @@ async fn main() -> Result<()> {
dotenv().ok(); dotenv().ok();
// Initialize the database pool // Initialize the database pool
let coordinator = Coordinator { let coordinator = Arc::new(Coordinator {
coordinator_db: CoordinatorDB::init().await?, coordinator_db: Arc::new(CoordinatorDB::init().await?),
coordinator_wallet: CoordinatorWallet::init()?, coordinator_wallet: Arc::new(CoordinatorWallet::init()?),
}; });
// begin monitoring bonds // begin monitoring bonds
monitor_bonds(&coordinator).await?; tokio::spawn(monitor_bonds(Arc::clone(&coordinator)));
// Start the API server // Start the API server
api_server(coordinator).await?; api_server(coordinator).await?;