use super::super::types::DagStatus;
use super::DagEngine;
pub(crate) const MAX_TERMINAL_RUNS: usize = 50;
impl DagEngine {
pub(crate) fn evict(&self, session_id: Option<&str>) -> Vec<String> {
let evicted: Vec<String> = {
let mut inner = self.inner.lock();
let mut terminal: Vec<(i64, String)> = inner
.runs
.iter()
.filter(|(_, run)| {
run.session_id.as_deref() == session_id
&& matches!(
run.status,
DagStatus::Completed | DagStatus::Failed | DagStatus::Cancelled
)
})
.map(|(id, run)| (run.created_at, id.clone()))
.collect();
if terminal.len() <= MAX_TERMINAL_RUNS {
Vec::new()
} else {
terminal.sort_by_key(|(created_at, _)| *created_at);
let overflow = terminal.len() - MAX_TERMINAL_RUNS;
let ids: Vec<String> = terminal
.into_iter()
.take(overflow)
.map(|(_, id)| id)
.collect();
for id in &ids {
inner.runs.remove(id);
}
ids
}
};
if !evicted.is_empty() {
self.notify_persist();
}
evicted
}
pub fn clear_session_runs(&self, session_id: Option<&str>, keep: usize) -> usize {
let removed: Vec<String> = {
let mut inner = self.inner.lock();
let mut terminal: Vec<(i64, String)> = inner
.runs
.iter()
.filter(|(_, run)| {
run.session_id.as_deref() == session_id
&& matches!(
run.status,
DagStatus::Completed | DagStatus::Failed | DagStatus::Cancelled
)
})
.map(|(id, run)| (run.created_at, id.clone()))
.collect();
terminal.sort_by_key(|(created_at, _)| *created_at);
let overflow = terminal.len().saturating_sub(keep);
let ids: Vec<String> = terminal
.into_iter()
.take(overflow)
.map(|(_, id)| id)
.collect();
for id in &ids {
inner.runs.remove(id);
}
ids
};
if !removed.is_empty() {
self.notify_persist();
}
removed.len()
}
pub fn clear_run(&self, run_id: &str) -> bool {
let removed = {
let mut inner = self.inner.lock();
let Some(run) = inner.runs.get(run_id) else {
return false;
};
let terminal = matches!(
run.status,
DagStatus::Completed | DagStatus::Failed | DagStatus::Cancelled
);
if terminal {
inner.runs.remove(run_id);
}
terminal
};
if removed {
self.notify_persist();
}
removed
}
}