feat: make add cron return cronjob uuid

This commit is contained in:
Max batleforc 2024-06-24 19:39:00 +02:00
parent d4b7364719
commit 13abeb42f3
No known key found for this signature in database
GPG Key ID: 25D243AB4B6AC9E7
2 changed files with 22 additions and 4 deletions

View File

@ -70,7 +70,12 @@ impl ScheduleJob {
} }
#[instrument(skip(self))] #[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<Uuid, ()> {
let job = match Job::new_async(cron_expression.as_str(), |uuid, _l| { let job = match Job::new_async(cron_expression.as_str(), |uuid, _l| {
Box::pin(async move { Box::pin(async move {
info!("Cron job fired: {:?}", uuid); info!("Cron job fired: {:?}", uuid);
@ -79,7 +84,7 @@ impl ScheduleJob {
Ok(job) => job, Ok(job) => job,
Err(e) => { Err(e) => {
error!("Error creating cron job: {:?}", e); error!("Error creating cron job: {:?}", e);
return; return Err(());
} }
}; };
@ -87,9 +92,11 @@ impl ScheduleJob {
Ok(job_uid) => { Ok(job_uid) => {
info!("Cron job added"); info!("Cron job added");
self.job_id.insert((server_id, channel_id), job_uid); self.job_id.insert((server_id, channel_id), job_uid);
Ok(job_uid)
} }
Err(e) => { Err(e) => {
error!("Error adding cron job: {:?}", e); error!("Error adding cron job: {:?}", e);
Err(())
} }
} }
} }

View File

@ -35,9 +35,19 @@ async fn main() -> std::io::Result<()> {
return Ok(()); return Ok(());
} }
}; };
cron_scheduler let mut cron_test = cron_scheduler.clone();
match cron_test
.add_cron_job(123, 123, "* * * * * *".to_string()) .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 (tx_bot, rx_bot) = oneshot::channel();
let http = start_bot(config.clone(), rx_bot).await; let http = start_bot(config.clone(), rx_bot).await;
info!("API Server started on port {}", port); info!("API Server started on port {}", port);
@ -80,6 +90,7 @@ async fn main() -> std::io::Result<()> {
.bind(("0.0.0.0", port))? .bind(("0.0.0.0", port))?
.run() .run()
.await?; .await?;
cron_test.stop_scheduled_job(123, 123).await;
info!("API Server stopped."); info!("API Server stopped.");
tx_bot.send(()).unwrap(); tx_bot.send(()).unwrap();
cron_scheduler.stop_cron_scheduler().await; cron_scheduler.stop_cron_scheduler().await;