Compare commits

...

3 Commits

Author SHA1 Message Date
bf816c4c5c Merge pull request 'dev' (#14) from dev into main
All checks were successful
Build && Deploy / cargo build (push) Successful in 1m0s
Reviewed-on: #14
2025-06-01 07:07:17 +00:00
Akulij
f8628a11f5 make BotRuntime for js runtime management
Some checks failed
Build && Deploy / cargo build (push) Has been cancelled
2025-06-01 12:06:38 +05:00
Akulij
8b871ec147 fix: unnecessary BotController clone for script handler generation
All checks were successful
Build && Deploy / cargo build (push) Successful in 1m1s
2025-06-01 11:42:50 +05:00
3 changed files with 38 additions and 25 deletions

View File

@ -17,14 +17,14 @@ use crate::{
commands::BotCommand,
db::{CallDB, DB},
message_answerer::MessageAnswerer,
update_user_tg, BotError, BotResult,
update_user_tg, BotError, BotResult, BotRuntime,
};
pub type BotHandler =
Handler<'static, DependencyMap, BotResult<()>, teloxide::dispatching::DpHandlerDescription>;
pub fn script_handler(rc: Arc<RwLock<RunnerConfig>>) -> BotHandler {
let crc = rc.clone();
pub fn script_handler(r: Arc<BotRuntime>) -> BotHandler {
let cr = r.clone();
dptree::entry()
.branch(
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()))
// check if command is presented in config
.filter_map(move |bc: BotCommand| {
let rc = std::sync::Arc::clone(&rc);
let r = std::sync::Arc::clone(&r);
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)
})
@ -45,8 +45,8 @@ pub fn script_handler(rc: Arc<RwLock<RunnerConfig>>) -> BotHandler {
Update::filter_callback_query()
.filter_map(move |q: CallbackQuery| {
q.data.and_then(|data| {
let rc = std::sync::Arc::clone(&crc);
let rc = rc.read().expect("RwLock lock on commands map failed");
let r = std::sync::Arc::clone(&cr);
let rc = r.rc.lock().expect("RwLock lock on commands map failed");
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 log::{error, info};
@ -13,7 +19,7 @@ use crate::{
bot_handler::{script_handler, BotHandler},
db::{bots::BotInstance, DbError, DB},
mongodb_storage::MongodbStorage,
BotController, BotError, BotResult,
BotController, BotError, BotResult, BotRuntime,
};
pub struct BotRunner {
@ -114,8 +120,10 @@ where
Some(thread) => Some(thread),
None => {
let handlers = (self.h_mapper)(bi.clone()).await;
let handler =
script_handler_gen(bot_runner.controller.clone(), handlers.collect())
let handler = script_handler_gen(
bot_runner.controller.runtime.clone(),
handlers.collect(),
)
.await;
Some(
spawn_bot_thread(
@ -143,7 +151,7 @@ where
let db = db.clone().with_name(bi.name.clone());
let controller = BotController::with_db(db.clone(), &bi.token, &bi.script).await?;
let handler = script_handler_gen(controller.clone(), plug_handlers).await;
let handler = script_handler_gen(controller.runtime.clone(), plug_handlers).await;
let thread =
spawn_bot_thread(controller.bot.clone(), controller.db.clone(), handler).await?;
@ -163,8 +171,8 @@ where
}
}
async fn script_handler_gen(c: BotController, plug_handlers: Vec<BotHandler>) -> BotHandler {
let handler = script_handler(c.rc.clone());
async fn script_handler_gen(r: Arc<BotRuntime>, plug_handlers: Vec<BotHandler>) -> BotHandler {
let handler = script_handler(r.clone());
// each handler will be added to dptree::entry()
let handler = plug_handlers
.into_iter()
@ -182,7 +190,7 @@ pub async fn start_bot(
let db = db.clone().with_name(bi.name.clone());
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()
let handler = plug_handlers
.into_iter()

View File

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