From fa2ca990a155fc102b7c2c9fc53f7e1f0f36db7c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 30 Sep 2021 12:02:03 +1000 Subject: [PATCH] Use the same database connection throughout the actor setup --- daemon/src/cleanup.rs | 13 +++++++------ daemon/src/maker.rs | 4 +++- daemon/src/taker.rs | 4 +++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/daemon/src/cleanup.rs b/daemon/src/cleanup.rs index 6ae528c..cd2d7a4 100644 --- a/daemon/src/cleanup.rs +++ b/daemon/src/cleanup.rs @@ -1,13 +1,14 @@ use crate::db::{insert_new_cfd_state_by_order_id, load_all_cfds}; use crate::model::cfd::{Cfd, CfdState, CfdStateCommon}; use anyhow::Result; -use sqlx::SqlitePool; +use sqlx::pool::PoolConnection; +use sqlx::Sqlite; use std::time::SystemTime; -pub async fn transition_non_continue_cfds_to_setup_failed(db: SqlitePool) -> Result<()> { - let mut conn = db.acquire().await?; - - let cfds = load_all_cfds(&mut conn).await?; +pub async fn transition_non_continue_cfds_to_setup_failed( + conn: &mut PoolConnection, +) -> Result<()> { + let cfds = load_all_cfds(conn).await?; for cfd in cfds.iter().filter(|cfd| Cfd::is_cleanup(cfd)) { insert_new_cfd_state_by_order_id( @@ -18,7 +19,7 @@ pub async fn transition_non_continue_cfds_to_setup_failed(db: SqlitePool) -> Res }, info: format!("Was in state {} which cannot be continued.", cfd.state), }, - &mut conn, + conn, ) .await?; } diff --git a/daemon/src/maker.rs b/daemon/src/maker.rs index 9fedf66..53b2f29 100644 --- a/daemon/src/maker.rs +++ b/daemon/src/maker.rs @@ -150,10 +150,12 @@ async fn main() -> Result<()> { Some(db) => (**db).clone(), None => return Err(rocket), }; + let mut conn = db.acquire().await.unwrap(); - cleanup::transition_non_continue_cfds_to_setup_failed(db.clone()) + cleanup::transition_non_continue_cfds_to_setup_failed(&mut conn) .await .unwrap(); + let cfds = load_all_cfds(&mut conn).await.unwrap(); let (maker_inc_connections_address, maker_inc_connections_context) = xtra::Context::new(None); diff --git a/daemon/src/taker.rs b/daemon/src/taker.rs index 2f0d39a..20d806d 100644 --- a/daemon/src/taker.rs +++ b/daemon/src/taker.rs @@ -152,10 +152,12 @@ async fn main() -> Result<()> { Some(db) => (**db).clone(), None => return Err(rocket), }; + let mut conn = db.acquire().await.unwrap(); - cleanup::transition_non_continue_cfds_to_setup_failed(db.clone()) + cleanup::transition_non_continue_cfds_to_setup_failed(&mut conn) .await .unwrap(); + let cfds = load_all_cfds(&mut conn).await.unwrap(); let send_to_maker = send_to_socket::Actor::new(write) .create(None)