From 048ce583e88e9143fb56ab14b45551b4b87d9832 Mon Sep 17 00:00:00 2001 From: toast Date: Fri, 2 Aug 2024 21:05:29 +1000 Subject: [PATCH] Block scheduler if another one is already running --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/internals/tasks/rss.rs | 2 +- src/main.rs | 38 ++++++++++++++++++++++++++------------ 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4d206d6..7643ef0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1020,7 +1020,7 @@ dependencies = [ [[package]] name = "kon" -version = "0.3.8" +version = "0.3.9" dependencies = [ "bb8", "bb8-postgres", diff --git a/Cargo.toml b/Cargo.toml index 185c130..3d03bc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kon" -version = "0.3.8" +version = "0.3.9" edition = "2021" [dependencies] diff --git a/src/internals/tasks/rss.rs b/src/internals/tasks/rss.rs index 520da8a..5f71d5c 100644 --- a/src/internals/tasks/rss.rs +++ b/src/internals/tasks/rss.rs @@ -281,7 +281,7 @@ async fn rust_message() -> Result, Error> { pub async fn rss(ctx: Arc) -> Result<(), Error> { let task_name = "RSS"; - let mut interval = interval(Duration::from_secs(900)); + let mut interval = interval(Duration::from_secs(900)); task_info(&task_name, "Task loaded!"); loop { diff --git a/src/main.rs b/src/main.rs index 0ad1076..c12d8cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,13 @@ use crate::{ use std::{ thread::current, - sync::Arc + sync::{ + Arc, + atomic::{ + AtomicBool, + Ordering + } + } }; use poise::serenity_prelude::{ builder::{ @@ -35,6 +41,7 @@ use poise::serenity_prelude::{ }; type Error = Box; +static TASK_RUNNING: AtomicBool = AtomicBool::new(false); async fn on_ready( ctx: &Context, @@ -97,7 +104,7 @@ async fn event_processor( let builder = poise::builtins::create_application_commands(&framework.options().commands); let commands = Command::set_global_commands(&ctx.http, builder).await; let mut commands_deployed = std::collections::HashSet::new(); - + match commands { Ok(cmdmap) => for command in cmdmap.iter() { commands_deployed.insert(command.name.clone()); @@ -107,7 +114,7 @@ async fn event_processor( new_message.reply(&ctx.http, "Deployment failed, check console for more details!").await?; } } - + if commands_deployed.len() > 0 { new_message.reply(&ctx.http, format!( "Deployed the commands globally:\n- {}", @@ -123,17 +130,24 @@ async fn event_processor( let ctx = Arc::new(ctx.clone()); - tokio::spawn(async move { - match internals::tasks::rss::rss(ctx).await { - Ok(_) => {}, - Err(y) => { - eprintln!("TaskScheduler[Main:RSS:Error]: Task execution failed: {}", y); - if let Some(source) = y.source() { - eprintln!("TaskScheduler[Main:RSS:Error]: Task execution failed caused by: {:#?}", source); + if !TASK_RUNNING.load(Ordering::SeqCst) { + TASK_RUNNING.store(true, Ordering::SeqCst); + + tokio::spawn(async move { + match internals::tasks::rss::rss(ctx).await { + Ok(_) => {}, + Err(y) => { + eprintln!("TaskScheduler[Main:RSS:Error]: Task execution failed: {}", y); + if let Some(source) = y.source() { + eprintln!("TaskScheduler[Main:RSS:Error]: Task execution failed caused by: {:#?}", source); + } } } - } - }); + TASK_RUNNING.store(false, Ordering::SeqCst); + }); + } else { + println!("TaskScheduler[Main:RSS:Notice]: Another thread is already running this task, ignoring"); + } } _ => {} }