pub struct TaskStore { /* private fields */ }Expand description
A store for managing tasks with JSONL persistence and file locking.
Implementations§
Source§impl TaskStore
impl TaskStore
Sourcepub fn load(path: &Path) -> Result<Self>
pub fn load(path: &Path) -> Result<Self>
Loads tasks from the JSONL file at the given path.
If the file doesn’t exist, returns an empty store. Logs warnings for malformed JSON lines and skips them.
Uses a shared lock to allow concurrent reads from multiple loops.
Sourcepub fn save(&self) -> Result<()>
pub fn save(&self) -> Result<()>
Saves all tasks to the JSONL file.
Creates parent directories if they don’t exist. Uses an exclusive lock to prevent concurrent writes.
Sourcepub fn reload(&mut self) -> Result<()>
pub fn reload(&mut self) -> Result<()>
Reloads tasks from disk, useful after external modifications.
Logs warnings for malformed JSON lines and skips them. Uses a shared lock to allow concurrent reads.
Sourcepub fn with_exclusive_lock<F, T>(&mut self, f: F) -> Result<T>where
F: FnOnce(&mut Self) -> T,
pub fn with_exclusive_lock<F, T>(&mut self, f: F) -> Result<T>where
F: FnOnce(&mut Self) -> T,
Executes a read-modify-write operation atomically.
Acquires an exclusive lock, reloads from disk, executes the provided function, and saves back to disk. This ensures that concurrent modifications from other loops are not lost.
§Example
store.with_exclusive_lock(|store| {
let task = Task::new("New task".to_string(), 1);
store.add(task);
})?;Sourcepub fn add(&mut self, task: Task) -> &Task
pub fn add(&mut self, task: Task) -> &Task
Adds a new task to the store and returns a reference to it.
Sourcepub fn get_by_key(&self, key: &str) -> Option<&Task>
pub fn get_by_key(&self, key: &str) -> Option<&Task>
Gets a task by stable key (immutable reference).
Sourcepub fn get_mut(&mut self, id: &str) -> Option<&mut Task>
pub fn get_mut(&mut self, id: &str) -> Option<&mut Task>
Gets a task by ID (mutable reference).
Sourcepub fn get_by_key_mut(&mut self, key: &str) -> Option<&mut Task>
pub fn get_by_key_mut(&mut self, key: &str) -> Option<&mut Task>
Gets a task by stable key (mutable reference).
Sourcepub fn close(&mut self, id: &str) -> Option<&Task>
pub fn close(&mut self, id: &str) -> Option<&Task>
Closes a task by ID and returns a reference to it.
Sourcepub fn start(&mut self, id: &str) -> Option<&Task>
pub fn start(&mut self, id: &str) -> Option<&Task>
Starts a task by ID and returns a reference to it.
Sourcepub fn fail(&mut self, id: &str) -> Option<&Task>
pub fn fail(&mut self, id: &str) -> Option<&Task>
Fails a task by ID and returns a reference to it.
Sourcepub fn reopen(&mut self, id: &str) -> Option<&Task>
pub fn reopen(&mut self, id: &str) -> Option<&Task>
Reopens a task by ID and returns a reference to it.
Sourcepub fn ensure(&mut self, task: Task) -> &Task
pub fn ensure(&mut self, task: Task) -> &Task
Ensures a task exists for a stable key, returning the existing or created task.
If a task with the same key already exists, its non-lifecycle metadata is refreshed and the existing task is returned.
Sourcepub fn has_open_tasks(&self) -> bool
pub fn has_open_tasks(&self) -> bool
Returns true if there are any open tasks.
A task is considered open if it is not Closed. This includes Failed tasks.
Sourcepub fn has_pending_tasks(&self) -> bool
pub fn has_pending_tasks(&self) -> bool
Returns true if there are any pending (non-terminal) tasks.
A task is pending if its status is not terminal (i.e., not Closed or Failed). Use this when you need to check if there’s active work remaining.