From c99692fbea2b559127860227cdd467b7440fc4e1 Mon Sep 17 00:00:00 2001 From: Max batleforc Date: Mon, 24 Jun 2024 19:51:28 +0200 Subject: [PATCH] feat: make the scheduler available to the api and the bot --- src/botv2/init.rs | 9 ++++++++- src/main.rs | 19 ++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/botv2/init.rs b/src/botv2/init.rs index f34bf62..7ac8d95 100644 --- a/src/botv2/init.rs +++ b/src/botv2/init.rs @@ -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 { +pub async fn start_bot( + config: Config, + rx: oneshot::Receiver<()>, + scheduler: ScheduleJob, +) -> Arc { 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 { Ok(Data { config_img: config_parsed, config: config.clone(), + scheduler, entity_name: format!( "{}-{}", config.bot_name.clone(), diff --git a/src/main.rs b/src/main.rs index de01bc7..bb704c4 100644 --- a/src/main.rs +++ b/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;