Skip to main content

shaul_core/
store.rs

1use std::fs::{File, OpenOptions};
2use std::path::{Path, PathBuf};
3
4use async_trait::async_trait;
5use fs2::FileExt;
6use serde_json::Error as JsonError;
7
8use crate::error::{Error, Result};
9use crate::task::{Task, TaskId};
10use crate::util::now_ms;
11
12/// In-place mutation applied by [`TaskStore::update`] while the task is
13/// locked. Returning an error aborts the update without saving.
14pub type TaskUpdate = Box<dyn for<'a> FnOnce(&'a mut Task) -> Result<()> + Send + 'static>;
15
16/// Persistence interface for tasks.
17///
18/// Implementations must make [`TaskStore::update`] atomic with respect to
19/// concurrent writers, including other processes sharing the same state
20/// directory.
21#[async_trait]
22pub trait TaskStore: Send + Sync {
23    /// Inserts or replaces the stored task.
24    async fn save(&self, task: &Task) -> Result<()>;
25    /// Loads a task by id, or fails with [`Error::TaskNotFound`] when absent.
26    async fn load(&self, id: TaskId) -> Result<Task>;
27    /// Returns all stored tasks ordered by creation time.
28    async fn list(&self) -> Result<Vec<Task>>;
29    /// Deletes a task. Deleting a task that does not exist is not an error.
30    async fn delete(&self, id: TaskId) -> Result<()>;
31    /// Applies `update` to the stored task under a lock and returns the saved
32    /// result.
33    async fn update(&self, id: TaskId, update: TaskUpdate) -> Result<Task>;
34}
35
36/// One JSON sidecar per task under a state directory. Writes are atomic
37/// (temp file + rename) and serialized by a cross-process lock file.
38#[derive(Clone, Debug)]
39pub struct SidecarStore {
40    root: PathBuf,
41}
42
43impl SidecarStore {
44    /// Creates a store rooted at `root`, creating the directory if it does not
45    /// exist yet.
46    pub fn new(root: impl Into<PathBuf>) -> Result<Self> {
47        let root = root.into();
48        std::fs::create_dir_all(&root)?;
49        Ok(Self { root })
50    }
51
52    /// Returns the directory holding task sidecars and the store lock file.
53    pub fn root(&self) -> &Path {
54        &self.root
55    }
56
57    fn task_path(&self, id: TaskId) -> PathBuf {
58        self.root.join(format!("{id}.json"))
59    }
60
61    fn lock_path(&self) -> PathBuf {
62        self.root.join(".store.lock")
63    }
64
65    fn with_lock<T>(&self, f: impl FnOnce() -> Result<T>) -> Result<T> {
66        let lock = OpenOptions::new()
67            .create(true)
68            .read(true)
69            .write(true)
70            .truncate(false)
71            .open(self.lock_path())?;
72        lock.lock_exclusive()?;
73        let result = f();
74        let _ = FileExt::unlock(&lock);
75        result
76    }
77
78    fn save_sync(&self, task: &Task) -> Result<()> {
79        let path = self.task_path(task.id);
80        let temp = self.root.join(format!("{}.json.tmp", task.id));
81        let data = serde_json::to_vec_pretty(task)?;
82        {
83            let mut file = File::create(&temp)?;
84            use std::io::Write;
85            file.write_all(&data)?;
86            file.sync_all()?;
87            #[cfg(unix)]
88            {
89                use std::os::unix::fs::PermissionsExt;
90                let _ = std::fs::set_permissions(&temp, std::fs::Permissions::from_mode(0o600));
91            }
92        }
93        if path.exists() {
94            std::fs::remove_file(&path)?;
95        }
96        std::fs::rename(&temp, &path)?;
97        Ok(())
98    }
99
100    fn load_sync(&self, id: TaskId) -> Result<Task> {
101        let path = self.task_path(id);
102        let data = std::fs::read(&path).map_err(|error| {
103            if error.kind() == std::io::ErrorKind::NotFound {
104                Error::TaskNotFound(id.to_string())
105            } else {
106                Error::Io(error)
107            }
108        })?;
109        Ok(serde_json::from_slice(&data)?)
110    }
111
112    fn list_sync(&self) -> Result<Vec<Task>> {
113        let mut tasks = Vec::new();
114        for entry in std::fs::read_dir(&self.root)? {
115            let entry = entry?;
116            let path = entry.path();
117            if path.extension().and_then(|value| value.to_str()) != Some("json") {
118                continue;
119            }
120            match std::fs::read(&path)
121                .map_err(Error::Io)
122                .and_then(|data| serde_json::from_slice::<Task>(&data).map_err(Error::Json))
123            {
124                Ok(task) => tasks.push(task),
125                Err(error) => {
126                    tracing::warn!(path = %path.display(), %error, "skipping unreadable task sidecar");
127                }
128            }
129        }
130        tasks.sort_by_key(|task| task.created_at_ms);
131        Ok(tasks)
132    }
133
134    fn update_sync(&self, id: TaskId, update: TaskUpdate) -> Result<Task> {
135        self.with_lock(|| {
136            let mut task = self.load_sync(id)?;
137            update(&mut task)?;
138            task.updated_at_ms = now_ms();
139            self.save_sync(&task)?;
140            Ok(task)
141        })
142    }
143
144    fn delete_sync(&self, id: TaskId) -> Result<()> {
145        self.with_lock(|| {
146            let path = self.task_path(id);
147            match std::fs::remove_file(&path) {
148                Ok(()) => Ok(()),
149                Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
150                Err(error) => Err(Error::Io(error)),
151            }
152        })
153    }
154}
155
156#[async_trait]
157impl TaskStore for SidecarStore {
158    async fn save(&self, task: &Task) -> Result<()> {
159        let store = self.clone();
160        let task = task.clone();
161        tokio::task::spawn_blocking(move || store.with_lock(|| store.save_sync(&task)))
162            .await
163            .map_err(|error| Error::Other(format!("store task failed: {error}")))?
164    }
165
166    async fn load(&self, id: TaskId) -> Result<Task> {
167        let store = self.clone();
168        tokio::task::spawn_blocking(move || store.load_sync(id))
169            .await
170            .map_err(|error| Error::Other(format!("store task failed: {error}")))?
171    }
172
173    async fn list(&self) -> Result<Vec<Task>> {
174        let store = self.clone();
175        tokio::task::spawn_blocking(move || store.list_sync())
176            .await
177            .map_err(|error| Error::Other(format!("store task failed: {error}")))?
178    }
179
180    async fn delete(&self, id: TaskId) -> Result<()> {
181        let store = self.clone();
182        tokio::task::spawn_blocking(move || store.delete_sync(id))
183            .await
184            .map_err(|error| Error::Other(format!("store task failed: {error}")))?
185    }
186
187    async fn update(&self, id: TaskId, update: TaskUpdate) -> Result<Task> {
188        let store = self.clone();
189        tokio::task::spawn_blocking(move || store.update_sync(id, update))
190            .await
191            .map_err(|error| Error::Other(format!("store task failed: {error}")))?
192    }
193}
194
195#[allow(dead_code)]
196fn _assert_json_error(_: JsonError) {}
197
198#[cfg(test)]
199mod tests {
200    use super::*;
201    use crate::link::Link;
202    use crate::task::{Task, TaskOptions, TaskState};
203
204    #[tokio::test]
205    async fn saves_loads_and_updates() {
206        let dir = tempfile::tempdir().unwrap();
207        let store = SidecarStore::new(dir.path()).unwrap();
208        let task = Task::new(
209            Link::new("https://example.com/file").unwrap(),
210            dir.path().to_path_buf(),
211            TaskOptions::default(),
212        );
213        store.save(&task).await.unwrap();
214        let loaded = store.load(task.id).await.unwrap();
215        assert_eq!(loaded.link.as_str(), "https://example.com/file");
216
217        let updated = store
218            .update(
219                task.id,
220                Box::new(|task| {
221                    task.state = TaskState::Paused;
222                    Ok(())
223                }),
224            )
225            .await
226            .unwrap();
227        assert_eq!(updated.state, TaskState::Paused);
228        assert_eq!(store.list().await.unwrap().len(), 1);
229    }
230
231    #[tokio::test]
232    async fn listing_skips_corrupt_sidecars() {
233        let dir = tempfile::tempdir().unwrap();
234        let store = SidecarStore::new(dir.path()).unwrap();
235        let task = Task::new(
236            Link::new("https://example.com/file").unwrap(),
237            dir.path().to_path_buf(),
238            TaskOptions::default(),
239        );
240        store.save(&task).await.unwrap();
241        std::fs::write(dir.path().join("corrupt.json"), b"{not valid json").unwrap();
242
243        let tasks = store.list().await.unwrap();
244        assert_eq!(tasks.len(), 1);
245        assert_eq!(tasks[0].id, task.id);
246    }
247}