make BotRuntime for js runtime management
Some checks failed
Build && Deploy / cargo build (push) Has been cancelled

This commit is contained in:
Akulij 2025-06-01 12:06:38 +05:00
parent 8b871ec147
commit f8628a11f5
3 changed files with 38 additions and 24 deletions

View File

@ -17,14 +17,14 @@ use crate::{
commands::BotCommand, commands::BotCommand,
db::{CallDB, DB}, db::{CallDB, DB},
message_answerer::MessageAnswerer, message_answerer::MessageAnswerer,
update_user_tg, BotError, BotResult, update_user_tg, BotError, BotResult, BotRuntime,
}; };
pub type BotHandler = pub type BotHandler =
Handler<'static, DependencyMap, BotResult<()>, teloxide::dispatching::DpHandlerDescription>; Handler<'static, DependencyMap, BotResult<()>, teloxide::dispatching::DpHandlerDescription>;
pub fn script_handler(rc: Arc<RwLock<RunnerConfig>>) -> BotHandler { pub fn script_handler(r: Arc<BotRuntime>) -> BotHandler {
let crc = rc.clone(); let cr = r.clone();
dptree::entry() dptree::entry()
.branch( .branch(
Update::filter_message() Update::filter_message()
@ -32,10 +32,10 @@ pub fn script_handler(rc: Arc<RwLock<RunnerConfig>>) -> BotHandler {
.filter_map(|m: Message| m.text().and_then(|t| BotCommand::from_str(t).ok())) .filter_map(|m: Message| m.text().and_then(|t| BotCommand::from_str(t).ok()))
// check if command is presented in config // check if command is presented in config
.filter_map(move |bc: BotCommand| { .filter_map(move |bc: BotCommand| {
let rc = std::sync::Arc::clone(&rc); let r = std::sync::Arc::clone(&r);
let command = bc.command(); let command = bc.command();
let rc = rc.read().expect("RwLock lock on commands map failed"); let rc = r.rc.lock().expect("RwLock lock on commands map failed");
rc.get_command_message(command) rc.get_command_message(command)
}) })
@ -45,8 +45,8 @@ pub fn script_handler(rc: Arc<RwLock<RunnerConfig>>) -> BotHandler {
Update::filter_callback_query() Update::filter_callback_query()
.filter_map(move |q: CallbackQuery| { .filter_map(move |q: CallbackQuery| {
q.data.and_then(|data| { q.data.and_then(|data| {
let rc = std::sync::Arc::clone(&crc); let r = std::sync::Arc::clone(&cr);
let rc = rc.read().expect("RwLock lock on commands map failed"); let rc = r.rc.lock().expect("RwLock lock on commands map failed");
rc.get_callback_message(&data) rc.get_callback_message(&data)
}) })

View File

@ -1,4 +1,10 @@
use std::{collections::HashMap, future::Future, sync::RwLock, thread::JoinHandle, time::Duration}; use std::{
collections::HashMap,
future::Future,
sync::{Arc, RwLock},
thread::JoinHandle,
time::Duration,
};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::{error, info}; use log::{error, info};
@ -13,7 +19,7 @@ use crate::{
bot_handler::{script_handler, BotHandler}, bot_handler::{script_handler, BotHandler},
db::{bots::BotInstance, DbError, DB}, db::{bots::BotInstance, DbError, DB},
mongodb_storage::MongodbStorage, mongodb_storage::MongodbStorage,
BotController, BotError, BotResult, BotController, BotError, BotResult, BotRuntime,
}; };
pub struct BotRunner { pub struct BotRunner {
@ -114,8 +120,11 @@ where
Some(thread) => Some(thread), Some(thread) => Some(thread),
None => { None => {
let handlers = (self.h_mapper)(bi.clone()).await; let handlers = (self.h_mapper)(bi.clone()).await;
let handler = let handler = script_handler_gen(
script_handler_gen(&bot_runner.controller, handlers.collect()).await; bot_runner.controller.runtime.clone(),
handlers.collect(),
)
.await;
Some( Some(
spawn_bot_thread( spawn_bot_thread(
bot_runner.controller.bot.clone(), bot_runner.controller.bot.clone(),
@ -142,7 +151,7 @@ where
let db = db.clone().with_name(bi.name.clone()); let db = db.clone().with_name(bi.name.clone());
let controller = BotController::with_db(db.clone(), &bi.token, &bi.script).await?; let controller = BotController::with_db(db.clone(), &bi.token, &bi.script).await?;
let handler = script_handler_gen(&controller, plug_handlers).await; let handler = script_handler_gen(controller.runtime.clone(), plug_handlers).await;
let thread = let thread =
spawn_bot_thread(controller.bot.clone(), controller.db.clone(), handler).await?; spawn_bot_thread(controller.bot.clone(), controller.db.clone(), handler).await?;
@ -162,8 +171,8 @@ where
} }
} }
async fn script_handler_gen(c: &BotController, plug_handlers: Vec<BotHandler>) -> BotHandler { async fn script_handler_gen(r: Arc<BotRuntime>, plug_handlers: Vec<BotHandler>) -> BotHandler {
let handler = script_handler(c.rc.clone()); let handler = script_handler(r.clone());
// each handler will be added to dptree::entry() // each handler will be added to dptree::entry()
let handler = plug_handlers let handler = plug_handlers
.into_iter() .into_iter()
@ -181,7 +190,7 @@ pub async fn start_bot(
let db = db.clone().with_name(bi.name.clone()); let db = db.clone().with_name(bi.name.clone());
let controller = BotController::with_db(db.clone(), &bi.token, &bi.script).await?; let controller = BotController::with_db(db.clone(), &bi.token, &bi.script).await?;
let handler = script_handler(controller.rc.clone()); let handler = script_handler(controller.runtime.clone());
// each handler will be added to dptree::entry() // each handler will be added to dptree::entry()
let handler = plug_handlers let handler = plug_handlers
.into_iter() .into_iter()

View File

@ -19,7 +19,7 @@ use db::message_forward::MessageForward;
use handlers::admin::admin_handler; use handlers::admin::admin_handler;
use log::{error, info, warn}; use log::{error, info, warn};
use message_answerer::MessageAnswerer; use message_answerer::MessageAnswerer;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, Mutex, RwLock};
use utils::create_callback_button; use utils::create_callback_button;
use crate::db::{CallDB, DB}; use crate::db::{CallDB, DB};
@ -104,10 +104,17 @@ type CallbackStore = CallbackInfo<Callback>;
pub struct BotController { pub struct BotController {
pub bot: Bot, pub bot: Bot,
pub db: DB, pub db: DB,
pub rc: Arc<RwLock<RunnerConfig>>, pub runtime: Arc<BotRuntime>,
}
pub struct BotRuntime {
pub rc: Mutex<RunnerConfig>,
pub runner: Runner, pub runner: Runner,
} }
unsafe impl Send for BotRuntime {}
unsafe impl Sync for BotRuntime {}
impl Drop for BotController { impl Drop for BotController {
fn drop(&mut self) { fn drop(&mut self) {
info!("called drop for BotController"); info!("called drop for BotController");
@ -141,14 +148,12 @@ impl BotController {
let mut runner = Runner::init_with_db(&mut db)?; let mut runner = Runner::init_with_db(&mut db)?;
runner.call_attacher(|c, o| attach_user_application(c, o, &db, &bot))??; runner.call_attacher(|c, o| attach_user_application(c, o, &db, &bot))??;
let rc = runner.init_config(script)?; let rc = runner.init_config(script)?;
let rc = Arc::new(RwLock::new(rc)); let runtime = Arc::new(BotRuntime {
rc: Mutex::new(rc),
Ok(Self {
bot,
db,
rc,
runner, runner,
}) });
Ok(Self { bot, db, runtime })
} }
} }