Add sql transactions

This commit is contained in:
Tobias Reisinger 2024-05-02 13:30:14 +02:00
parent 455ca50695
commit 9823511b62
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
9 changed files with 171 additions and 138 deletions

View file

@ -9,7 +9,6 @@ use emgauwa_common::constants::{HEARTBEAT_INTERVAL, HEARTBEAT_TIMEOUT};
use emgauwa_common::errors::EmgauwaError;
use emgauwa_common::types::{ControllerWsAction, EmgauwaUid};
use futures::executor::block_on;
use sqlx::pool::PoolConnection;
use sqlx::{Pool, Sqlite};
use ws::Message;
@ -48,12 +47,11 @@ impl Actor for ControllersWs {
impl ControllersWs {
pub fn handle_action(
&mut self,
conn: &mut PoolConnection<Sqlite>,
ctx: &mut <ControllersWs as Actor>::Context,
action: ControllerWsAction,
) {
let action_res = match action {
ControllerWsAction::Register(controller) => self.handle_register(conn, ctx, controller),
ControllerWsAction::Register(controller) => self.handle_register(ctx, controller),
ControllerWsAction::RelayStates((controller_uid, relay_states)) => {
self.handle_relay_states(controller_uid, relay_states)
}
@ -103,15 +101,6 @@ impl Handler<ControllerWsAction> for ControllersWs {
impl StreamHandler<Result<Message, ProtocolError>> for ControllersWs {
fn handle(&mut self, msg: Result<Message, ProtocolError>, ctx: &mut Self::Context) {
let mut pool_conn = match block_on(self.pool.acquire()) {
Ok(conn) => conn,
Err(err) => {
log::error!("Failed to acquire database connection: {:?}", err);
ctx.stop();
return;
}
};
let msg = match msg {
Err(_) => {
ctx.stop();
@ -130,7 +119,7 @@ impl StreamHandler<Result<Message, ProtocolError>> for ControllersWs {
}
Message::Text(text) => match serde_json::from_str(&text) {
Ok(action) => {
self.handle_action(&mut pool_conn, ctx, action);
self.handle_action(ctx, action);
}
Err(e) => {
log::error!("Error deserializing action: {:?}", e);