taskflow_rs/scheduler/
running_tasks.rs

1use std::collections::HashSet;
2use std::sync::Arc;
3use tokio::sync::RwLock;
4
5#[derive(Clone)]
6pub struct RunningTasks {
7    inner: Arc<RwLock<HashSet<String>>>,
8}
9
10impl RunningTasks {
11    pub fn new() -> Self {
12        Self {
13            inner: Arc::new(RwLock::new(HashSet::new())),
14        }
15    }
16
17    pub async fn add(&self, task_id: String) {
18        let mut running = self.inner.write().await;
19        running.insert(task_id);
20    }
21
22    pub async fn remove(&self, task_id: &str) {
23        let mut running = self.inner.write().await;
24        running.remove(task_id);
25    }
26
27    pub async fn contains(&self, task_id: &str) -> bool {
28        let running = self.inner.read().await;
29        running.contains(task_id)
30    }
31
32    pub async fn len(&self) -> usize {
33        let running = self.inner.read().await;
34        running.len()
35    }
36
37    pub async fn is_empty(&self) -> bool {
38        let running = self.inner.read().await;
39        running.is_empty()
40    }
41
42    pub async fn clear(&self) {
43        let mut running = self.inner.write().await;
44        running.clear();
45    }
46}