pub struct SessionPersistence { /* private fields */ }Expand description
High-level session persistence manager
Coordinates saving and loading of download session state using the ResumeData JSON format. Manages both individual command states (.aria2 files) and global session options.
§Examples
use aria2_core::session::session_persistence::SessionPersistence;
use std::path::Path;
let session = SessionPersistence::new(Path::new("/tmp/aria2_session"));
// Save current state
let count = session.save_state(&groups).await?;
println!("Saved {} downloads", count);
// Load saved state
let count = session.load_state(&mut groups).await?;
println!("Restored {} downloads", count);Implementations§
Source§impl SessionPersistence
impl SessionPersistence
Sourcepub fn new(session_dir: &Path) -> Self
pub fn new(session_dir: &Path) -> Self
Create a new SessionPersistence instance
§Arguments
session_dir- Directory path for storing .aria2 session files
Sourcepub fn with_interval(self, interval_secs: u64) -> Self
pub fn with_interval(self, interval_secs: u64) -> Self
Create with custom auto-save interval
Sourcepub fn without_auto_save(self) -> Self
pub fn without_auto_save(self) -> Self
Disable auto-save (only manual save/load)
Set cookie jar for persistence alongside session data
Get mutable reference to the cookie jar for adding cookies before saving
Get reference to the cookie jar
Sourcepub fn session_dir(&self) -> &Path
pub fn session_dir(&self) -> &Path
Get the session directory path
Sourcepub async fn save_state(
&self,
groups: &[Arc<RwLock<RequestGroup>>],
) -> Result<usize, String>
pub async fn save_state( &self, groups: &[Arc<RwLock<RequestGroup>>], ) -> Result<usize, String>
Save all active/paused/stopped command states to the session directory
Iterates through all RequestGroups, converts each to ResumeData, and writes individual .aria2 files. Also saves global options.
§Arguments
groups- Slice of active download groups to persist
§Returns
Ok(usize)- Number of successfully saved commandsErr(String)- Error message if critical failure occurs
§File Format
Each command is saved as {gid}.aria2 in JSON format.
Global options are saved as session_options.json.
Sourcepub async fn load_state(
&mut self,
groups: &mut Vec<Arc<RwLock<RequestGroup>>>,
) -> Result<usize, String>
pub async fn load_state( &mut self, groups: &mut Vec<Arc<RwLock<RequestGroup>>>, ) -> Result<usize, String>
Load saved states from session directory and restore paused commands
Reads all .aria2 files from the session directory, deserializes them, and creates paused download commands for each valid entry.
§Arguments
groups- Mutable reference to the groups vector to restore into
§Returns
Ok(usize)- Number of successfully restored commandsErr(String)- Error message if critical failure occurs
§Graceful Error Handling
- Missing session directory returns Ok(0) (not an error)
- Corrupt/malformed .aria2 files are skipped with a warning
- Partial restoration is allowed (some files may fail)
Sourcepub fn start_auto_save(
&self,
groups: Arc<RwLock<Vec<Arc<RwLock<RequestGroup>>>>>,
) -> Option<JoinHandle<()>>
pub fn start_auto_save( &self, groups: Arc<RwLock<Vec<Arc<RwLock<RequestGroup>>>>>, ) -> Option<JoinHandle<()>>
Sourcepub async fn cleanup(&self) -> Result<(), String>
pub async fn cleanup(&self) -> Result<(), String>
Clean up all session files (for testing or reset)
Sourcepub async fn save_server_stats(
&self,
stat_man: &ServerStatMan,
) -> Result<usize, String>
pub async fn save_server_stats( &self, stat_man: &ServerStatMan, ) -> Result<usize, String>
Save server statistics to the session directory.
Persists all server performance statistics (download speeds, error counts, etc.) to a JSON file in the session directory. This allows the adaptive URI selector to remember server performance across restarts.
§Arguments
stat_man- Reference to the ServerStatMan to save
§Returns
Ok(usize)- Number of server stats savedErr(String)- Error message if save fails
§File Location
Stats are saved to {session_dir}/server-stat.json
§Example
use aria2_core::session::session_persistence::SessionPersistence;
use aria2_core::selector::server_stat_man::ServerStatMan;
let persistence = SessionPersistence::new(Path::new("/tmp/aria2_session"));
let stat_man = ServerStatMan::new();
stat_man.update("fast.mirror.com", 10000, false);
let saved = persistence.save_server_stats(&stat_man).await?;
println!("Saved {} server stats", saved);Sourcepub async fn load_server_stats(
&self,
stat_man: &ServerStatMan,
) -> Result<usize, String>
pub async fn load_server_stats( &self, stat_man: &ServerStatMan, ) -> Result<usize, String>
Load server statistics from the session directory.
Restores previously saved server performance statistics from a JSON file in the session directory. This allows the adaptive URI selector to make informed decisions immediately after startup.
§Arguments
stat_man- Reference to the ServerStatMan to load into
§Returns
Ok(usize)- Number of server stats loadedErr(String)- Error message if load fails
§Behavior
- Returns
Ok(0)if no server-stat.json file exists (not an error) - Returns error if file exists but is invalid
- Merges with existing stats (doesn’t clear current stats)
§Example
use aria2_core::session::session_persistence::SessionPersistence;
use aria2_core::selector::server_stat_man::ServerStatMan;
let persistence = SessionPersistence::new(Path::new("/tmp/aria2_session"));
let stat_man = ServerStatMan::new();
let loaded = persistence.load_server_stats(&stat_man).await?;
println!("Loaded {} server stats from previous session", loaded);Sourcepub async fn save_active_only(
&self,
groups: &[Arc<RwLock<RequestGroup>>],
) -> Result<usize, String>
pub async fn save_active_only( &self, groups: &[Arc<RwLock<RequestGroup>>], ) -> Result<usize, String>
Save only active/in-progress downloads (skip completed/stopped/error).
Filters groups by download status, persisting only those that are actively downloading or waiting in queue.
§Arguments
groups- Slice of all download groups to filter
§Returns
Ok(usize)- Number of active downloads successfully savedErr(String)- Error message if critical failure occurs
Sourcepub async fn save_completed(
&self,
groups: &[Arc<RwLock<RequestGroup>>],
) -> Result<usize, String>
pub async fn save_completed( &self, groups: &[Arc<RwLock<RequestGroup>>], ) -> Result<usize, String>
Save only completed downloads for archival.
Filters groups by completion status, persisting only finished downloads. Useful for creating archives of successful downloads separate from active/pending work.
§Arguments
groups- Slice of all download groups to filter
§Returns
Ok(usize)- Number of completed downloads successfully savedErr(String)- Error message if critical failure occurs