feat: make the scheduler available to the api and the bot
This commit is contained in:
parent
13abeb42f3
commit
c99692fbea
@ -2,6 +2,7 @@ use crate::botv2::cmd::meme::{answer::answer, enable::enable, list::list};
|
||||
use crate::botv2::cmd::server_config::server::server;
|
||||
use crate::botv2::cmd::{help::help, ping::ping};
|
||||
use crate::config::Config;
|
||||
use crate::event::schedule_job::ScheduleJob;
|
||||
use crate::{botv2::handler::event_handler, img::config_file::ConfigFile};
|
||||
use poise::serenity_prelude::{self as serenity, Http};
|
||||
use serenity::GatewayIntents;
|
||||
@ -16,6 +17,7 @@ pub struct Data {
|
||||
pub config_img: ConfigFile,
|
||||
pub config: Config,
|
||||
pub entity_name: String,
|
||||
pub scheduler: ScheduleJob,
|
||||
}
|
||||
|
||||
// Types used by all command functions
|
||||
@ -34,7 +36,11 @@ async fn age(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn start_bot(config: Config, rx: oneshot::Receiver<()>) -> Arc<Http> {
|
||||
pub async fn start_bot(
|
||||
config: Config,
|
||||
rx: oneshot::Receiver<()>,
|
||||
scheduler: ScheduleJob,
|
||||
) -> Arc<Http> {
|
||||
let config_img = match fs::read_to_string(format!("{}/config.yaml", config.image.path)) {
|
||||
Ok(content) => content,
|
||||
Err(err) => {
|
||||
@ -86,6 +92,7 @@ pub async fn start_bot(config: Config, rx: oneshot::Receiver<()>) -> Arc<Http> {
|
||||
Ok(Data {
|
||||
config_img: config_parsed,
|
||||
config: config.clone(),
|
||||
scheduler,
|
||||
entity_name: format!(
|
||||
"{}-{}",
|
||||
config.bot_name.clone(),
|
||||
|
19
src/main.rs
19
src/main.rs
@ -35,26 +35,14 @@ async fn main() -> std::io::Result<()> {
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let mut cron_test = cron_scheduler.clone();
|
||||
match cron_test
|
||||
.add_cron_job(123, 123, "* * * * * *".to_string())
|
||||
.await
|
||||
{
|
||||
Ok(cron_uuid) => {
|
||||
info!("Cron job added: {:?}", cron_uuid);
|
||||
}
|
||||
Err(_) => {
|
||||
info!("Error adding cron job");
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let (tx_bot, rx_bot) = oneshot::channel();
|
||||
let http = start_bot(config.clone(), rx_bot).await;
|
||||
let http = start_bot(config.clone(), rx_bot, cron_scheduler.clone()).await;
|
||||
info!("API Server started on port {}", port);
|
||||
let mut openapi = ApiDocs::openapi();
|
||||
openapi.info.title = format!("{} Api", config.bot_name.clone());
|
||||
openapi.info.version = env!("CARGO_PKG_VERSION").to_string();
|
||||
let api_config = config.clone();
|
||||
let cron_api = cron_scheduler.clone();
|
||||
HttpServer::new(move || {
|
||||
let cors = Cors::default()
|
||||
.allow_any_header()
|
||||
@ -65,6 +53,7 @@ async fn main() -> std::io::Result<()> {
|
||||
App::new()
|
||||
.app_data(web::Data::new(api_config.clone()))
|
||||
.app_data(web::Data::new(http.clone()))
|
||||
.app_data(web::Data::new(cron_api.clone()))
|
||||
.wrap(cors)
|
||||
.service(swagger_ui)
|
||||
.service(
|
||||
@ -90,7 +79,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.bind(("0.0.0.0", port))?
|
||||
.run()
|
||||
.await?;
|
||||
cron_test.stop_scheduled_job(123, 123).await;
|
||||
cron_scheduler.stop_scheduled_job(123, 123).await;
|
||||
info!("API Server stopped.");
|
||||
tx_bot.send(()).unwrap();
|
||||
cron_scheduler.stop_cron_scheduler().await;
|
||||
|
Loading…
Reference in New Issue
Block a user