diff --git a/src/event/schedule_job.rs b/src/event/schedule_job.rs index a69b1c2..d8a275e 100644 --- a/src/event/schedule_job.rs +++ b/src/event/schedule_job.rs @@ -70,7 +70,12 @@ impl ScheduleJob { } #[instrument(skip(self))] - pub async fn add_cron_job(&mut self, server_id: u64, channel_id: u64, cron_expression: String) { + pub async fn add_cron_job( + &mut self, + server_id: u64, + channel_id: u64, + cron_expression: String, + ) -> Result { let job = match Job::new_async(cron_expression.as_str(), |uuid, _l| { Box::pin(async move { info!("Cron job fired: {:?}", uuid); @@ -79,7 +84,7 @@ impl ScheduleJob { Ok(job) => job, Err(e) => { error!("Error creating cron job: {:?}", e); - return; + return Err(()); } }; @@ -87,9 +92,11 @@ impl ScheduleJob { Ok(job_uid) => { info!("Cron job added"); self.job_id.insert((server_id, channel_id), job_uid); + Ok(job_uid) } Err(e) => { error!("Error adding cron job: {:?}", e); + Err(()) } } } diff --git a/src/main.rs b/src/main.rs index 8b35036..de01bc7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,9 +35,19 @@ async fn main() -> std::io::Result<()> { return Ok(()); } }; - cron_scheduler + let mut cron_test = cron_scheduler.clone(); + match cron_test .add_cron_job(123, 123, "* * * * * *".to_string()) - .await; + .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; info!("API Server started on port {}", port); @@ -80,6 +90,7 @@ async fn main() -> std::io::Result<()> { .bind(("0.0.0.0", port))? .run() .await?; + cron_test.stop_scheduled_job(123, 123).await; info!("API Server stopped."); tx_bot.send(()).unwrap(); cron_scheduler.stop_cron_scheduler().await;