Skip to content

Commit

Permalink
change format
Browse files Browse the repository at this point in the history
  • Loading branch information
“Giems” committed Mar 21, 2024
1 parent c39f2cf commit 68193d1
Show file tree
Hide file tree
Showing 14 changed files with 621 additions and 562 deletions.
110 changes: 59 additions & 51 deletions server/src/http/cloud/events/processors/process_event_app_connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,71 +119,79 @@ pub async fn process_event_app_connect(

async fn save_event_app_connect(db: &Arc<Db>, app_id: &String, event: &AppConnectEvent) {
// Establish a new transaction
match db.connection_pool.begin().await {
Ok(mut tx) => {
// Create a new event index in the database
match db
.create_new_event_entry(&mut tx, &app_id, &EventType::AppConnect)
.await
{
Ok(event_id) => {
// Now create a new event app connect corresponding to the event
match db
.create_new_event_app_connect(
&mut tx,
event_id,
&event.session_id,
&event
.device_metadata
.to_string()
.unwrap_or("Failed to serialize device metadata".to_string()),
&event.language,
&event.timezone,
event.new_session,
)
.await
{
Ok(_) => {
// Commit the transaction
if let Err(err) = tx.commit().await {
error!(
"Failed to commit transaction for new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
let mut tx = match db.connection_pool.begin().await {
Ok(tx) => tx,
Err(err) => {
error!(
"Failed to create new transaction to save app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
return;
}
};

return;
}
Err(err) => {
error!(
"Failed to create new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
}
Err(err) => {
error!(
"Failed to create new event index, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
// Create a new event index
let event_id = match db
.create_new_event_entry(&mut tx, &app_id, &EventType::AppConnect)
.await
{
Ok(event_id) => event_id,
Err(err) => {
error!(
"Failed to create new event index, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);

// If we have not returned yet, then we have failed to save the event
// Rollback the transaction
if let Err(err) = tx.rollback().await {
error!(
"Failed to rollback transaction for new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}

return;
}
};

// Now create a new event app connect corresponding to the event
match db
.create_new_event_app_connect(
&mut tx,
event_id,
&event.session_id,
&event
.device_metadata
.to_string()
.unwrap_or("Failed to serialize device metadata".to_string()),
&event.language,
&event.timezone,
event.new_session,
)
.await
{
Ok(_) => {
// Commit the transaction
if let Err(err) = tx.commit().await {
error!(
"Failed to commit transaction for new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
Err(err) => {
error!(
"Failed to create new transaction to save app connection event, app_id: [{}], event: [{:?}], err: [{}]",
"Failed to create new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);

// Rollback the transaction
if let Err(err) = tx.rollback().await {
error!(
"Failed to rollback transaction for new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,71 +12,79 @@ pub async fn process_event_app_disconnect(
// Save event to Db
save_event_app_disconnect(db, app_id, event).await;

// Close app connection in the database
// Close app disconnect in the database
if let Err(err) = db.close_app_connection(&event.session_id, &app_id).await {
error!(
"Failed to close app connection, app_id: [{}], ip: [{}], event: [{:?}], err: [{}]",
"Failed to close app disconnect, app_id: [{}], ip: [{}], event: [{:?}], err: [{}]",
app_id, ip, event, err
);
}
}

async fn save_event_app_disconnect(db: &Arc<Db>, app_id: &String, event: &AppDisconnectEvent) {
// Establish a new transaction
match db.connection_pool.begin().await {
Ok(mut tx) => {
// Create a new event index in the database
match db
.create_new_event_entry(&mut tx, &app_id, &EventType::AppDisconnect)
.await
{
Ok(event_id) => {
// Now create a new event app disconnect corresponding to the event
match db
.create_new_event_app_disconnect(&mut tx, event_id, &event.session_id)
.await
{
Ok(_) => {
// Commit the transaction
if let Err(err) = tx.commit().await {
error!(
"Failed to commit transaction for new app disconnect event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
let mut tx = match db.connection_pool.begin().await {
Ok(tx) => tx,
Err(err) => {
error!(
"Failed to create new transaction to save app disconnect event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
return;
}
};

return;
}
Err(err) => {
error!(
"Failed to create new app disconnect event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
}
Err(err) => {
error!(
"Failed to create new event index, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
// Create a new event index
let event_id = match db
.create_new_event_entry(&mut tx, &app_id, &EventType::AppDisconnect)
.await
{
Ok(event_id) => event_id,
Err(err) => {
error!(
"Failed to create new event index, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);

// If we have not returned yet, then we have failed to save the event
// Rollback the transaction
if let Err(err) = tx.rollback().await {
error!(
"Failed to rollback transaction for new app disconnect event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}

return;
}
};

// Now create a new event app disconnect corresponding to the event
match db
.create_new_event_app_disconnect(&mut tx, event_id, &event.session_id)
.await
{
Ok(_) => {
// Commit the transaction
if let Err(err) = tx.commit().await {
error!(
"Failed to commit transaction for new app disconnect event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
Err(err) => {
error!(
"Failed to create new transaction to save app disconnect event, app_id: [{}], event: [{:?}], err: [{}]",
"Failed to create new app disconnect event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);

// Rollback the transaction
if let Err(err) = tx.rollback().await {
error!(
"Failed to rollback transaction for new app disconnect event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ pub async fn process_event_change_network_resolve(
// Commit the transaction
if let Err(err) = tx.commit().await {
error!(
"Failed to commit transaction for update change network event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
"Failed to commit transaction for update change network event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
Err(err) => {
Expand Down
106 changes: 57 additions & 49 deletions server/src/http/cloud/events/processors/process_event_change_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,77 @@ use std::sync::Arc;

pub async fn process_event_change_wallet(event: &ChangeWalletEvent, app_id: &String, db: &Arc<Db>) {
// Establish a new transaction
match db.connection_pool.begin().await {
Ok(mut tx) => {
// Create a new event index in the database
match db
.create_new_event_entry(&mut tx, &app_id, &EventType::ChangeWallet)
.await
{
Ok(event_id) => {
// Now create a new event change wallet corresponding to the event
match db
.create_new_event_change_wallet(
&mut tx,
event_id,
&event.session_id,
&event.request_id,
&event.network,
&event.wallet_name,
&event.wallet_type,
&event.old_wallet_address,
)
.await
{
Ok(_) => {
// Commit the transaction
if let Err(err) = tx.commit().await {
error!(
"Failed to commit transaction for new change wallet event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
let mut tx = match db.connection_pool.begin().await {
Ok(tx) => tx,
Err(err) => {
error!(
"Failed to create new transaction to save change wallet event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
return;
}
};

return;
}
Err(err) => {
error!(
"Failed to create new change wallet event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
}
Err(err) => {
error!(
"Failed to create new event index, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
// Create a new event index
let event_id = match db
.create_new_event_entry(&mut tx, &app_id, &EventType::ChangeWallet)
.await
{
Ok(event_id) => event_id,
Err(err) => {
error!(
"Failed to create new event index, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);

// If we have not returned yet, then we have failed to save the event
// Rollback the transaction
if let Err(err) = tx.rollback().await {
error!(
"Failed to rollback transaction for new change wallet event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}

return;
}
};

// Now create a new event change wallet corresponding to the event
match db
.create_new_event_change_wallet(
&mut tx,
event_id,
&event.session_id,
&event.request_id,
&event.network,
&event.wallet_name,
&event.wallet_type,
&event.old_wallet_address,
)
.await
{
Ok(_) => {
// Commit the transaction
if let Err(err) = tx.commit().await {
error!(
"Failed to commit transaction for new change wallet event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
Err(err) => {
error!(
"Failed to create new transaction to save change wallet event, app_id: [{}], event: [{:?}], err: [{}]",
"Failed to create new change wallet event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);

// Rollback the transaction
if let Err(err) = tx.rollback().await {
error!(
"Failed to rollback transaction for new change wallet event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
}
}
Loading

0 comments on commit 68193d1

Please sign in to comment.