Skip to content

Commit

Permalink
Change to use fs:write and added debug messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu-maeda committed Feb 1, 2024
1 parent a360773 commit 7bbb860
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
27 changes: 22 additions & 5 deletions src/common/persist.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ::log::{error, trace};
use log::debug;
use std::{
fs::File,
io::Write,
fs,
path::Path,
sync::{
mpsc::{self, TrySendError},
Expand Down Expand Up @@ -77,6 +77,10 @@ impl Persister {
}
}
}
debug!(
"Persistence thread for {} exiting",
data_path.display().to_string()
);
});
(persist_tx, task_handle)
}
Expand All @@ -86,9 +90,22 @@ impl Persister {
data_path: impl AsRef<Path>,
) -> Result<(), N3xbError> {
let json = serde_json::to_string(&*store)?;
let mut file = File::create(data_path.as_ref())?;
file.write_all(json.as_bytes())?;
file.sync_all()?;
let contains_type = json.contains("type");
let contains_type_string = if contains_type {
"containing type"
} else {
"not containing type"
};

debug!(
"Persisting JSON {} to path: {} - {}",
contains_type_string,
data_path.as_ref().display().to_string(),
json
);

assert!(contains_type);
fs::write(data_path.as_ref(), json)?;
Ok(())
}

Expand Down
8 changes: 7 additions & 1 deletion src/comms/data.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::debug;
use std::{
collections::HashMap,
net::SocketAddr,
Expand Down Expand Up @@ -68,7 +69,12 @@ impl CommsData {
}

fn restore(data_path: impl AsRef<Path>) -> Result<CommsDataStore, N3xbError> {
let json = Persister::restore(data_path)?;
let json = Persister::restore(&data_path)?;
debug!(
"Restored JSON from path: {} - {}",
data_path.as_ref().display().to_string(),
&json
);
let store: CommsDataStore = serde_json::from_str(&json)?;
Ok(store)
}
Expand Down

0 comments on commit 7bbb860

Please sign in to comment.