mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 15:26:09 +00:00
fix(daemon): leave repos alone while grove add or clone is still building them
Add a .grove-busy marker held by migrate and clone/init, treat a repo as ready only with .bare/HEAD and no marker or .grove-stage, warn once on stale markers. Also re-watch recreated dirs by inode, watch shallow dirs before discovery, and retry failed watches with a rate-limited log. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3784e1533b
commit
e2abeed414
8 files changed
+335
-45
No files matched your search
@@ -0,0 +1,88 @@
|
||||
//! The "busy" marker: `<repo>/.grove-busy` exists while grove restructures a repo (`grove add`,
|
||||
//! clone/import, init). The daemon treats such a repo as not ready and leaves it alone until the
|
||||
//! marker is gone, so it never fetches, materializes or snapshots a half-built layout.
|
||||
//!
|
||||
//! The marker holds `<pid> <start unix seconds>`. A marker whose process is gone is *stale*: the
|
||||
//! operation was interrupted, the repo may be half-migrated, and nothing removes the marker
|
||||
//! automatically.
|
||||
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub const BUSY: &str = ".grove-busy";
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum BusyState {
|
||||
Free,
|
||||
/// The marker's process is still running.
|
||||
Active,
|
||||
/// The marker's process is gone: an interrupted operation.
|
||||
Stale,
|
||||
}
|
||||
|
||||
/// Holds the marker; removes it on drop, so also on error paths and rollback.
|
||||
#[derive(Debug)]
|
||||
pub struct BusyGuard(PathBuf);
|
||||
|
||||
impl BusyGuard {
|
||||
/// Creates `<dir>/.grove-busy` (and `dir` if needed). Fails if the marker already exists.
|
||||
pub fn acquire(dir: &Path) -> io::Result<BusyGuard> {
|
||||
fs::create_dir_all(dir)?;
|
||||
let path = dir.join(BUSY);
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_secs());
|
||||
let mut f = fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(&path)?;
|
||||
io::Write::write_all(&mut f, format!("{} {now}\n", std::process::id()).as_bytes())?;
|
||||
Ok(BusyGuard(path))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BusyGuard {
|
||||
fn drop(&mut self) {
|
||||
let _ = fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// State of the marker in `dir`.
|
||||
pub fn busy_state(dir: &Path) -> BusyState {
|
||||
let Ok(text) = fs::read_to_string(dir.join(BUSY)) else {
|
||||
return if dir.join(BUSY).exists() {
|
||||
BusyState::Stale
|
||||
} else {
|
||||
BusyState::Free
|
||||
};
|
||||
};
|
||||
match text
|
||||
.split_whitespace()
|
||||
.next()
|
||||
.and_then(|p| p.parse::<u32>().ok())
|
||||
{
|
||||
Some(pid) if Path::new(&format!("/proc/{pid}")).exists() => BusyState::Active,
|
||||
_ => BusyState::Stale,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn marker_lifecycle() {
|
||||
let d = std::env::temp_dir().join(format!("grove-busy-{}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&d);
|
||||
assert_eq!(busy_state(&d), BusyState::Free);
|
||||
let g = BusyGuard::acquire(&d).unwrap();
|
||||
assert_eq!(busy_state(&d), BusyState::Active);
|
||||
assert!(BusyGuard::acquire(&d).is_err());
|
||||
drop(g);
|
||||
assert_eq!(busy_state(&d), BusyState::Free);
|
||||
fs::write(d.join(BUSY), "999999999 1\n").unwrap();
|
||||
assert_eq!(busy_state(&d), BusyState::Stale);
|
||||
fs::remove_dir_all(&d).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Sync engine core: git plumbing wrapper, config, snapshots and state classification.
|
||||
|
||||
pub mod busy;
|
||||
pub mod config;
|
||||
pub mod git;
|
||||
pub mod inspect;
|
||||
|
||||
@@ -19,6 +19,7 @@ use std::process::Command;
|
||||
|
||||
const BARE: &str = ".bare";
|
||||
const STAGE: &str = ".grove-stage";
|
||||
const BUSY: &str = crate::busy::BUSY;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum MigrateError {
|
||||
@@ -477,7 +478,9 @@ fn migrate_inner(
|
||||
let (backup_tar, backup_ref, linked_backups) =
|
||||
backup(&root, &git, machine, backup_dir, stamp, &linked)?;
|
||||
|
||||
let dir = dir_name(&branch, &[BARE, STAGE]);
|
||||
let dir = dir_name(&branch, &[BARE, STAGE, BUSY]);
|
||||
// From here on the daemon must leave the repo alone; dropped last, also after a rollback.
|
||||
let _busy = crate::busy::BusyGuard::acquire(&root)?;
|
||||
let mut p = Progress {
|
||||
orig_ids: list_ids(&root.join(".git")),
|
||||
..Progress::default()
|
||||
@@ -573,7 +576,7 @@ fn run_steps(
|
||||
// 1. Move everything except .git into a staging dir.
|
||||
fs::create_dir(&stage).map_err(s)?;
|
||||
p.staged = true;
|
||||
move_entries(root, &stage, &[".git", STAGE]).map_err(s)?;
|
||||
move_entries(root, &stage, &[".git", STAGE, BUSY]).map_err(s)?;
|
||||
hook(inject, Step::Staged)?;
|
||||
|
||||
// 2. .git -> .bare, make it bare.
|
||||
|
||||
@@ -5,7 +5,6 @@ use crate::git::{Git, GitError};
|
||||
use crate::layout::dir_name;
|
||||
use crate::receive::{ensure_fetch_refspec, materialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize, Serialize)]
|
||||
@@ -53,7 +52,8 @@ pub fn clone_bare_layout(
|
||||
dir: &Path,
|
||||
envs: &[(String, String)],
|
||||
) -> Result<Vec<PathBuf>, GitError> {
|
||||
fs::create_dir_all(dir)?;
|
||||
// Released last: until then the daemon ignores the half-built repo.
|
||||
let _busy = crate::busy::BusyGuard::acquire(dir)?;
|
||||
let bare = dir.join(".bare");
|
||||
let mut g = Git::new(dir);
|
||||
for (k, v) in envs {
|
||||
@@ -85,7 +85,7 @@ pub fn clone_bare_layout(
|
||||
/// Creates a brand new local repo in the bare layout with an initial empty commit on `main`,
|
||||
/// pointing at `url`. Nothing is pushed here.
|
||||
pub fn init_bare_layout(dir: &Path, url: &str) -> Result<PathBuf, GitError> {
|
||||
fs::create_dir_all(dir)?;
|
||||
let _busy = crate::busy::BusyGuard::acquire(dir)?;
|
||||
let bare = dir.join(".bare");
|
||||
let g = Git::new(dir);
|
||||
g.run(&[
|
||||
|
||||
Reference in new issue
Block a user