🐛 Stabilize drosync automatic scheduling

This commit is contained in:
eust-w 2026-09-01 23:52:23 +08:00
parent f7ca2755fd
commit 921fc37211
2 changed files with 54 additions and 6 deletions

View File

@ -115,6 +115,10 @@ sudo env \
--remote-prefix ubt/incoming/
```
系统服务启用了 `PrivateTmp=true`。不要把同步源目录配置在 `/tmp` 或
`/var/tmp`;systemd 私有临时目录会让 daemon 看不到用户 shell 创建的文件。
应使用 `/data`、`/srv`、挂载的数据盘或实际采集目录。
`--bucket` 只会在当前 Key 可见的 Space 中解析。没有匹配或存在多个匹配时命令会拒绝猜测。管理员可改用 `--space <SPACE_ID>` 精确指定。
可选参数:

View File

@ -1143,7 +1143,9 @@ async fn handle_update(state: &mut StateStore, arguments: &Value) -> Result<Valu
async fn run_scheduler(state_db: PathBuf, transfer_gate: std::sync::Arc<Semaphore>) -> Result<()> {
let (event_sender, mut event_receiver) = tokio::sync::mpsc::unbounded_channel();
let mut watcher = notify::recommended_watcher(move |event: notify::Result<notify::Event>| {
if let Ok(event) = event {
if let Ok(event) = event
&& event_triggers_sync(event.kind)
{
let _ = event_sender.send(event.paths);
}
})?;
@ -1163,8 +1165,19 @@ async fn run_scheduler(state_db: PathBuf, transfer_gate: std::sync::Arc<Semaphor
let current = state.mappings()?.into_iter().map(|mapping| (mapping.id.clone(), mapping)).collect::<BTreeMap<_, _>>();
for (id, mapping) in &current {
if !mappings.contains_key(id) {
watcher.watch(&mapping.local_root, RecursiveMode::Recursive)?;
dirty.insert(id.clone(), std::time::Instant::now());
match watcher.watch(&mapping.local_root, RecursiveMode::Recursive) {
Ok(()) => {
dirty.insert(id.clone(), std::time::Instant::now());
}
Err(error) => {
tracing::error!(
mapping_id = %id,
local_root = %mapping.local_root.display(),
error = %error,
"failed to watch mapping; scheduler will retry on refresh"
);
}
}
}
}
for (id, mapping) in &mappings {
@ -1221,6 +1234,24 @@ async fn run_scheduler(state_db: PathBuf, transfer_gate: std::sync::Arc<Semaphor
}
}
fn event_triggers_sync(kind: notify::EventKind) -> bool {
matches!(
kind,
notify::EventKind::Create(_) | notify::EventKind::Modify(_) | notify::EventKind::Remove(_)
)
}
fn spawn_scheduler(state_db: PathBuf, transfer_gate: std::sync::Arc<Semaphore>) {
tokio::spawn(async move {
loop {
if let Err(error) = run_scheduler(state_db.clone(), transfer_gate.clone()).await {
tracing::error!(error = %error, "drosync scheduler stopped; restarting");
tokio::time::sleep(Duration::from_secs(5)).await;
}
}
});
}
const fn drosync_protocol_version() -> &'static str {
"v1"
}
@ -1242,7 +1273,7 @@ pub async fn run(paths: &Paths) -> Result<()> {
let state = StateStore::open(&paths.state_db)?;
state.set_meta("data_dir", &paths.data_dir.to_string_lossy())?;
let transfer_gate = std::sync::Arc::new(Semaphore::new(1));
tokio::spawn(run_scheduler(paths.state_db.clone(), transfer_gate.clone()));
spawn_scheduler(paths.state_db.clone(), transfer_gate.clone());
loop {
let (stream, _) = listener.accept().await?;
let state_db = paths.state_db.clone();
@ -1300,7 +1331,7 @@ async fn run_windows_pipe(paths: &Paths, pipe_name: &str) -> Result<()> {
let state = StateStore::open(&paths.state_db)?;
state.set_meta("data_dir", &paths.data_dir.to_string_lossy())?;
let transfer_gate = std::sync::Arc::new(Semaphore::new(1));
tokio::spawn(run_scheduler(paths.state_db.clone(), transfer_gate.clone()));
spawn_scheduler(paths.state_db.clone(), transfer_gate.clone());
let mut first_instance = true;
loop {
let mut options = ServerOptions::new();
@ -1411,7 +1442,11 @@ pub fn default_log_path(data_dir: &Path) -> PathBuf {
#[cfg(test)]
mod tests {
use super::{MULTIPART_MAX_PART, multipart_part_size, resolve_bucket_space};
use super::{
MULTIPART_MAX_PART, event_triggers_sync, multipart_part_size, resolve_bucket_space,
};
use notify::EventKind;
use notify::event::{AccessKind, CreateKind, ModifyKind, RemoveKind};
use serde_json::json;
#[test]
@ -1457,4 +1492,13 @@ mod tests {
assert!(resolve_bucket_space(&capabilities, "bucket-missing").is_err());
assert!(resolve_bucket_space(&capabilities, " ").is_err());
}
#[test]
fn watcher_ignores_reads_but_tracks_content_changes() {
assert!(!event_triggers_sync(EventKind::Access(AccessKind::Any)));
assert!(!event_triggers_sync(EventKind::Other));
assert!(event_triggers_sync(EventKind::Create(CreateKind::Any)));
assert!(event_triggers_sync(EventKind::Modify(ModifyKind::Any)));
assert!(event_triggers_sync(EventKind::Remove(RemoveKind::Any)));
}
}