2022-08-10 01:56:22 +01:00
|
|
|
use std::sync::mpsc::{self, Receiver, Sender};
|
|
|
|
use std::thread::{self, JoinHandle};
|
|
|
|
|
|
|
|
use crate::persist::SettingsJson;
|
|
|
|
use crate::settings::Settings;
|
|
|
|
use crate::utility::{unwrap_lock, unwrap_maybe_fatal};
|
|
|
|
|
|
|
|
pub fn spawn(settings: Settings) -> (JoinHandle<()>, Sender<()>) {
|
|
|
|
let (sender, receiver): (Sender<()>, Receiver<()>) = mpsc::channel();
|
|
|
|
let worker = thread::spawn(move || {
|
2022-09-01 01:18:15 +01:00
|
|
|
log::info!("save_worker starting...");
|
2022-08-10 01:56:22 +01:00
|
|
|
for _ in receiver.iter() {
|
2022-09-01 01:18:15 +01:00
|
|
|
log::debug!("save_worker is saving...");
|
2022-10-11 22:38:20 +01:00
|
|
|
let is_persistent = unwrap_lock(settings.general.lock(), "general").persistent.clone();
|
|
|
|
if is_persistent {
|
|
|
|
let save_path = crate::utility::settings_dir()
|
|
|
|
.join(unwrap_lock(settings.general.lock(), "general").path.clone());
|
|
|
|
let settings_clone = settings.clone();
|
|
|
|
let save_json: SettingsJson = settings_clone.into();
|
|
|
|
unwrap_maybe_fatal(save_json.save(&save_path), "Failed to save settings");
|
|
|
|
log::debug!("Saved settings to {}", save_path.display());
|
|
|
|
} else {
|
|
|
|
log::debug!("Ignored save request for non-persistent settings");
|
|
|
|
}
|
2022-08-10 01:56:22 +01:00
|
|
|
}
|
2022-09-01 01:18:15 +01:00
|
|
|
log::warn!("save_worker completed!");
|
2022-08-10 01:56:22 +01:00
|
|
|
});
|
|
|
|
(worker, sender)
|
|
|
|
}
|