use std::sync::{Arc, atomic::Ordering};
use super::Registry;
use crate::identity::TaskId;
impl Registry {
pub async fn wait_until_empty(&self) {
loop {
let notified = self.empty_notify.notified();
tokio::pin!(notified);
notified.as_mut().enable();
if self.is_empty().await {
return;
}
notified.await;
}
}
pub async fn list(&self) -> Vec<(TaskId, Arc<str>)> {
let st = self.state.read().await;
let mut tasks: Vec<(TaskId, Arc<str>)> = st
.tasks
.iter()
.map(|(id, entry)| (*id, Arc::clone(&entry.label)))
.collect();
drop(st);
tasks.sort_by_key(|(id, _)| *id);
tasks
}
pub(in crate::core) async fn is_alive(&self, label: &str) -> bool {
let state = self.state.read().await;
let registered = state.by_label.get(label).is_some_and(|id| {
state
.tasks
.get(id)
.is_some_and(|entry| entry.activity.load(Ordering::Acquire))
});
drop(state);
registered || self.actors.attempt_reaper().is_alive(label)
}
pub(in crate::core) async fn alive_snapshot(&self) -> Vec<Arc<str>> {
let state = self.state.read().await;
let mut alive: Vec<_> = state
.tasks
.values()
.filter(|entry| entry.activity.load(Ordering::Acquire))
.map(|entry| Arc::clone(&entry.label))
.collect();
drop(state);
alive.extend(self.actors.attempt_reaper().alive_labels());
alive.sort_unstable();
alive.dedup();
alive
}
#[cfg(test)]
pub async fn contains(&self, id: TaskId) -> bool {
self.state.read().await.tasks.contains_key(&id)
}
#[cfg(test)]
pub async fn id_for_label(&self, label: &str) -> Option<TaskId> {
self.state.read().await.by_label.get(label).copied()
}
pub async fn is_empty(&self) -> bool {
self.state.read().await.tasks.is_empty()
}
}