synchronization buf fixed

This commit is contained in:
병준 박 2022-08-31 02:41:53 +00:00
parent 0b16750e01
commit 6edcc2d615
7 changed files with 557 additions and 301 deletions

View File

@ -59,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let manager = ConnectionManager::<PgConnection>::new(url_db);
let pool = Pool::builder()
.max_size(4)
.max_size(10)
.test_on_check_out(true)
.build(manager)?;
let conn = pool.get()?;

View File

@ -2,6 +2,7 @@ use super::schema::api_kgon_synchronizations;
pub static ITEM_MEMBERS: &str = "members";
pub static ITEM_BALANCE_PARTNER: &str = "balance_partner";
pub static ITEM_BALANCE_USER_ALL: &str = "balance_user_all";
pub static ITEM_BALANCE_USER: &str = "balance_user";
pub static ITEM_VENDORS: &str = "vendors";
pub static ITEM_GAMES: &str = "games";

View File

@ -54,18 +54,35 @@ impl Synchronizer {
let start_at = (chrono::Utc::now()).timestamp();
if let Err(e) = async {
let conn = self.pool.get().expect("conn");
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("pool.get error: {}", e)),
});
}
};
let sdate = match self
.synchronization_history_repository
.select_latest_by_item(
&conn,
repositories::synchronization::models::ITEM_BETTING_HISTORY.to_string(),
)
.expect("synchronization_repository.select_by_item")
{
) {
Ok(v) => match v {
Some(s) => s.data,
None => None,
},
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!(
"synchronization_history_repository.select_latest_by_item error: {}",
e
)),
});
}
};
let req = api::betting::models::ListBettingsRequest {
@ -76,7 +93,15 @@ impl Synchronizer {
username: None,
limit: 2000,
};
let res = self.betting_api.list_bettings(req).await?;
let res = match self.betting_api.list_bettings(req).await {
Ok(v) => v,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("betting_api.list_bettings error: {:?}", e)),
});
}
};
let last_object_id = res.last_object_id;
let mut last_utc_created_at: Option<String> = None;
@ -94,15 +119,22 @@ impl Synchronizer {
match chrono::NaiveDateTime::parse_from_str(&b.created_at, "%Y-%m-%d %H:%M:%S") {
Ok(t) => t.timestamp(),
Err(e) => {
println!("NaiveDateTime::parse_from_str error: {}", e);
continue;
return Err(api::core::models::Error {
code: -1,
msg: Some(format!(
"chrono::NaiveDateTime::parse_from_str error: {}",
e
)),
});
}
};
let utc_created_at = match chrono::DateTime::parse_from_rfc3339(b.utc_created_at.as_str()) {
Ok(t) => t.timestamp(),
Err(e) => {
println!("chrono::DateTime::parse_from_rfc3339 error: {}", e);
continue;
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("chrono::DateTime::parse_from_rfc3339 error: {}", e)),
});
}
};
@ -134,15 +166,21 @@ impl Synchronizer {
}
if !new_betting_history.is_empty() {
self
match self
.betting_history_repository
.inserts(&conn, &new_betting_history)
.expect("betting_history_repository.inserts");
{
Ok(v) => v,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("betting_history_repository.inserts error: {:?}", e)),
});
}
};
}
self
.synchronization_history_repository
.insert(
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_BETTING_HISTORY.to_string(),
@ -152,17 +190,23 @@ impl Synchronizer {
message: None,
data: last_utc_created_at,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
Ok::<(), api::core::models::Error>(())
}
.await
{
let conn = self.pool.get().expect("conn");
self
.synchronization_history_repository
.insert(
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
println!("pool.get error: {}", e);
return Ok(());
}
};
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_BETTING_HISTORY.to_string(),
@ -172,8 +216,9 @@ impl Synchronizer {
message: e.msg,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
}
Ok(())

View File

@ -57,19 +57,32 @@ impl Synchronizer {
let start_at = (chrono::Utc::now()).timestamp();
if let Err(e) = async {
let conn = self.pool.get().expect("conn");
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("pool.get error: {}", e)),
});
}
};
let vendors = self
.vendor_repository
.select_all(
let vendors = match self.vendor_repository.select_all(
&conn,
&repositories::vendor::models::FindAll {
pagination: None,
sorts: None,
search: None,
},
)
.expect("vendor select_all");
) {
Ok(v) => v,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("vendor_repository.select_all error: {}", e)),
});
}
};
let mut upsert_games: Vec<repositories::game::models::UpsertGame> = vec![];
@ -77,13 +90,23 @@ impl Synchronizer {
let req = api::game::models::ListGamesRequest {
vendor_key: v.key.clone(),
};
let res = self.game_api.list_games(req).await?;
let res = match self.game_api.list_games(req).await {
Ok(v) => v,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("game_api.list_games error: {:?}", e)),
});
}
};
for g in res.games {
match upsert_games.iter().find(|v| v.id.eq(&g.id)) {
Some(d) => {
println!("id is duplicated. e: {:?}, n: {:?}", d, g);
continue;
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("id is duplicated. e: {:?}, n: {:?}", d, g)),
});
}
None => {}
};
@ -101,14 +124,17 @@ impl Synchronizer {
}
}
let _affected = self
.game_repository
.upserts(&conn, upsert_games)
.expect("game upsert");
let _affected = match self.game_repository.upserts(&conn, upsert_games) {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("game_repository.upserts error: {}", e)),
});
}
};
self
.synchronization_history_repository
.insert(
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_GAMES.to_string(),
@ -118,17 +144,23 @@ impl Synchronizer {
message: None,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
Ok::<(), api::core::models::Error>(())
}
.await
{
let conn = self.pool.get().expect("conn");
self
.synchronization_history_repository
.insert(
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
println!("pool.get error: {}", e);
return Ok(());
}
};
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_GAMES.to_string(),
@ -138,8 +170,9 @@ impl Synchronizer {
message: e.msg,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
}
Ok(())

View File

@ -63,16 +63,37 @@ impl Synchronizer {
let start_at = (chrono::Utc::now()).timestamp();
if let Err(e) = async {
let conn = self.pool.get().expect("conn");
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("pool.get error: {}", e)),
});
}
};
let req = api::member::models::ListMembersRequest { group_key: None };
let res = self.member_api.list_members(req).await?;
let res = match self.member_api.list_members(req).await {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("member_api.list_members: {:?}", e)),
});
}
};
for u in res.users {
if self
.member_repository
.select(&conn, u.id)
.expect("member_repository.select")
if match self.member_repository.select(&conn, u.id) {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("member_repository.select error: {}", e)),
});
}
}
.is_none()
{
let ss_get_member_by_username_req = bpr::ss::member::GetMemberByUsernameRequest {
@ -100,8 +121,13 @@ impl Synchronizer {
{
Ok(r) => r,
Err(e) => {
println!("bpr::ss::member::SUBJECT_GET_MEMBER_BY_USERNAME: {}", e);
continue;
return Err(api::core::models::Error {
code: -1,
msg: Some(format!(
"bpr::ss::member::SUBJECT_GET_MEMBER_BY_USERNAME: {}",
e
)),
});
}
};
@ -111,17 +137,24 @@ impl Synchronizer {
) {
Ok(r) => r,
Err(e) => {
println!("bpr::ss::member::GetMemberByUsernameResponse: {}", e);
continue;
return Err(api::core::models::Error {
code: -1,
msg: Some(format!(
"bpr::ss::member::GetMemberByUsernameResponse: {}",
e
)),
});
}
};
if let Some(e) = ss_get_member_by_username_res.error {
println!(
return Err(api::core::models::Error {
code: -1,
msg: Some(format!(
"bpr::ss::member::SUBJECT_GET_MEMBER_BY_USERNAME error: {}",
e
);
continue;
)),
});
}
match ss_get_member_by_username_res.result {
@ -138,19 +171,25 @@ impl Synchronizer {
) {
Ok(r) => r,
Err(e) => {
println!("member_repository.insert: {}", e);
continue;
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("member_repository.insert error: {}", e)),
});
}
};
}
None => {
println!("member is not exist: {}", u.site_username);
continue;
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("member is not exist: {}", u.site_username)),
});
}
},
None => {
println!("member is not exist: {}", u.site_username);
continue;
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("member is not exist: {}", u.site_username)),
});
}
};
}
@ -169,9 +208,7 @@ impl Synchronizer {
.expect("member update");
}
self
.synchronization_history_repository
.insert(
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_MEMBERS.to_string(),
@ -181,17 +218,23 @@ impl Synchronizer {
message: None,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
Ok::<(), api::core::models::Error>(())
}
.await
{
let conn = self.pool.get().expect("conn");
self
.synchronization_history_repository
.insert(
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
println!("pool.get error: {}", e);
return Ok(());
}
};
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_MEMBERS.to_string(),
@ -201,8 +244,9 @@ impl Synchronizer {
message: e.msg,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
}
Ok(())

View File

@ -60,12 +60,26 @@ impl Synchronizer {
let start_at = (chrono::Utc::now()).timestamp();
if let Err(e) = async {
let conn = self.pool.get().expect("conn");
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("pool.get error: {}", e)),
});
}
};
let req = api::member::models::ListMembersRequest { group_key: None };
let res = self.member_api.list_members(req).await?;
let mut messages: Vec<String> = vec![];
let res = match self.member_api.list_members(req).await {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("member_api.list_members: {:?}", e)),
});
}
};
for u in res.users {
let req = api::member_account::models::GetBalanceForUserRequest {
@ -74,11 +88,13 @@ impl Synchronizer {
let res = match self.member_account_api.get_balance_for_user(req).await {
Ok(r) => r,
Err(e) => {
messages.push(format!(
"api.get_balance_for_user code: {}, msg: {:?}",
e.code, e.msg
));
continue;
return Err(api::core::models::Error {
code: -1,
msg: Some(format!(
"member_account_api.get_balance_for_user error: {:?}",
e
)),
});
}
};
@ -89,56 +105,59 @@ impl Synchronizer {
companies: res.companies,
};
if let Err(e) = self
match self
.member_repository
.update_balance(&conn, u.id, &modify_member)
{
messages.push(format!("repository.update_balance error: {}", e));
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("member_repository.update_balance error: {}", e)),
});
}
};
}
if !messages.is_empty() {
return Err(api::core::models::Error {
code: -1,
msg: Some(serde_json::to_string(&messages).expect("json")),
});
}
self
.synchronization_history_repository
.insert(
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_BALANCE_USER.to_string(),
item: repositories::synchronization::models::ITEM_BALANCE_USER_ALL.to_string(),
start_at,
complete_at: (chrono::Utc::now()).timestamp(),
code: 0,
message: None,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
Ok::<(), api::core::models::Error>(())
}
.await
{
let conn = self.pool.get().expect("conn");
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
println!("pool.get error: {}", e);
return Ok(());
}
};
self
.synchronization_history_repository
.insert(
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_BALANCE_USER.to_string(),
item: repositories::synchronization::models::ITEM_BALANCE_USER_ALL.to_string(),
start_at,
complete_at: (chrono::Utc::now()).timestamp(),
code: e.code,
message: e.msg,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
}
Ok(())
@ -152,23 +171,46 @@ impl Synchronizer {
let start_at = (chrono::Utc::now()).timestamp();
if let Err(e) = async {
let conn = self.pool.get().expect("conn");
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("pool.get error: {}", e)),
});
}
};
let m = match self
.member_repository
.select_by_member_id(&conn, member_id)
.expect("member_repository.select_by_member_id")
{
let m = match self.member_repository.select_by_member_id(&conn, member_id) {
Ok(c) => match c {
Some(m) => m,
None => {
return Ok(());
}
},
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!(
"member_repository.select_by_member_id error: {}",
e
)),
});
}
};
let req = api::member_account::models::GetBalanceForUserRequest {
username: usename.to_string(),
};
let res = self.member_account_api.get_balance_for_user(req).await?;
let res = match self.member_account_api.get_balance_for_user(req).await {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("member_account_api.get_balance_for_user: {:?}", e)),
});
}
};
let modify_member = repositories::member::models::ModifyMemberForBalance {
balance: res.balance,
@ -177,20 +219,46 @@ impl Synchronizer {
companies: res.companies,
};
self
match self
.member_repository
.update_balance(&conn, m.id, &modify_member)
.expect("member_repository.update_balance");
{
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("member_repository.update_balance: {}", e)),
});
}
};
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_BALANCE_USER.to_string(),
start_at,
complete_at: (chrono::Utc::now()).timestamp(),
code: 0,
message: None,
data: None,
},
) {
println!("synchronization_history.insert error: {}", e);
};
Ok::<(), api::core::models::Error>(())
}
.await
{
let conn = self.pool.get().expect("conn");
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
println!("pool.get error: {}", e);
return Ok(());
}
};
self
.synchronization_history_repository
.insert(
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_BALANCE_USER.to_string(),
@ -200,8 +268,9 @@ impl Synchronizer {
message: e.msg,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
}
Ok(())
@ -211,46 +280,77 @@ impl Synchronizer {
let start_at = (chrono::Utc::now()).timestamp();
if let Err(e) = async {
let conn = self.pool.get().expect("conn");
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("pool.get error: {}", e)),
});
}
};
let req = api::member_account::models::GetBalanceForPartnerRequest {};
let res = self.member_account_api.get_balance_for_partner(req).await?;
let res = match self.member_account_api.get_balance_for_partner(req).await {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!(
"member_account_api.get_balance_for_partner: {:?}",
e
)),
});
}
};
match self
.balance_repository
.select(&conn)
.expect("balance select")
{
match self.balance_repository.select(&conn) {
Ok(c) => match c {
Some(b) => {
self
.balance_repository
.update(
match self.balance_repository.update(
&conn,
b.id,
&repositories::balance::models::ModifyBalance {
balance: res.balance,
balance_bota: res.balance_bota,
},
)
.expect("balance update");
) {
Ok(_c) => {}
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("balance_repository.update error: {}", e)),
});
}
}
}
None => {
self
.balance_repository
.insert(
match self.balance_repository.insert(
&conn,
&repositories::balance::models::NewBalance {
balance: res.balance,
balance_bota: res.balance_bota,
},
)
.expect("balance insert");
) {
Ok(_c) => {}
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("balance_repository.insert error: {}", e)),
});
}
};
}
},
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("member_repository.select error: {}", e)),
});
}
};
self
.synchronization_history_repository
.insert(
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_BALANCE_PARTNER.to_string(),
@ -260,17 +360,23 @@ impl Synchronizer {
message: None,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
Ok::<(), api::core::models::Error>(())
}
.await
{
let conn = self.pool.get().expect("conn");
self
.synchronization_history_repository
.insert(
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
println!("pool.get error: {}", e);
return Ok(());
}
};
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_BALANCE_PARTNER.to_string(),
@ -280,8 +386,9 @@ impl Synchronizer {
message: e.msg,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
}
Ok(())

View File

@ -54,10 +54,26 @@ impl Synchronizer {
let start_at = (chrono::Utc::now()).timestamp();
if let Err(e) = async {
let conn = self.pool.get().expect("conn");
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("pool.get error: {}", e)),
});
}
};
let req = api::vendor::models::ListVendorsRequest {};
let res = self.vendor_api.list_vendors(req).await?;
let res = match self.vendor_api.list_vendors(req).await {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("vendor_api.list_vendors: {:?}", e)),
});
}
};
let upsert_vendors: Vec<repositories::vendor::models::UpsertVendor> = res
.vendors
@ -76,14 +92,17 @@ impl Synchronizer {
})
.collect();
let _affected = self
.vendor_repository
.upserts(&conn, upsert_vendors)
.expect("vendor upsert");
let _affected = match self.vendor_repository.upserts(&conn, upsert_vendors) {
Ok(c) => c,
Err(e) => {
return Err(api::core::models::Error {
code: -1,
msg: Some(format!("vendor_repository.upserts: {:?}", e)),
});
}
};
self
.synchronization_history_repository
.insert(
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_VENDORS.to_string(),
@ -93,17 +112,23 @@ impl Synchronizer {
message: None,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
Ok::<(), api::core::models::Error>(())
}
.await
{
let conn = self.pool.get().expect("conn");
self
.synchronization_history_repository
.insert(
let conn = match self.pool.get() {
Ok(c) => c,
Err(e) => {
println!("pool.get error: {}", e);
return Ok(());
}
};
if let Err(e) = self.synchronization_history_repository.insert(
&conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item: repositories::synchronization::models::ITEM_VENDORS.to_string(),
@ -113,8 +138,9 @@ impl Synchronizer {
message: e.msg,
data: None,
},
)
.expect("synchronization_history insert");
) {
println!("synchronization_history.insert error: {}", e);
};
}
Ok(())