Compare commits
No commits in common. "bf816c4c5c008ba96932bdf6f90e8ffd3de78e08" and "92c97abd4a58bc9b0fe75e686a65703b636a1877" have entirely different histories.
bf816c4c5c
...
92c97abd4a
@ -17,14 +17,14 @@ use crate::{
|
||||
commands::BotCommand,
|
||||
db::{CallDB, DB},
|
||||
message_answerer::MessageAnswerer,
|
||||
update_user_tg, BotError, BotResult, BotRuntime,
|
||||
update_user_tg, BotError, BotResult,
|
||||
};
|
||||
|
||||
pub type BotHandler =
|
||||
Handler<'static, DependencyMap, BotResult<()>, teloxide::dispatching::DpHandlerDescription>;
|
||||
|
||||
pub fn script_handler(r: Arc<BotRuntime>) -> BotHandler {
|
||||
let cr = r.clone();
|
||||
pub fn script_handler(rc: Arc<RwLock<RunnerConfig>>) -> BotHandler {
|
||||
let crc = rc.clone();
|
||||
dptree::entry()
|
||||
.branch(
|
||||
Update::filter_message()
|
||||
@ -32,10 +32,10 @@ pub fn script_handler(r: Arc<BotRuntime>) -> 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 r = std::sync::Arc::clone(&r);
|
||||
let rc = std::sync::Arc::clone(&rc);
|
||||
let command = bc.command();
|
||||
|
||||
let rc = r.rc.lock().expect("RwLock lock on commands map failed");
|
||||
let rc = rc.read().expect("RwLock lock on commands map failed");
|
||||
|
||||
rc.get_command_message(command)
|
||||
})
|
||||
@ -45,8 +45,8 @@ pub fn script_handler(r: Arc<BotRuntime>) -> BotHandler {
|
||||
Update::filter_callback_query()
|
||||
.filter_map(move |q: CallbackQuery| {
|
||||
q.data.and_then(|data| {
|
||||
let r = std::sync::Arc::clone(&cr);
|
||||
let rc = r.rc.lock().expect("RwLock lock on commands map failed");
|
||||
let rc = std::sync::Arc::clone(&crc);
|
||||
let rc = rc.read().expect("RwLock lock on commands map failed");
|
||||
|
||||
rc.get_callback_message(&data)
|
||||
})
|
||||
|
||||
@ -1,10 +1,4 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
future::Future,
|
||||
sync::{Arc, RwLock},
|
||||
thread::JoinHandle,
|
||||
time::Duration,
|
||||
};
|
||||
use std::{collections::HashMap, future::Future, sync::RwLock, thread::JoinHandle, time::Duration};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use log::{error, info};
|
||||
@ -19,7 +13,7 @@ use crate::{
|
||||
bot_handler::{script_handler, BotHandler},
|
||||
db::{bots::BotInstance, DbError, DB},
|
||||
mongodb_storage::MongodbStorage,
|
||||
BotController, BotError, BotResult, BotRuntime,
|
||||
BotController, BotError, BotResult,
|
||||
};
|
||||
|
||||
pub struct BotRunner {
|
||||
@ -120,11 +114,9 @@ where
|
||||
Some(thread) => Some(thread),
|
||||
None => {
|
||||
let handlers = (self.h_mapper)(bi.clone()).await;
|
||||
let handler = script_handler_gen(
|
||||
bot_runner.controller.runtime.clone(),
|
||||
handlers.collect(),
|
||||
)
|
||||
.await;
|
||||
let handler =
|
||||
script_handler_gen(bot_runner.controller.clone(), handlers.collect())
|
||||
.await;
|
||||
Some(
|
||||
spawn_bot_thread(
|
||||
bot_runner.controller.bot.clone(),
|
||||
@ -151,7 +143,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.runtime.clone(), plug_handlers).await;
|
||||
let handler = script_handler_gen(controller.clone(), plug_handlers).await;
|
||||
|
||||
let thread =
|
||||
spawn_bot_thread(controller.bot.clone(), controller.db.clone(), handler).await?;
|
||||
@ -171,8 +163,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
async fn script_handler_gen(r: Arc<BotRuntime>, plug_handlers: Vec<BotHandler>) -> BotHandler {
|
||||
let handler = script_handler(r.clone());
|
||||
async fn script_handler_gen(c: BotController, plug_handlers: Vec<BotHandler>) -> BotHandler {
|
||||
let handler = script_handler(c.rc.clone());
|
||||
// each handler will be added to dptree::entry()
|
||||
let handler = plug_handlers
|
||||
.into_iter()
|
||||
@ -190,7 +182,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.runtime.clone());
|
||||
let handler = script_handler(controller.rc.clone());
|
||||
// each handler will be added to dptree::entry()
|
||||
let handler = plug_handlers
|
||||
.into_iter()
|
||||
|
||||
23
src/main.rs
23
src/main.rs
@ -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, Mutex, RwLock};
|
||||
use std::sync::{Arc, RwLock};
|
||||
use utils::create_callback_button;
|
||||
|
||||
use crate::db::{CallDB, DB};
|
||||
@ -104,17 +104,10 @@ type CallbackStore = CallbackInfo<Callback>;
|
||||
pub struct BotController {
|
||||
pub bot: Bot,
|
||||
pub db: DB,
|
||||
pub runtime: Arc<BotRuntime>,
|
||||
}
|
||||
|
||||
pub struct BotRuntime {
|
||||
pub rc: Mutex<RunnerConfig>,
|
||||
pub rc: Arc<RwLock<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");
|
||||
@ -148,12 +141,14 @@ 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 runtime = Arc::new(BotRuntime {
|
||||
rc: Mutex::new(rc),
|
||||
runner,
|
||||
});
|
||||
let rc = Arc::new(RwLock::new(rc));
|
||||
|
||||
Ok(Self { bot, db, runtime })
|
||||
Ok(Self {
|
||||
bot,
|
||||
db,
|
||||
rc,
|
||||
runner,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user