use camino::{Utf8Path, Utf8PathBuf};
use jiff::{SignedDuration, Timestamp};
use serde::{Deserialize, Serialize};
use crate::domain::ownership::Sha256;
use crate::domain::paths::{SELF_DEPEND_CACHE_DIR, SELF_DEPEND_STAMP_DIR};
pub const REFRESH_AFTER: SignedDuration = SignedDuration::from_hours(7 * 24);
pub const RETRY_AFTER: SignedDuration = SignedDuration::from_hours(1);
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Entry {
#[serde(default)]
pub last_attempt: Option<String>,
#[serde(default)]
pub last_success: Option<String>,
#[serde(default)]
pub latest: Option<String>,
#[serde(default)]
pub error: Option<String>,
}
fn time(text: Option<&String>) -> Option<Timestamp> {
text.and_then(|text| text.parse().ok())
}
impl Entry {
#[must_use]
pub fn attempted_at(&self) -> Option<Timestamp> {
time(self.last_attempt.as_ref())
}
#[must_use]
pub fn succeeded_at(&self) -> Option<Timestamp> {
time(self.last_success.as_ref())
}
#[must_use]
pub fn next_try(&self) -> Option<Timestamp> {
self.error.as_ref()?;
self.attempted_at()?
.checked_add(RETRY_AFTER)
.ok()?
.round(jiff::Unit::Second)
.ok()
}
#[must_use]
pub fn latest_version(&self) -> Option<semver::Version> {
self.latest.as_deref()?.parse().ok()
}
}
fn key(target: &Utf8Path) -> String {
let digest = Sha256::of(target.as_str().as_bytes());
digest.as_str()[..16].to_string()
}
#[must_use]
pub fn path(cache_root: &Utf8Path, target: &Utf8Path) -> Utf8PathBuf {
cache_root
.join(SELF_DEPEND_CACHE_DIR)
.join(format!("{}.json", key(target)))
}
#[must_use]
pub fn lock_path(cache: &Utf8Path) -> Utf8PathBuf {
cache.with_extension("lock")
}
#[must_use]
pub fn read(path: &Utf8Path) -> Option<Entry> {
serde_json::from_str(&std::fs::read_to_string(path).ok()?).ok()
}
pub fn write(path: &Utf8Path, entry: &Entry) -> std::io::Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
match std::fs::remove_file(scratch_path(path)) {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(error),
}
let mut text = serde_json::to_string_pretty(entry).map_err(std::io::Error::other)?;
text.push('\n');
crate::adapters::fs::write_atomic(path, text.as_bytes())
}
fn scratch_path(path: &Utf8Path) -> Utf8PathBuf {
path.with_extension(format!("{}.sdd-tmp", path.extension().unwrap_or_default()))
}
#[must_use]
pub fn due(entry: Option<&Entry>, now: Timestamp) -> bool {
let Some(entry) = entry else {
return true;
};
if entry.error.is_some() {
return entry
.attempted_at()
.is_none_or(|attempt| now.duration_since(attempt) >= RETRY_AFTER);
}
entry
.succeeded_at()
.is_none_or(|success| now.duration_since(success) >= REFRESH_AFTER)
}
#[derive(Debug)]
pub struct Lock {
_file: std::fs::File,
}
pub fn try_lock(path: &Utf8Path) -> std::io::Result<Option<Lock>> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(false)
.open(path)?;
match file.try_lock() {
Ok(()) => Ok(Some(Lock { _file: file })),
Err(std::fs::TryLockError::WouldBlock) => Ok(None),
Err(std::fs::TryLockError::Error(error)) => Err(error),
}
}
#[must_use]
pub fn retired_stamps(state_root: &Utf8Path) -> Vec<Utf8PathBuf> {
let dir = state_root.join(SELF_DEPEND_STAMP_DIR);
let Ok(entries) = dir.read_dir_utf8() else {
return Vec::new();
};
let mut found: Vec<Utf8PathBuf> = entries
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_file()))
.map(|entry| entry.path().to_owned())
.filter(|path| path.extension() == Some("stamp"))
.collect();
found.sort();
found
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, reason = "a test panics as its failure signal")]
use super::*;
fn at(now: Timestamp, ago: SignedDuration) -> Option<String> {
now.checked_sub(ago).ok().map(|time| time.to_string())
}
#[test]
fn a_refresh_is_due_weekly_after_success_and_hourly_after_failure() {
let now = Timestamp::now();
assert!(due(None, now));
assert!(due(Some(&Entry::default()), now));
let fresh = Entry {
last_attempt: at(now, SignedDuration::from_hours(6 * 24)),
last_success: at(now, SignedDuration::from_hours(6 * 24)),
latest: Some("1.0.0".to_string()),
error: None,
};
assert!(!due(Some(&fresh), now));
let week = Entry {
last_success: at(now, REFRESH_AFTER),
..fresh
};
assert!(due(Some(&week), now));
let failed = Entry {
last_attempt: at(now, SignedDuration::from_mins(59)),
last_success: at(now, SignedDuration::from_hours(8 * 24)),
latest: None,
error: Some("down".to_string()),
};
assert!(!due(Some(&failed), now));
let hour = Entry {
last_attempt: at(now, RETRY_AFTER),
..failed
};
assert!(due(Some(&hour), now));
}
#[test]
fn two_checkouts_keep_two_caches() {
let root = Utf8Path::new("/cache");
assert_ne!(
path(root, Utf8Path::new("/work/a")),
path(root, Utf8Path::new("/work/b"))
);
let one = path(root, Utf8Path::new("/work/a"));
assert_eq!(lock_path(&one).extension(), Some("lock"));
}
#[test]
fn a_leftover_scratch_file_does_not_block_the_cache() {
let dir = tempfile::tempdir().unwrap();
let root = Utf8Path::from_path(dir.path()).unwrap();
let cache = root.join("x.json");
std::fs::write(scratch_path(&cache), "half").unwrap();
let entry = Entry {
latest: Some("1.0.0".to_string()),
..Entry::default()
};
write(&cache, &entry).unwrap();
assert_eq!(read(&cache), Some(entry));
assert!(!scratch_path(&cache).exists());
}
#[test]
fn a_second_lock_on_the_file_fails_while_the_first_is_held() {
let dir = tempfile::tempdir().unwrap();
let root = Utf8Path::from_path(dir.path()).unwrap();
let lock = root.join("x.lock");
let held = try_lock(&lock).unwrap();
assert!(held.is_some());
assert!(
try_lock(&lock).unwrap().is_none(),
"a held lock was taken twice"
);
drop(held);
assert!(lock.exists(), "releasing the lock removed its file");
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
while try_lock(&lock).unwrap().is_none() {
assert!(
std::time::Instant::now() < deadline,
"a released lock stayed held"
);
std::thread::sleep(std::time::Duration::from_millis(1));
}
}
}