Block scheduler if another one is already running
All checks were successful
Build and push container image / build (push) Successful in 12m40s

This commit is contained in:
toast 2024-08-02 21:05:29 +10:00
parent 483ba390e9
commit 048ce583e8
4 changed files with 29 additions and 15 deletions

2
Cargo.lock generated
View File

@ -1020,7 +1020,7 @@ dependencies = [
[[package]] [[package]]
name = "kon" name = "kon"
version = "0.3.8" version = "0.3.9"
dependencies = [ dependencies = [
"bb8", "bb8",
"bb8-postgres", "bb8-postgres",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "kon" name = "kon"
version = "0.3.8" version = "0.3.9"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -281,7 +281,7 @@ async fn rust_message() -> Result<Option<String>, Error> {
pub async fn rss(ctx: Arc<Context>) -> Result<(), Error> { pub async fn rss(ctx: Arc<Context>) -> Result<(), Error> {
let task_name = "RSS"; 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!"); task_info(&task_name, "Task loaded!");
loop { loop {

View File

@ -17,7 +17,13 @@ use crate::{
use std::{ use std::{
thread::current, thread::current,
sync::Arc sync::{
Arc,
atomic::{
AtomicBool,
Ordering
}
}
}; };
use poise::serenity_prelude::{ use poise::serenity_prelude::{
builder::{ builder::{
@ -35,6 +41,7 @@ use poise::serenity_prelude::{
}; };
type Error = Box<dyn std::error::Error + Send + Sync>; type Error = Box<dyn std::error::Error + Send + Sync>;
static TASK_RUNNING: AtomicBool = AtomicBool::new(false);
async fn on_ready( async fn on_ready(
ctx: &Context, ctx: &Context,
@ -97,7 +104,7 @@ async fn event_processor(
let builder = poise::builtins::create_application_commands(&framework.options().commands); let builder = poise::builtins::create_application_commands(&framework.options().commands);
let commands = Command::set_global_commands(&ctx.http, builder).await; let commands = Command::set_global_commands(&ctx.http, builder).await;
let mut commands_deployed = std::collections::HashSet::new(); let mut commands_deployed = std::collections::HashSet::new();
match commands { match commands {
Ok(cmdmap) => for command in cmdmap.iter() { Ok(cmdmap) => for command in cmdmap.iter() {
commands_deployed.insert(command.name.clone()); 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?; new_message.reply(&ctx.http, "Deployment failed, check console for more details!").await?;
} }
} }
if commands_deployed.len() > 0 { if commands_deployed.len() > 0 {
new_message.reply(&ctx.http, format!( new_message.reply(&ctx.http, format!(
"Deployed the commands globally:\n- {}", "Deployed the commands globally:\n- {}",
@ -123,17 +130,24 @@ async fn event_processor(
let ctx = Arc::new(ctx.clone()); let ctx = Arc::new(ctx.clone());
tokio::spawn(async move { if !TASK_RUNNING.load(Ordering::SeqCst) {
match internals::tasks::rss::rss(ctx).await { TASK_RUNNING.store(true, Ordering::SeqCst);
Ok(_) => {},
Err(y) => { tokio::spawn(async move {
eprintln!("TaskScheduler[Main:RSS:Error]: Task execution failed: {}", y); match internals::tasks::rss::rss(ctx).await {
if let Some(source) = y.source() { Ok(_) => {},
eprintln!("TaskScheduler[Main:RSS:Error]: Task execution failed caused by: {:#?}", source); 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");
}
} }
_ => {} _ => {}
} }