dev #14
@ -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)
|
||||
})
|
||||
|
||||
@ -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,11 @@ where
|
||||
Some(thread) => Some(thread),
|
||||
None => {
|
||||
let handlers = (self.h_mapper)(bi.clone()).await;
|
||||
let handler =
|
||||
script_handler_gen(&bot_runner.controller, handlers.collect()).await;
|
||||
let handler = script_handler_gen(
|
||||
bot_runner.controller.runtime.clone(),
|
||||
handlers.collect(),
|
||||
)
|
||||
.await;
|
||||
Some(
|
||||
spawn_bot_thread(
|
||||
bot_runner.controller.bot.clone(),
|
||||
@ -142,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, 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?;
|
||||
@ -162,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()
|
||||
@ -181,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()
|
||||
|
||||
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, 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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user