Skip to content

Commit

Permalink
rename deadman's switch to something more meaningful
Browse files Browse the repository at this point in the history
  • Loading branch information
bertiqwerty committed Oct 13, 2024
1 parent df18212 commit e11e79c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
8 changes: 0 additions & 8 deletions rvimage/src/rvlib/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
sort_params::SortParams,
ssh,
};
use chrono::{DateTime, Utc};
use rvimage_domain::{rverr, to_rv, RvError, RvResult};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -77,7 +76,6 @@ impl CfgLegacy {
prefix: ab.prefix,
}),
sort_params: SortParams::default(),
deadmansswitch: Utc::now(),
};
Cfg { usr, prj }
}
Expand Down Expand Up @@ -341,8 +339,6 @@ pub struct CfgPrj {
pub azure_blob: Option<AzureBlobCfgPrj>,
#[serde(default)]
pub sort_params: SortParams,
#[serde(default = "Utc::now")]
pub deadmansswitch: DateTime<Utc>,
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Default)]
pub struct Cfg {
Expand Down Expand Up @@ -374,10 +370,6 @@ impl Cfg {
}
}

pub fn push_deadmansswitch(&mut self) {
self.prj.deadmansswitch = Utc::now();
}

pub fn tmpdir(&self) -> &str {
match &self.usr.tmpdir {
Some(td) => td.as_str(),
Expand Down
20 changes: 10 additions & 10 deletions rvimage/src/rvlib/control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod detail {

use crate::{
cfg::{read_cfg, write_cfg, Cfg, CfgPrj},
control::{Deadmansswitch, SavePrjData},
control::{LastTimePrjOpened, SavePrjData},
file_util::{self, tf_to_annomap_key, SavedCfg},
result::trace_ok_err,
util::version_label,
Expand Down Expand Up @@ -119,7 +119,7 @@ mod detail {
opened_folder: opened_folder.map(|of| of.to_string()),
tools_data_map: tdm.clone(),
cfg: SavedCfg::CfgPrj(cfg.prj.clone()),
deadmansswitch: Deadmansswitch::new(),
last_time_prj_opened: LastTimePrjOpened::new(),
};
tracing::info!("saved to {file_path:?}");
write(tools_data_map, make_data, file_path)?;
Expand Down Expand Up @@ -172,21 +172,21 @@ pub fn load_prj(file_path: &Path) -> RvResult<SavePrjData> {
serde_json::from_str::<SavePrjData>(s.as_str()).map_err(to_rv)
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct Deadmansswitch {
pub struct LastTimePrjOpened {
time: DateTime<Utc>,
username: String,
realname: String,
}
impl Deadmansswitch {
impl LastTimePrjOpened {
pub fn new() -> Self {
Deadmansswitch {
LastTimePrjOpened {
time: Utc::now(),
username: whoami::username(),
realname: whoami::realname(),
}
}
}
impl Default for Deadmansswitch {
impl Default for LastTimePrjOpened {
fn default() -> Self {
Self::new()
}
Expand All @@ -199,8 +199,8 @@ pub struct SavePrjData {
pub opened_folder: Option<String>,
pub tools_data_map: ToolsDataMap,
pub cfg: SavedCfg,
#[serde(default = "Deadmansswitch::new")]
pub deadmansswitch: Deadmansswitch,
#[serde(default = "LastTimePrjOpened::new")]
pub last_time_prj_opened: LastTimePrjOpened,
}

#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -291,14 +291,14 @@ impl Control {
}
}

pub fn push_deadmansswitch(&mut self) {
pub fn write_lasttimeprjopened(&mut self) {
self.wait_for_save();
let prj_path = self.cfg.current_prj_path().to_path_buf();
let handle = thread::spawn(move || {
tracing::info!("pushing dead man's switch...");
let prj_data = trace_ok_err(load_prj(&prj_path));
if let Some(mut prj_data) = prj_data {
prj_data.deadmansswitch = Deadmansswitch::new();
prj_data.last_time_prj_opened = LastTimePrjOpened::new();
trace_ok_err(file_util::save(&prj_path, prj_data));
}
tracing::info!("pushing dead man's done!");
Expand Down
12 changes: 6 additions & 6 deletions rvimage/src/rvlib/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub struct MainEventLoop {
rx_from_http: Option<Receiver<RvResult<String>>>,
http_addr: String,
autosave_timer: Instant,
deadmansswitch_timer: Instant,
last_time_prj_opened: Instant,
}
impl Default for MainEventLoop {
fn default() -> Self {
Expand All @@ -151,7 +151,7 @@ impl MainEventLoop {
} else {
None
};
ctrl.push_deadmansswitch();
ctrl.write_lasttimeprjopened();
let mut self_ = Self {
world,
ctrl,
Expand All @@ -163,7 +163,7 @@ impl MainEventLoop {
recently_clicked_tool_idx: None,
rx_from_http,
autosave_timer: Instant::now(),
deadmansswitch_timer: Instant::now(),
last_time_prj_opened: Instant::now(),
};

trace_ok_err(self_.load_prj(prj_file_path));
Expand Down Expand Up @@ -388,9 +388,9 @@ impl MainEventLoop {
};
self.world.update_view.image_info = Some(s);
}
if self.deadmansswitch_timer.elapsed().as_secs() > DEAD_MANS_SWITCH_S {
self.ctrl.push_deadmansswitch();
self.deadmansswitch_timer = Instant::now();
if self.last_time_prj_opened.elapsed().as_secs() > DEAD_MANS_SWITCH_S {
self.ctrl.write_lasttimeprjopened();
self.last_time_prj_opened = Instant::now();
}
if let Some(n_autosaves) = self.ctrl.cfg.usr.n_autosaves {
if self.autosave_timer.elapsed().as_secs() > AUTOSAVE_INTERVAL_S {
Expand Down

0 comments on commit e11e79c

Please sign in to comment.