Skip to content

Commit b04bb66

Browse files
committed
add seperate desktop_session worker
1 parent d3978be commit b04bb66

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "background-tasks-rust"
33
version = "0.1.0"
44
edition = "2021"
5+
default-run = "background-tasks-rust"
56

67
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
78

@@ -62,3 +63,11 @@ features = ["rustls-tls", "json"]
6263

6364
[profile.dev.package.sqlx-macros]
6465
opt-level = 3
66+
67+
[[bin]]
68+
name = "background-tasks-rust"
69+
path = "src/main.rs"
70+
71+
[[bin]]
72+
name = "ea_desktop_session"
73+
path = "src/ea_desktop_session.rs"

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ HEALTHCHECK --interval=5m --timeout=3s --start-period=5s \
3333
# RUN ldconfig
3434
COPY server.pem .
3535
COPY --from=builder /usr/local/cargo/bin/background-tasks-rust /usr/local/bin/background-tasks-rust
36-
RUN apt-get update && apt-get upgrade -y && apt-get install --assume-yes curl protobuf-compiler libprotobuf-dev && apt-get clean
36+
RUN apt-get update && apt-get upgrade -y \
37+
&& apt-get install --assume-yes curl protobuf-compiler libprotobuf-dev \
38+
&& apt-get clean
3739
CMD background-tasks-rust

src/ea_desktop_session.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
mod check_ea_desktop_session;
2+
mod connectors;
3+
mod gatherer;
4+
mod structs;
5+
6+
use gatherer::battlefield_grpc_bf2042;
7+
use grpc_rust::access_token::ea_desktop_access_token;
8+
use std::{
9+
collections::HashMap,
10+
env,
11+
sync::{atomic, Arc},
12+
time::Duration,
13+
};
14+
use tokio::time::sleep;
15+
use warp::Filter;
16+
17+
use crate::connectors::mongo::MongoClient;
18+
19+
#[tokio::main]
20+
async fn main() -> anyhow::Result<()> {
21+
match dotenvy::dotenv() {
22+
Ok(_) => {}
23+
Err(_) => log::info!(".env not found, using env variables..."),
24+
};
25+
26+
let last_update = Arc::new(atomic::AtomicI64::new(chrono::Utc::now().timestamp() / 60));
27+
let last_update_clone = Arc::clone(&last_update);
28+
29+
flexi_logger::Logger::try_with_str("info")?.start()?;
30+
log::info!("Starting...");
31+
32+
tokio::spawn(async move {
33+
let hello = warp::any().map(move || {
34+
let last_update_i64 = last_update_clone.load(atomic::Ordering::Relaxed);
35+
let now_minutes = chrono::Utc::now().timestamp() / 60;
36+
37+
// error if 10 minutes without updates
38+
if (now_minutes - last_update_i64) > 10 {
39+
warp::reply::with_status(
40+
format!("{}", now_minutes - last_update_i64),
41+
warp::http::StatusCode::SERVICE_UNAVAILABLE,
42+
)
43+
} else {
44+
warp::reply::with_status(
45+
format!("{}", now_minutes - last_update_i64),
46+
warp::http::StatusCode::OK,
47+
)
48+
}
49+
});
50+
warp::serve(hello).run(([0, 0, 0, 0], 3030)).await;
51+
});
52+
let mut mongo_client = MongoClient::connect().await?;
53+
54+
let api_bf2042_account = env::var("API_BF2042_ACCOUNT").expect("API_BF2042_ACCOUNT wasn't set");
55+
56+
let (mut bf2042_cookie, mut ea_access_token) =
57+
match mongo_client.get_cookies(&api_bf2042_account).await {
58+
Ok(result) => result,
59+
Err(e) => {
60+
log::warn!("Cookie failed, {}", e);
61+
(
62+
bf_sparta::cookie::Cookie {
63+
sid: "".to_string(),
64+
remid: "".to_string(),
65+
},
66+
"".to_string(),
67+
)
68+
}
69+
};
70+
71+
if ea_access_token.is_empty() {
72+
match ea_desktop_access_token(bf2042_cookie.clone()).await {
73+
Ok(res) => {
74+
(ea_access_token, bf2042_cookie) = res;
75+
mongo_client
76+
.push_new_cookies(&api_bf2042_account, &bf2042_cookie, ea_access_token.clone())
77+
.await?;
78+
}
79+
Err(e) => log::error!("access_token for ea desktop failed: {:#?}", e),
80+
};
81+
}
82+
83+
let empty_game_hash: HashMap<String, String> = HashMap::new();
84+
let mut sessions: HashMap<String, HashMap<String, String>> = HashMap::new();
85+
86+
log::info!("Started");
87+
88+
loop {
89+
match battlefield_grpc_bf2042::check_session(
90+
sessions
91+
.get("kingston")
92+
.unwrap_or(&empty_game_hash)
93+
.to_owned(),
94+
bf2042_cookie.clone(),
95+
ea_access_token.clone(),
96+
)
97+
.await
98+
{
99+
Ok(session) => {
100+
sessions.insert("kingston".to_string(), session);
101+
last_update.store(
102+
chrono::Utc::now().timestamp() / 60,
103+
atomic::Ordering::Relaxed,
104+
);
105+
log::info!("kingston: Finished auth check!");
106+
}
107+
Err(e) => {
108+
log::error!("Failed kingston_grpc, auth_check reason: {:#?}", e);
109+
match ea_desktop_access_token(bf2042_cookie.clone()).await {
110+
Ok(res) => {
111+
(ea_access_token, bf2042_cookie) = res;
112+
mongo_client
113+
.push_new_cookies(
114+
&api_bf2042_account,
115+
&bf2042_cookie,
116+
ea_access_token.clone(),
117+
)
118+
.await?;
119+
}
120+
Err(e) => log::error!("access_token for ea desktop failed: {:#?}", e),
121+
};
122+
}
123+
};
124+
sleep(Duration::from_secs(30)).await;
125+
}
126+
}

0 commit comments

Comments
 (0)