Skip to content

Commit

Permalink
Ensure statefile is always written in start, also without websocket r…
Browse files Browse the repository at this point in the history
…unning
  • Loading branch information
HEnquist committed Nov 6, 2023
1 parent 9dd12df commit 11c1608
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
17 changes: 14 additions & 3 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,20 @@ fn main_process() -> i32 {
}
}

// All state variables are prepared, save to the statefile if needed
if let Some(fname) = &statefilename {
let state_to_save = statefile::State {
config_path: configname.clone(),
volume: initial_volumes,
mute: initial_mutes,
};
if state.is_none() || state.map(|s| s != state_to_save).unwrap_or(false) {
statefile::save_state_to_file(fname, &state_to_save);
} else {
debug!("No change to state from {}, not overwriting.", fname);
}
}

let (tx_command, rx_command) = crossbeam_channel::bounded(10);
if let Some(path) = &configname {
match config::load_validate_config(path) {
Expand Down Expand Up @@ -931,9 +945,6 @@ fn main_process() -> i32 {
let serveraddress = matches.value_of("address").unwrap_or("127.0.0.1");
let serverport = port_str.parse::<usize>().unwrap();

// Send one state change to trigger an initial save
tx_state.try_send(()).unwrap_or(());

let shared_data = socketserver::SharedData {
active_config: active_config.clone(),
active_config_path,
Expand Down
25 changes: 15 additions & 10 deletions src/statefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ use std::sync::Arc;

use crate::ProcessingParameters;

//use std::path::{Path, PathBuf};

//use crate::PrcFmt;
//type Res<T> = Result<T, Box<dyn error::Error>>;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct State {
pub config_path: Option<String>,
Expand Down Expand Up @@ -60,6 +55,13 @@ pub fn save_state(
volume: params.volumes(),
mute: params.mutes(),
};
if save_state_to_file(filename, &state) {
unsaved_changes.store(false, Ordering::Relaxed);
}
}

pub fn save_state_to_file(filename: &str, state: &State) -> bool {
debug!("Saving state to {}", filename);
match std::fs::OpenOptions::new()
.write(true)
.create(true)
Expand All @@ -72,17 +74,20 @@ pub fn save_state(
"Unable to write to statefile '{}', error: {}",
filename, writeerr
);
return;
return false;
}
if let Err(syncerr) = &f.sync_all() {
error!(
"Unable to commit statefile '{}' data to disk, error: {}",
filename, syncerr
);
return;
return false;
}
unsaved_changes.store(false, Ordering::Relaxed);
true
}
Err(openerr) => {
error!("Unable to open statefile {}, error: {}", filename, openerr);
false
}
Err(openerr) => error!("Unable to open statefile {}, error: {}", filename, openerr),
}
}

0 comments on commit 11c1608

Please sign in to comment.