use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
const PASS: Duration = Duration::from_secs(30);
static RUNNING: AtomicBool = AtomicBool::new(false);
pub(super) fn ensure_running() {
if cfg!(test)
|| RUNNING
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.is_err()
{
return;
}
let spawned = std::thread::Builder::new()
.name("cloud-task-claims".into())
.spawn(|| {
loop {
std::thread::sleep(PASS);
if super::renew_own_due() == 0 {
RUNNING.store(false, Ordering::Release);
if super::own_claims() == 0
|| RUNNING
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.is_err()
{
return;
}
}
}
});
if spawned.is_err() {
RUNNING.store(false, Ordering::Release);
}
}