use std::collections::HashSet;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Clone)]
pub struct RunningTasks {
inner: Arc<RwLock<HashSet<String>>>,
}
impl RunningTasks {
pub fn new() -> Self {
Self {
inner: Arc::new(RwLock::new(HashSet::new())),
}
}
pub async fn add(&self, task_id: String) {
let mut running = self.inner.write().await;
running.insert(task_id);
}
pub async fn remove(&self, task_id: &str) {
let mut running = self.inner.write().await;
running.remove(task_id);
}
pub async fn contains(&self, task_id: &str) -> bool {
let running = self.inner.read().await;
running.contains(task_id)
}
pub async fn len(&self) -> usize {
let running = self.inner.read().await;
running.len()
}
pub async fn is_empty(&self) -> bool {
let running = self.inner.read().await;
running.is_empty()
}
pub async fn clear(&self) {
let mut running = self.inner.write().await;
running.clear();
}
}