style fixups

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-08-18 08:50:14 +02:00
parent c1d7d708d4
commit 4ea831bfa1
1 changed files with 39 additions and 35 deletions

View File

@ -41,12 +41,14 @@ use std::fs::File;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::Duration; use std::time::Duration;
use serde::{Serialize, Deserialize}; use anyhow::{bail, format_err, Error};
use anyhow::{bail, Error, format_err}; use proxmox::tools::fs::{
use proxmox::tools::fs::{file_read_optional_string, replace_file, create_path, CreateOptions, open_file_locked}; create_path, file_read_optional_string, open_file_locked, replace_file, CreateOptions,
};
use serde::{Deserialize, Serialize};
use crate::server::{upid_read_status, worker_is_active_local, TaskState, UPID};
use crate::tools::epoch_now_u64; use crate::tools::epoch_now_u64;
use crate::server::{TaskState, UPID, worker_is_active_local, upid_read_status};
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -57,7 +59,7 @@ pub enum JobState {
/// The Job was last started in 'upid', /// The Job was last started in 'upid',
Started { upid: String }, Started { upid: String },
/// The Job was last started in 'upid', which finished with 'state' /// The Job was last started in 'upid', which finished with 'state'
Finished { upid: String, state: TaskState } Finished { upid: String, state: TaskState },
} }
/// Represents a Job and holds the correct lock /// Represents a Job and holds the correct lock
@ -92,7 +94,7 @@ fn get_path(jobtype: &str, jobname: &str) -> PathBuf {
fn get_lock<P>(path: P) -> Result<File, Error> fn get_lock<P>(path: P) -> Result<File, Error>
where where
P: AsRef<Path> P: AsRef<Path>,
{ {
let mut path = path.as_ref().to_path_buf(); let mut path = path.as_ref().to_path_buf();
path.set_extension("lck"); path.set_extension("lck");
@ -106,14 +108,24 @@ where
pub fn remove_state_file(jobtype: &str, jobname: &str) -> Result<(), Error> { pub fn remove_state_file(jobtype: &str, jobname: &str) -> Result<(), Error> {
let mut path = get_path(jobtype, jobname); let mut path = get_path(jobtype, jobname);
let _lock = get_lock(&path)?; let _lock = get_lock(&path)?;
std::fs::remove_file(&path).map_err(|err| std::fs::remove_file(&path).map_err(|err| {
format_err!("cannot remove statefile for {} - {}: {}", jobtype, jobname, err) format_err!(
)?; "cannot remove statefile for {} - {}: {}",
jobtype,
jobname,
err
)
})?;
path.set_extension("lck"); path.set_extension("lck");
// ignore errors // ignore errors
let _ = std::fs::remove_file(&path).map_err(|err| let _ = std::fs::remove_file(&path).map_err(|err| {
format_err!("cannot remove lockfile for {} - {}: {}", jobtype, jobname, err) format_err!(
); "cannot remove lockfile for {} - {}: {}",
jobtype,
jobname,
err
)
});
Ok(()) Ok(())
} }
@ -130,9 +142,9 @@ pub fn last_run_time(jobtype: &str, jobname: &str) -> Result<i64, Error> {
match JobState::load(jobtype, jobname)? { match JobState::load(jobtype, jobname)? {
JobState::Created { time } => Ok(time), JobState::Created { time } => Ok(time),
JobState::Started { upid } | JobState::Finished { upid, .. } => { JobState::Started { upid } | JobState::Finished { upid, .. } => {
let upid: UPID = upid.parse().map_err(|err| let upid: UPID = upid
format_err!("could not parse upid from state: {}", err) .parse()
)?; .map_err(|err| format_err!("could not parse upid from state: {}", err))?;
Ok(upid.starttime) Ok(upid.starttime)
} }
} }
@ -149,17 +161,15 @@ impl JobState {
if let Some(state) = file_read_optional_string(get_path(jobtype, jobname))? { if let Some(state) = file_read_optional_string(get_path(jobtype, jobname))? {
match serde_json::from_str(&state)? { match serde_json::from_str(&state)? {
JobState::Started { upid } => { JobState::Started { upid } => {
let parsed: UPID = upid.parse() let parsed: UPID = upid
.parse()
.map_err(|err| format_err!("error parsing upid: {}", err))?; .map_err(|err| format_err!("error parsing upid: {}", err))?;
if !worker_is_active_local(&parsed) { if !worker_is_active_local(&parsed) {
let state = upid_read_status(&parsed) let state = upid_read_status(&parsed)
.map_err(|err| format_err!("error reading upid log status: {}", err))?; .map_err(|err| format_err!("error reading upid log status: {}", err))?;
Ok(JobState::Finished { Ok(JobState::Finished { upid, state })
upid,
state
})
} else { } else {
Ok(JobState::Started { upid }) Ok(JobState::Started { upid })
} }
@ -189,7 +199,7 @@ impl Job {
jobtype: jobtype.to_string(), jobtype: jobtype.to_string(),
jobname: jobname.to_string(), jobname: jobname.to_string(),
state: JobState::Created { state: JobState::Created {
time: epoch_now_u64()? as i64 time: epoch_now_u64()? as i64,
}, },
_lock, _lock,
}) })
@ -219,12 +229,10 @@ impl Job {
JobState::Created { .. } => bail!("cannot finish when not started"), JobState::Created { .. } => bail!("cannot finish when not started"),
JobState::Started { upid } => upid, JobState::Started { upid } => upid,
JobState::Finished { upid, .. } => upid, JobState::Finished { upid, .. } => upid,
}.to_string(); }
.to_string();
self.state = JobState::Finished { self.state = JobState::Finished { upid, state };
upid,
state,
};
self.write_state() self.write_state()
} }
@ -250,10 +258,6 @@ impl Job {
.owner(backup_user.uid) .owner(backup_user.uid)
.group(backup_user.gid); .group(backup_user.gid);
replace_file( replace_file(path, serialized.as_bytes(), options)
path,
serialized.as_bytes(),
options,
)
} }
} }