|
| 1 | +use pony::ConnectionStorageApiOp; |
| 2 | +use std::collections::HashMap; |
1 | 3 | use std::sync::Arc; |
2 | | -use tokio::sync::mpsc; |
3 | 4 | use tokio::sync::Mutex; |
4 | 5 | use tokio_postgres::Client as PgClient; |
5 | 6 | use tokio_postgres::NoTls; |
6 | 7 |
|
| 8 | +use pony::config::settings::PostgresConfig; |
| 9 | +use pony::memory::node::Node; |
| 10 | +use pony::Connection; |
| 11 | +use pony::MemoryCache; |
| 12 | +use pony::NodeStorageOp; |
| 13 | +use pony::OperationStatus; |
| 14 | +use pony::Result; |
| 15 | + |
7 | 16 | use crate::core::postgres::connection::ConnRow; |
8 | 17 | use crate::core::postgres::connection::PgConn; |
9 | 18 | use crate::core::postgres::node::PgNode; |
10 | 19 | use crate::core::postgres::user::PgUser; |
11 | 20 | use crate::core::postgres::user::UserRow; |
12 | | -use crate::core::sync::SyncTask; |
13 | | -use crate::Result; |
14 | | -use pony::config::settings::PostgresConfig; |
15 | 21 |
|
16 | 22 | pub mod connection; |
17 | 23 | pub mod node; |
@@ -55,66 +61,50 @@ impl PgContext { |
55 | 61 | } |
56 | 62 | } |
57 | 63 |
|
58 | | -pub async fn run_shadow_sync(mut rx: mpsc::Receiver<SyncTask>, db: PgContext) { |
59 | | - while let Some(task) = rx.recv().await { |
60 | | - match task { |
61 | | - SyncTask::InsertUser { user_id, user } => { |
62 | | - let user_row: UserRow = (user_id, user).into(); |
63 | | - if let Err(err) = db.user().insert(user_row).await { |
64 | | - log::error!("Failed to sync InsertUser: {err}"); |
65 | | - } |
66 | | - } |
67 | | - SyncTask::UpdateUser { user_id, user } => { |
68 | | - if let Err(err) = db.user().update(&user_id, user).await { |
69 | | - log::error!("Failed to sync UpdateUser: {err}"); |
70 | | - } |
71 | | - } |
72 | | - SyncTask::DeleteUser { user_id } => { |
73 | | - if let Err(err) = db.user().delete(&user_id).await { |
74 | | - log::error!("Failed to sync DeleteUser: {err}"); |
75 | | - } |
76 | | - } |
77 | | - SyncTask::InsertNode { node_id, node } => { |
78 | | - if let Err(err) = db.node().insert(node_id, node).await { |
79 | | - log::error!("Failed to sync InsertNode: {err}"); |
80 | | - } |
81 | | - } |
82 | | - SyncTask::UpdateNodeStatus { |
83 | | - node_id, |
84 | | - env, |
85 | | - status, |
86 | | - } => { |
87 | | - if let Err(err) = db.node().update_status(&node_id, &env, status).await { |
88 | | - log::error!("Failed to sync UpdateNodeStatus: {err}"); |
89 | | - } |
90 | | - } |
91 | | - SyncTask::InsertConn { conn_id, conn } => { |
92 | | - let conn_row: ConnRow = (conn_id, conn).into(); |
93 | | - if let Err(err) = db.conn().insert(conn_row).await { |
94 | | - log::error!("Failed to sync InsertConn: {err}"); |
95 | | - } |
96 | | - } |
97 | | - SyncTask::UpdateConn { conn_id, conn } => { |
98 | | - let conn_row: ConnRow = (conn_id, conn).into(); |
99 | | - if let Err(err) = db.conn().update(conn_row).await { |
100 | | - log::error!("Failed to sync UpdateConn: {err}"); |
101 | | - } |
102 | | - } |
103 | | - SyncTask::DeleteConn { conn_id } => { |
104 | | - if let Err(err) = db.conn().delete(&conn_id).await { |
105 | | - log::error!("Failed to sync DeleteConn: {err}"); |
106 | | - } |
107 | | - } |
108 | | - SyncTask::UpdateConnStat { conn_id, stat } => { |
109 | | - if let Err(err) = db.conn().update_stat(&conn_id, stat).await { |
110 | | - log::error!("Failed to sync UpdateConnStat: {err}"); |
111 | | - } |
112 | | - } |
113 | | - SyncTask::UpdateConnStatus { conn_id, status } => { |
114 | | - if let Err(err) = db.conn().update_status(&conn_id, status).await { |
115 | | - log::error!("Failed to sync UpdateConnStatus: {err}"); |
116 | | - } |
| 64 | +#[async_trait::async_trait] |
| 65 | +pub trait Tasks { |
| 66 | + async fn add_node(&mut self, db_node: Node) -> Result<()>; |
| 67 | + async fn add_conn(&mut self, db_conn: ConnRow) -> Result<OperationStatus>; |
| 68 | + async fn add_user(&mut self, db_user: UserRow) -> Result<OperationStatus>; |
| 69 | +} |
| 70 | + |
| 71 | +#[async_trait::async_trait] |
| 72 | +impl Tasks for MemoryCache<HashMap<String, Vec<Node>>, Connection> { |
| 73 | + async fn add_user(&mut self, db_user: UserRow) -> Result<OperationStatus> { |
| 74 | + let user_id = db_user.user_id; |
| 75 | + let user = db_user.try_into()?; |
| 76 | + |
| 77 | + if self.users.contains_key(&user_id) { |
| 78 | + return Ok(OperationStatus::AlreadyExist(user_id)); |
| 79 | + } |
| 80 | + |
| 81 | + self.users.insert(user_id, user); |
| 82 | + Ok(OperationStatus::Ok(user_id)) |
| 83 | + } |
| 84 | + |
| 85 | + async fn add_conn(&mut self, db_conn: ConnRow) -> Result<OperationStatus> { |
| 86 | + let conn_id = db_conn.conn_id; |
| 87 | + let conn: Connection = db_conn.try_into()?; |
| 88 | + |
| 89 | + self.connections.add(&conn_id, conn.into()).map_err(|e| { |
| 90 | + format!( |
| 91 | + "Create: Failed to add connection {} to state: {}", |
| 92 | + conn_id, e |
| 93 | + ) |
| 94 | + .into() |
| 95 | + }) |
| 96 | + } |
| 97 | + async fn add_node(&mut self, db_node: Node) -> Result<()> { |
| 98 | + match self.nodes.add(db_node.clone()) { |
| 99 | + Ok(_) => { |
| 100 | + log::debug!("Node added to State: {}", db_node.uuid); |
| 101 | + Ok(()) |
117 | 102 | } |
| 103 | + Err(e) => Err(format!( |
| 104 | + "Create: Failed to add node {} to state: {}", |
| 105 | + db_node.uuid, e |
| 106 | + ) |
| 107 | + .into()), |
118 | 108 | } |
119 | 109 | } |
120 | 110 | } |
0 commit comments