Update init

This commit is contained in:
2025-03-29 21:40:19 +01:00
parent 2f5dac7b9d
commit 74eb71f966

View File

@@ -77,26 +77,31 @@ pub static BOTS_ROUTES: Lazy<Cache<String, StopTokenWithSender>> = Lazy::new(||
}); });
pub static BOTS_DATA: Lazy<Cache<String, BotData>> = Lazy::new(|| Cache::builder().build()); pub static BOTS_DATA: Lazy<Cache<String, BotData>> = Lazy::new(|| Cache::builder().build());
pub static INITED_BOTS_IDS: Lazy<Cache<u32, ()>> = Lazy::new(|| Cache::builder().build());
pub struct BotsManager; pub struct BotsManager;
impl BotsManager { impl BotsManager {
async fn check() { async fn check_bots_data(bots: &[BotData]) {
let bots_data = get_bots().await; for bot_data in bots.iter() {
if BOTS_DATA.contains_key(&bot_data.token) {
let bots_data = match bots_data { continue;
Ok(v) => v,
Err(err) => {
log::info!("{:?}", err);
return;
} }
};
let bot_data: BotData = bot_data.clone();
BOTS_DATA
.insert(bot_data.token.clone(), bot_data.clone())
.await;
}
}
async fn check_unininted(bots_data: &[BotData]) {
let semaphore = Arc::new(Semaphore::const_new(10)); let semaphore = Arc::new(Semaphore::const_new(10));
let mut set_webhook_tasks = JoinSet::new(); let mut set_webhook_tasks = JoinSet::new();
for bot_data in bots_data.iter() { for bot_data in bots_data.iter() {
if BOTS_DATA.contains_key(&bot_data.token) { if INITED_BOTS_IDS.contains_key(&bot_data.id) {
continue; continue;
} }
@@ -109,9 +114,7 @@ impl BotsManager {
let webhook_status = set_webhook(&bot_data).await; let webhook_status = set_webhook(&bot_data).await;
if webhook_status { if webhook_status {
BOTS_DATA INITED_BOTS_IDS.insert(bot_data.id, ()).await;
.insert(bot_data.token.clone(), bot_data.clone())
.await;
} }
drop(_permit); drop(_permit);
@@ -125,6 +128,21 @@ impl BotsManager {
} }
} }
async fn check() {
let bots_data = get_bots().await;
let bots_data = match bots_data {
Ok(v) => v,
Err(err) => {
log::info!("{:?}", err);
return;
}
};
let _ = BotsManager::check_bots_data(&bots_data).await;
let _ = BotsManager::check_unininted(&bots_data).await;
}
pub async fn stop_all() { pub async fn stop_all() {
for (_, (stop_token, _)) in BOTS_ROUTES.iter() { for (_, (stop_token, _)) in BOTS_ROUTES.iter() {
stop_token.stop(); stop_token.stop();