Expand description
Foothold: A session state management crate for tracking successful, failed, and skipped tasks.
This crate provides utilities for managing session state, tracking progress, and persisting results for batch processing tasks.
§Features
- Track successful, failed, and skipped tasks
- Persist session state to disk
- Compute statistics for current and previous sessions
- Thread-safe file writing
§Example
use foothold::{Foothold, Stats};
/// Helper function to simulates running tasks with possible failures and
/// resumes from previous state.
fn run_tasks(
tasks: &[String],
foothold: &mut Foothold<String, (), ()>,
fake_failure: bool) {
for (index, task) in tasks.iter().enumerate() {
let task = task.to_string();
if foothold.is_successful(&task) {
println!("skipping {task}");
foothold.mark_skipped(task).unwrap();
continue;
} else if fake_failure && foothold.is_failed(&task) {
// If previously failed, skip (or retry)
println!("skipping {task}");
foothold.mark_skipped(task).unwrap();
continue;
}
if fake_failure && index % 2 == 1 {
println!("failing {task}");
foothold.mark_failed(task, ()).unwrap();
} else {
println!("completing {task}");
foothold.mark_successful(task, ()).unwrap();
}
}
}
let mut tasks = vec![
"task1".to_string(),
"task2".to_string(),
"task3".to_string(),
];
/// First run. Assume this session may have failures or gets interrupted.
{
let mut foothold =
Foothold::<String, (), ()>::new(success_log, failed_log, tasks.len()).unwrap();
run_tasks(&tasks, &mut foothold, true);
let stats = foothold.stats();
println!("Stats after first run: {}", stats);
}
// More tasks get added
tasks.push("task4".to_string());
/// Second run. Resuming from previous state.
{
let mut foothold =
Foothold::<String, (), ()>::new(success_log, failed_log, tasks.len()).unwrap();
run_tasks(&tasks, &mut foothold, false);
let stats = foothold.stats();
println!("Stats after resume: {}", stats);
}The above should produce output similar to:
completing task1
failing task2
completing task3
Stats after first run: Total: 3, Successful: 2, Failed: 1, Skipped: 0, Elapsed: 59.81µs, Rate: 33287.84 tasks/sec. In previous session: successful: 0, failed: 0
skipping task1
completing task2
skipping task3
completing task4
Stats after resume: Total: 4, Successful: 2, Failed: 0, Skipped: 2, Elapsed: 23.57µs, Rate: 84670.42 tasks/sec. In previous session: successful: 2, failed: 1See struct-level documentation for details.
Structs§
- Foothold
- Main entry point for managing session state and file persistence for batch processing tasks.
- Stats
- Provides statistics about the progress and results of batch processing sessions.
- Sync
Config - Configuration options for synchronizing batch processing sessions.