use super::*;
#[cfg(test)]
thread_local! {
static POLLABILITY_VISITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
#[cfg(test)]
pub(crate) fn take_pollability_visits() -> usize {
POLLABILITY_VISITS.with(|visits| visits.replace(0))
}
pub fn session_target_is_pollable(session: &mj_core::state::SessionRecord) -> bool {
#[cfg(test)]
POLLABILITY_VISITS.with(|visits| visits.set(visits.get() + 1));
session.state.is_active()
&& !matches!(
session.state,
SessionState::Error
| SessionState::Provisioning
| SessionState::Destroying
| SessionState::Parked
)
&& session.target.is_some()
}
pub fn spawn_aws_resource_options_resolution(
config: Config,
target_id: String,
updates: tokio::sync::mpsc::UnboundedSender<(
String,
std::result::Result<Vec<SessionResourceAllocation>, String>,
)>,
tracker: mj_client::operations::CriticalOperationTracker,
) {
let cancelled = Arc::new(AtomicBool::new(false));
let guard = tracker.begin_cancellable(
format!("resolving resources for {target_id}"),
cancelled.clone(),
);
let _task = tokio::task::spawn_blocking(move || {
let controller = Controller {
config,
state: State::default(),
};
let result = controller
.resolve_aws_resource_options(&target_id, &CancellableProcessExecutor::new(cancelled))
.map_err(|error| format!("{error:#}"));
if let Err(error) = updates.send((target_id.clone(), result)) {
tracing::debug!(target_id, %error, "AWS resource options result dropped after dashboard shutdown");
}
drop(guard);
});
}