Skip to main content

Mountain/RunTime/Shutdown/
SaveApplicationState.rs

1
2//! Persist the global memento to disk before the runtime tears down. Creates
3//! the parent directory if missing.
4
5use CommonLibrary::Error::CommonError::CommonError;
6
7use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
8
9impl ApplicationRunTime {
10	pub async fn SaveApplicationState(&self) -> Result<(), CommonError> {
11		dev_log!("lifecycle", "[ApplicationRunTime] Saving application state...");
12
13		let GlobalMementoGuard = self
14			.Environment
15			.ApplicationState
16			.Configuration
17			.MementoGlobalStorage
18			.lock()
19			.map_err(|E| CommonError::StateLockPoisoned { Context:E.to_string() })?;
20
21		let GlobalMementoPath = self
22			.Environment
23			.ApplicationState
24			.GlobalMementoPath
25			.lock()
26			.map_err(|E| CommonError::StateLockPoisoned { Context:E.to_string() })?
27			.clone();
28
29		if let Some(Parent) = GlobalMementoPath.parent() {
30			if !Parent.exists() {
31				std::fs::create_dir_all(Parent)
32					.map_err(|E| CommonError::FileSystemIO { Path:Parent.to_path_buf(), Description:E.to_string() })?;
33			}
34		}
35
36		let MementoJSON = serde_json::to_string_pretty(&*GlobalMementoGuard)
37			.map_err(|E| CommonError::SerializationError { Description:E.to_string() })?;
38
39		std::fs::write(&GlobalMementoPath, MementoJSON)
40			.map_err(|E| CommonError::FileSystemIO { Path:GlobalMementoPath.clone(), Description:E.to_string() })
41	}
42}