Revert "Add sql transactions"
This caused the error "locked database".
This reverts commit 4489b37a3f.
			
			
This commit is contained in:
		
							parent
							
								
									4489b37a3f
								
							
						
					
					
						commit
						9ed576b552
					
				
					 4 changed files with 477 additions and 625 deletions
				
			
		
							
								
								
									
										1021
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										1021
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							| 
						 | 
					@ -86,10 +86,9 @@ impl Handler<Reload> for AppState {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fn handle(&mut self, _msg: Reload, _ctx: &mut Self::Context) -> Self::Result {
 | 
						fn handle(&mut self, _msg: Reload, _ctx: &mut Self::Context) -> Self::Result {
 | 
				
			||||||
		log::debug!("Reloading controller");
 | 
							log::debug!("Reloading controller");
 | 
				
			||||||
		let mut tx = block_on(self.pool.begin())?;
 | 
							let mut pool_conn = block_on(self.pool.acquire())?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		self.this.reload(&mut tx)?;
 | 
							self.this.reload(&mut pool_conn)?;
 | 
				
			||||||
		block_on(tx.commit())?;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		self.notify_controller_change();
 | 
							self.notify_controller_change();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										29
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										29
									
								
								src/main.rs
									
										
									
									
									
								
							| 
						 | 
					@ -6,7 +6,7 @@ use emgauwa_common::models::{Controller, FromDbModel};
 | 
				
			||||||
use emgauwa_common::types::EmgauwaUid;
 | 
					use emgauwa_common::types::EmgauwaUid;
 | 
				
			||||||
use emgauwa_common::utils::{drop_privileges, init_logging};
 | 
					use emgauwa_common::utils::{drop_privileges, init_logging};
 | 
				
			||||||
use rppal_pfd::PiFaceDigital;
 | 
					use rppal_pfd::PiFaceDigital;
 | 
				
			||||||
use sqlx::Transaction;
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
use sqlx::Sqlite;
 | 
					use sqlx::Sqlite;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::relay_loop::run_relays_loop;
 | 
					use crate::relay_loop::run_relays_loop;
 | 
				
			||||||
| 
						 | 
					@ -21,11 +21,11 @@ mod utils;
 | 
				
			||||||
mod ws;
 | 
					mod ws;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn create_this_controller(
 | 
					async fn create_this_controller(
 | 
				
			||||||
	tx: &mut Transaction<'_, Sqlite>,
 | 
						conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
	settings: &Settings,
 | 
						settings: &Settings,
 | 
				
			||||||
) -> Result<DbController, EmgauwaError> {
 | 
					) -> Result<DbController, EmgauwaError> {
 | 
				
			||||||
	DbController::create(
 | 
						DbController::create(
 | 
				
			||||||
		tx,
 | 
							conn,
 | 
				
			||||||
		&EmgauwaUid::default(),
 | 
							&EmgauwaUid::default(),
 | 
				
			||||||
		&settings.name,
 | 
							&settings.name,
 | 
				
			||||||
		settings.relays.len() as i64,
 | 
							settings.relays.len() as i64,
 | 
				
			||||||
| 
						 | 
					@ -35,12 +35,12 @@ async fn create_this_controller(
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn create_this_relay(
 | 
					async fn create_this_relay(
 | 
				
			||||||
	tx: &mut Transaction<'_, Sqlite>,
 | 
						conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
	this_controller: &DbController,
 | 
						this_controller: &DbController,
 | 
				
			||||||
	settings_relay: &settings::Relay,
 | 
						settings_relay: &settings::Relay,
 | 
				
			||||||
) -> Result<DbRelay, EmgauwaError> {
 | 
					) -> Result<DbRelay, EmgauwaError> {
 | 
				
			||||||
	let relay = DbRelay::create(
 | 
						let relay = DbRelay::create(
 | 
				
			||||||
		tx,
 | 
							conn,
 | 
				
			||||||
		&settings_relay.name,
 | 
							&settings_relay.name,
 | 
				
			||||||
		settings_relay.number.ok_or(EmgauwaError::Internal(
 | 
							settings_relay.number.ok_or(EmgauwaError::Internal(
 | 
				
			||||||
			"Relay number is missing".to_string(),
 | 
								"Relay number is missing".to_string(),
 | 
				
			||||||
| 
						 | 
					@ -49,9 +49,9 @@ async fn create_this_relay(
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	.await?;
 | 
						.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let off = DbSchedule::get_off(tx).await?;
 | 
						let off = DbSchedule::get_off(conn).await?;
 | 
				
			||||||
	for weekday in 0..7 {
 | 
						for weekday in 0..7 {
 | 
				
			||||||
		DbJunctionRelaySchedule::set_schedule(tx, &relay, &off, weekday).await?;
 | 
							DbJunctionRelaySchedule::set_schedule(conn, &relay, &off, weekday).await?;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Ok(relay)
 | 
						Ok(relay)
 | 
				
			||||||
| 
						 | 
					@ -72,20 +72,20 @@ async fn main() -> Result<(), std::io::Error> {
 | 
				
			||||||
		.await
 | 
							.await
 | 
				
			||||||
		.map_err(EmgauwaError::from)?;
 | 
							.map_err(EmgauwaError::from)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let mut tx = pool.begin().await.map_err(EmgauwaError::from)?;
 | 
						let mut conn = pool.acquire().await.map_err(EmgauwaError::from)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let db_controller = match DbController::get_all(&mut tx)
 | 
						let db_controller = match DbController::get_all(&mut conn)
 | 
				
			||||||
		.await
 | 
							.await
 | 
				
			||||||
		.map_err(EmgauwaError::from)?
 | 
							.map_err(EmgauwaError::from)?
 | 
				
			||||||
		.pop()
 | 
							.pop()
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		None => futures::executor::block_on(create_this_controller(&mut tx, &settings))?,
 | 
							None => futures::executor::block_on(create_this_controller(&mut conn, &settings))?,
 | 
				
			||||||
		Some(c) => c,
 | 
							Some(c) => c,
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for relay in &settings.relays {
 | 
						for relay in &settings.relays {
 | 
				
			||||||
		if DbRelay::get_by_controller_and_num(
 | 
							if DbRelay::get_by_controller_and_num(
 | 
				
			||||||
			&mut tx,
 | 
								&mut conn,
 | 
				
			||||||
			&db_controller,
 | 
								&db_controller,
 | 
				
			||||||
			relay.number.ok_or(EmgauwaError::Internal(
 | 
								relay.number.ok_or(EmgauwaError::Internal(
 | 
				
			||||||
				"Relay number is missing".to_string(),
 | 
									"Relay number is missing".to_string(),
 | 
				
			||||||
| 
						 | 
					@ -95,19 +95,18 @@ async fn main() -> Result<(), std::io::Error> {
 | 
				
			||||||
		.map_err(EmgauwaError::from)?
 | 
							.map_err(EmgauwaError::from)?
 | 
				
			||||||
		.is_none()
 | 
							.is_none()
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			create_this_relay(&mut tx, &db_controller, relay)
 | 
								create_this_relay(&mut conn, &db_controller, relay)
 | 
				
			||||||
				.await
 | 
									.await
 | 
				
			||||||
				.map_err(EmgauwaError::from)?;
 | 
									.map_err(EmgauwaError::from)?;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let db_controller = db_controller
 | 
						let db_controller = db_controller
 | 
				
			||||||
		.update(&mut tx, &db_controller.name, settings.relays.len() as i64)
 | 
							.update(&mut conn, &db_controller.name, settings.relays.len() as i64)
 | 
				
			||||||
		.await
 | 
							.await
 | 
				
			||||||
		.map_err(EmgauwaError::from)?;
 | 
							.map_err(EmgauwaError::from)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let this = Controller::from_db_model(&mut tx, db_controller).map_err(EmgauwaError::from)?;
 | 
						let this = Controller::from_db_model(&mut conn, db_controller).map_err(EmgauwaError::from)?;
 | 
				
			||||||
	tx.commit().await.map_err(EmgauwaError::from)?;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let url = format!(
 | 
						let url = format!(
 | 
				
			||||||
		"ws://{}:{}/api/v1/ws/controllers",
 | 
							"ws://{}:{}/api/v1/ws/controllers",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,7 +5,7 @@ use emgauwa_common::errors::{DatabaseError, EmgauwaError};
 | 
				
			||||||
use emgauwa_common::models::{Controller, Relay};
 | 
					use emgauwa_common::models::{Controller, Relay};
 | 
				
			||||||
use emgauwa_common::types::{ControllerWsAction, ScheduleUid};
 | 
					use emgauwa_common::types::{ControllerWsAction, ScheduleUid};
 | 
				
			||||||
use futures::{future, pin_mut, SinkExt, StreamExt};
 | 
					use futures::{future, pin_mut, SinkExt, StreamExt};
 | 
				
			||||||
use sqlx::Transaction;
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
use sqlx::{Pool, Sqlite};
 | 
					use sqlx::{Pool, Sqlite};
 | 
				
			||||||
use tokio::time;
 | 
					use tokio::time;
 | 
				
			||||||
use tokio_tungstenite::tungstenite::Message;
 | 
					use tokio_tungstenite::tungstenite::Message;
 | 
				
			||||||
| 
						 | 
					@ -108,7 +108,14 @@ async fn handle_message(
 | 
				
			||||||
		match serde_json::from_str(&text) {
 | 
							match serde_json::from_str(&text) {
 | 
				
			||||||
			Ok(action) => {
 | 
								Ok(action) => {
 | 
				
			||||||
				log::debug!("Received action: {:?}", action);
 | 
									log::debug!("Received action: {:?}", action);
 | 
				
			||||||
				let action_res = handle_action(pool, app_state, action).await;
 | 
									let mut pool_conn = match pool.acquire().await {
 | 
				
			||||||
 | 
										Ok(conn) => conn,
 | 
				
			||||||
 | 
										Err(err) => {
 | 
				
			||||||
 | 
											log::error!("Failed to acquire database connection: {:?}", err);
 | 
				
			||||||
 | 
											return;
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									};
 | 
				
			||||||
 | 
									let action_res = handle_action(&mut pool_conn, app_state, action).await;
 | 
				
			||||||
				if let Err(e) = action_res {
 | 
									if let Err(e) = action_res {
 | 
				
			||||||
					log::error!("Error handling action: {:?}", e);
 | 
										log::error!("Error handling action: {:?}", e);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
| 
						 | 
					@ -121,31 +128,29 @@ async fn handle_message(
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub async fn handle_action(
 | 
					pub async fn handle_action(
 | 
				
			||||||
	pool: Pool<Sqlite>,
 | 
						conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
	app_state: &Addr<AppState>,
 | 
						app_state: &Addr<AppState>,
 | 
				
			||||||
	action: ControllerWsAction,
 | 
						action: ControllerWsAction,
 | 
				
			||||||
) -> Result<(), EmgauwaError> {
 | 
					) -> Result<(), EmgauwaError> {
 | 
				
			||||||
	let this = app_state_get_this(app_state).await?;
 | 
						let this = app_state_get_this(app_state).await?;
 | 
				
			||||||
	let mut tx = pool.begin().await?;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	match action {
 | 
						match action {
 | 
				
			||||||
		ControllerWsAction::Controller(controller) => {
 | 
							ControllerWsAction::Controller(controller) => {
 | 
				
			||||||
			handle_controller(&mut tx, &this, controller).await?
 | 
								handle_controller(conn, &this, controller).await?
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		ControllerWsAction::Relays(relays) => handle_relays(&mut tx, &this, relays).await?,
 | 
							ControllerWsAction::Relays(relays) => handle_relays(conn, &this, relays).await?,
 | 
				
			||||||
		ControllerWsAction::Schedules(schedules) => handle_schedules(&mut tx, schedules).await?,
 | 
							ControllerWsAction::Schedules(schedules) => handle_schedules(conn, schedules).await?,
 | 
				
			||||||
		ControllerWsAction::RelayPulse((relay_num, duration)) => {
 | 
							ControllerWsAction::RelayPulse((relay_num, duration)) => {
 | 
				
			||||||
			handle_relay_pulse(app_state, relay_num, duration).await?
 | 
								handle_relay_pulse(app_state, relay_num, duration).await?
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		_ => return Ok(()),
 | 
							_ => return Ok(()),
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
	tx.commit().await?;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	utils::app_state_reload(app_state).await
 | 
						utils::app_state_reload(app_state).await
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn handle_controller(
 | 
					async fn handle_controller(
 | 
				
			||||||
	tx: &mut Transaction<'_, Sqlite>,
 | 
						conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
	this: &Controller,
 | 
						this: &Controller,
 | 
				
			||||||
	controller: Controller,
 | 
						controller: Controller,
 | 
				
			||||||
) -> Result<(), EmgauwaError> {
 | 
					) -> Result<(), EmgauwaError> {
 | 
				
			||||||
| 
						 | 
					@ -154,17 +159,17 @@ async fn handle_controller(
 | 
				
			||||||
			"Controller UID mismatch during update",
 | 
								"Controller UID mismatch during update",
 | 
				
			||||||
		)));
 | 
							)));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	DbController::get_by_uid(tx, &controller.c.uid)
 | 
						DbController::get_by_uid(conn, &controller.c.uid)
 | 
				
			||||||
		.await?
 | 
							.await?
 | 
				
			||||||
		.ok_or(DatabaseError::NotFound)?
 | 
							.ok_or(DatabaseError::NotFound)?
 | 
				
			||||||
		.update(tx, controller.c.name.as_str(), this.c.relay_count)
 | 
							.update(conn, controller.c.name.as_str(), this.c.relay_count)
 | 
				
			||||||
		.await?;
 | 
							.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Ok(())
 | 
						Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn handle_schedules(
 | 
					async fn handle_schedules(
 | 
				
			||||||
	tx: &mut Transaction<'_, Sqlite>,
 | 
						conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
	schedules: Vec<DbSchedule>,
 | 
						schedules: Vec<DbSchedule>,
 | 
				
			||||||
) -> Result<(), EmgauwaError> {
 | 
					) -> Result<(), EmgauwaError> {
 | 
				
			||||||
	let mut handled_uids = vec![
 | 
						let mut handled_uids = vec![
 | 
				
			||||||
| 
						 | 
					@ -179,15 +184,15 @@ async fn handle_schedules(
 | 
				
			||||||
		handled_uids.push(schedule.uid.clone());
 | 
							handled_uids.push(schedule.uid.clone());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		log::debug!("Handling schedule: {:?}", schedule);
 | 
							log::debug!("Handling schedule: {:?}", schedule);
 | 
				
			||||||
		let schedule_db = DbSchedule::get_by_uid(tx, &schedule.uid).await?;
 | 
							let schedule_db = DbSchedule::get_by_uid(conn, &schedule.uid).await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if let Some(schedule_db) = schedule_db {
 | 
							if let Some(schedule_db) = schedule_db {
 | 
				
			||||||
			schedule_db
 | 
								schedule_db
 | 
				
			||||||
				.update(tx, schedule.name.as_str(), &schedule.periods)
 | 
									.update(conn, schedule.name.as_str(), &schedule.periods)
 | 
				
			||||||
				.await?;
 | 
									.await?;
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			DbSchedule::create(
 | 
								DbSchedule::create(
 | 
				
			||||||
				tx,
 | 
									conn,
 | 
				
			||||||
				schedule.uid.clone(),
 | 
									schedule.uid.clone(),
 | 
				
			||||||
				schedule.name.as_str(),
 | 
									schedule.name.as_str(),
 | 
				
			||||||
				&schedule.periods,
 | 
									&schedule.periods,
 | 
				
			||||||
| 
						 | 
					@ -200,7 +205,7 @@ async fn handle_schedules(
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn handle_relays(
 | 
					async fn handle_relays(
 | 
				
			||||||
	tx: &mut Transaction<'_, Sqlite>,
 | 
						conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
	this: &Controller,
 | 
						this: &Controller,
 | 
				
			||||||
	relays: Vec<Relay>,
 | 
						relays: Vec<Relay>,
 | 
				
			||||||
) -> Result<(), EmgauwaError> {
 | 
					) -> Result<(), EmgauwaError> {
 | 
				
			||||||
| 
						 | 
					@ -210,24 +215,24 @@ async fn handle_relays(
 | 
				
			||||||
				"Controller UID mismatch during relay update",
 | 
									"Controller UID mismatch during relay update",
 | 
				
			||||||
			)));
 | 
								)));
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		let db_relay = DbRelay::get_by_controller_and_num(tx, &this.c, relay.r.number)
 | 
							let db_relay = DbRelay::get_by_controller_and_num(conn, &this.c, relay.r.number)
 | 
				
			||||||
			.await?
 | 
								.await?
 | 
				
			||||||
			.ok_or(DatabaseError::NotFound)?;
 | 
								.ok_or(DatabaseError::NotFound)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		db_relay.update(tx, relay.r.name.as_str()).await?;
 | 
							db_relay.update(conn, relay.r.name.as_str()).await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		handle_schedules(tx, relay.schedules.clone()).await?;
 | 
							handle_schedules(conn, relay.schedules.clone()).await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		let mut schedules = Vec::new(); // We need to get the schedules from the database to have the right IDs
 | 
							let mut schedules = Vec::new(); // We need to get the schedules from the database to have the right IDs
 | 
				
			||||||
		for schedule in relay.schedules {
 | 
							for schedule in relay.schedules {
 | 
				
			||||||
			schedules.push(
 | 
								schedules.push(
 | 
				
			||||||
				DbSchedule::get_by_uid(tx, &schedule.uid)
 | 
									DbSchedule::get_by_uid(conn, &schedule.uid)
 | 
				
			||||||
					.await?
 | 
										.await?
 | 
				
			||||||
					.ok_or(DatabaseError::NotFound)?,
 | 
										.ok_or(DatabaseError::NotFound)?,
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		DbJunctionRelaySchedule::set_schedules(tx, &db_relay, schedules.iter().collect()).await?;
 | 
							DbJunctionRelaySchedule::set_schedules(conn, &db_relay, schedules.iter().collect()).await?;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Ok(())
 | 
						Ok(())
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue