2020-08-13 08:29:15 +00:00
|
|
|
//! Generic JobState handling
|
|
|
|
//!
|
|
|
|
//! A 'Job' can have 3 states
|
|
|
|
//! - Created, when a schedule was created but never executed
|
|
|
|
//! - Started, when a job is running right now
|
|
|
|
//! - Finished, when a job was running in the past
|
|
|
|
//!
|
|
|
|
//! and is identified by 2 values: jobtype and jobname (e.g. 'syncjob' and 'myfirstsyncjob')
|
|
|
|
//!
|
|
|
|
//! This module Provides 2 helper structs to handle those coniditons
|
|
|
|
//! 'Job' which handles locking and writing to a file
|
|
|
|
//! 'JobState' which is the actual state
|
|
|
|
//!
|
|
|
|
//! an example usage would be
|
|
|
|
//! ```no_run
|
|
|
|
//! # use anyhow::{bail, Error};
|
|
|
|
//! # use proxmox_backup::server::TaskState;
|
2020-10-28 09:52:16 +00:00
|
|
|
//! # use proxmox_backup::server::jobstate::*;
|
2020-08-13 12:30:17 +00:00
|
|
|
//! # fn some_code() -> TaskState { TaskState::OK { endtime: 0 } }
|
2020-08-13 08:29:15 +00:00
|
|
|
//! # fn code() -> Result<(), Error> {
|
|
|
|
//! // locks the correct file under /var/lib
|
|
|
|
//! // or fails if someone else holds the lock
|
|
|
|
//! let mut job = match Job::new("jobtype", "jobname") {
|
|
|
|
//! Ok(job) => job,
|
|
|
|
//! Err(err) => bail!("could not lock jobstate"),
|
|
|
|
//! };
|
|
|
|
//!
|
2020-08-13 12:30:19 +00:00
|
|
|
//! // job holds the lock, we can start it
|
2020-08-13 08:29:15 +00:00
|
|
|
//! job.start("someupid")?;
|
|
|
|
//! // do something
|
|
|
|
//! let task_state = some_code();
|
|
|
|
//! job.finish(task_state)?;
|
|
|
|
//!
|
|
|
|
//! // release the lock
|
|
|
|
//! drop(job);
|
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
use std::fs::File;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2020-08-18 06:50:14 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
|
|
|
use proxmox::tools::fs::{
|
|
|
|
create_path, file_read_optional_string, open_file_locked, replace_file, CreateOptions,
|
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-08-13 08:29:15 +00:00
|
|
|
|
2020-08-18 06:50:14 +00:00
|
|
|
use crate::server::{upid_read_status, worker_is_active_local, TaskState, UPID};
|
2020-08-13 08:29:15 +00:00
|
|
|
|
2020-08-18 06:50:14 +00:00
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2020-08-13 08:29:15 +00:00
|
|
|
/// Represents the State of a specific Job
|
|
|
|
pub enum JobState {
|
|
|
|
/// A job was created at 'time', but never started/finished
|
|
|
|
Created { time: i64 },
|
|
|
|
/// The Job was last started in 'upid',
|
|
|
|
Started { upid: String },
|
2020-08-13 12:30:17 +00:00
|
|
|
/// The Job was last started in 'upid', which finished with 'state'
|
2020-08-18 06:50:14 +00:00
|
|
|
Finished { upid: String, state: TaskState },
|
2020-08-13 08:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a Job and holds the correct lock
|
|
|
|
pub struct Job {
|
|
|
|
jobtype: String,
|
|
|
|
jobname: String,
|
|
|
|
/// The State of the job
|
|
|
|
pub state: JobState,
|
|
|
|
_lock: File,
|
|
|
|
}
|
|
|
|
|
|
|
|
const JOB_STATE_BASEDIR: &str = "/var/lib/proxmox-backup/jobstates";
|
|
|
|
|
|
|
|
/// Create jobstate stat dir with correct permission
|
|
|
|
pub fn create_jobstate_dir() -> Result<(), Error> {
|
|
|
|
let backup_user = crate::backup::backup_user()?;
|
|
|
|
let opts = CreateOptions::new()
|
|
|
|
.owner(backup_user.uid)
|
|
|
|
.group(backup_user.gid);
|
|
|
|
|
|
|
|
create_path(JOB_STATE_BASEDIR, None, Some(opts))
|
|
|
|
.map_err(|err: Error| format_err!("unable to create rrdb stat dir - {}", err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_path(jobtype: &str, jobname: &str) -> PathBuf {
|
|
|
|
let mut path = PathBuf::from(JOB_STATE_BASEDIR);
|
|
|
|
path.push(format!("{}-{}.json", jobtype, jobname));
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_lock<P>(path: P) -> Result<File, Error>
|
|
|
|
where
|
2020-08-18 06:50:14 +00:00
|
|
|
P: AsRef<Path>,
|
2020-08-13 08:29:15 +00:00
|
|
|
{
|
|
|
|
let mut path = path.as_ref().to_path_buf();
|
|
|
|
path.set_extension("lck");
|
2020-09-28 08:50:44 +00:00
|
|
|
let lock = open_file_locked(&path, Duration::new(10, 0), true)?;
|
2020-08-13 12:30:19 +00:00
|
|
|
let backup_user = crate::backup::backup_user()?;
|
|
|
|
nix::unistd::chown(&path, Some(backup_user.uid), Some(backup_user.gid))?;
|
|
|
|
Ok(lock)
|
2020-08-13 08:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the statefile of a job, this is useful if we delete a job
|
|
|
|
pub fn remove_state_file(jobtype: &str, jobname: &str) -> Result<(), Error> {
|
2020-08-13 12:30:19 +00:00
|
|
|
let mut path = get_path(jobtype, jobname);
|
2020-08-13 08:29:15 +00:00
|
|
|
let _lock = get_lock(&path)?;
|
2020-08-18 06:50:14 +00:00
|
|
|
std::fs::remove_file(&path).map_err(|err| {
|
|
|
|
format_err!(
|
|
|
|
"cannot remove statefile for {} - {}: {}",
|
|
|
|
jobtype,
|
|
|
|
jobname,
|
|
|
|
err
|
|
|
|
)
|
|
|
|
})?;
|
2020-08-13 12:30:19 +00:00
|
|
|
path.set_extension("lck");
|
|
|
|
// ignore errors
|
2020-08-18 06:50:14 +00:00
|
|
|
let _ = std::fs::remove_file(&path).map_err(|err| {
|
|
|
|
format_err!(
|
|
|
|
"cannot remove lockfile for {} - {}: {}",
|
|
|
|
jobtype,
|
|
|
|
jobname,
|
|
|
|
err
|
|
|
|
)
|
|
|
|
});
|
2020-08-13 12:30:19 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates the statefile with the state 'Created'
|
|
|
|
/// overwrites if it exists already
|
|
|
|
pub fn create_state_file(jobtype: &str, jobname: &str) -> Result<(), Error> {
|
|
|
|
let mut job = Job::new(jobtype, jobname)?;
|
|
|
|
job.write_state()
|
2020-08-13 08:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the last run time of a job by reading the statefile
|
|
|
|
/// Note that this is not locked
|
|
|
|
pub fn last_run_time(jobtype: &str, jobname: &str) -> Result<i64, Error> {
|
|
|
|
match JobState::load(jobtype, jobname)? {
|
|
|
|
JobState::Created { time } => Ok(time),
|
|
|
|
JobState::Started { upid } | JobState::Finished { upid, .. } => {
|
2020-08-18 06:50:14 +00:00
|
|
|
let upid: UPID = upid
|
|
|
|
.parse()
|
|
|
|
.map_err(|err| format_err!("could not parse upid from state: {}", err))?;
|
2020-08-13 08:29:15 +00:00
|
|
|
Ok(upid.starttime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl JobState {
|
|
|
|
/// Loads and deserializes the jobstate from type and name.
|
|
|
|
/// When the loaded state indicates a started UPID,
|
|
|
|
/// we go and check if it has already stopped, and
|
|
|
|
/// returning the correct state.
|
|
|
|
///
|
|
|
|
/// This does not update the state in the file.
|
|
|
|
pub fn load(jobtype: &str, jobname: &str) -> Result<Self, Error> {
|
|
|
|
if let Some(state) = file_read_optional_string(get_path(jobtype, jobname))? {
|
|
|
|
match serde_json::from_str(&state)? {
|
|
|
|
JobState::Started { upid } => {
|
2020-08-18 06:50:14 +00:00
|
|
|
let parsed: UPID = upid
|
|
|
|
.parse()
|
2020-08-13 08:29:15 +00:00
|
|
|
.map_err(|err| format_err!("error parsing upid: {}", err))?;
|
|
|
|
|
|
|
|
if !worker_is_active_local(&parsed) {
|
2020-08-13 12:30:17 +00:00
|
|
|
let state = upid_read_status(&parsed)
|
2020-08-13 08:29:15 +00:00
|
|
|
.map_err(|err| format_err!("error reading upid log status: {}", err))?;
|
|
|
|
|
2020-08-18 06:50:14 +00:00
|
|
|
Ok(JobState::Finished { upid, state })
|
2020-08-13 08:29:15 +00:00
|
|
|
} else {
|
|
|
|
Ok(JobState::Started { upid })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
other => Ok(other),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(JobState::Created {
|
2020-09-12 13:10:47 +00:00
|
|
|
time: proxmox::tools::time::epoch_i64() - 30,
|
2020-08-13 08:29:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Job {
|
|
|
|
/// Creates a new instance of a job with the correct lock held
|
|
|
|
/// (will be hold until the job is dropped again).
|
|
|
|
///
|
|
|
|
/// This does not load the state from the file, to do that,
|
|
|
|
/// 'load' must be called
|
|
|
|
pub fn new(jobtype: &str, jobname: &str) -> Result<Self, Error> {
|
|
|
|
let path = get_path(jobtype, jobname);
|
|
|
|
|
|
|
|
let _lock = get_lock(&path)?;
|
|
|
|
|
2020-08-18 06:50:14 +00:00
|
|
|
Ok(Self {
|
2020-08-13 08:29:15 +00:00
|
|
|
jobtype: jobtype.to_string(),
|
|
|
|
jobname: jobname.to_string(),
|
|
|
|
state: JobState::Created {
|
2020-09-12 13:10:47 +00:00
|
|
|
time: proxmox::tools::time::epoch_i64(),
|
2020-08-13 08:29:15 +00:00
|
|
|
},
|
|
|
|
_lock,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start the job and update the statefile accordingly
|
|
|
|
/// Fails if the job was already started
|
|
|
|
pub fn start(&mut self, upid: &str) -> Result<(), Error> {
|
|
|
|
match self.state {
|
|
|
|
JobState::Started { .. } => {
|
|
|
|
bail!("cannot start job that is started!");
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2020-08-18 06:50:14 +00:00
|
|
|
self.state = JobState::Started {
|
2020-08-13 08:29:15 +00:00
|
|
|
upid: upid.to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.write_state()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finish the job and update the statefile accordingly with the given taskstate
|
|
|
|
/// Fails if the job was not yet started
|
|
|
|
pub fn finish(&mut self, state: TaskState) -> Result<(), Error> {
|
|
|
|
let upid = match &self.state {
|
|
|
|
JobState::Created { .. } => bail!("cannot finish when not started"),
|
|
|
|
JobState::Started { upid } => upid,
|
|
|
|
JobState::Finished { upid, .. } => upid,
|
2020-08-18 06:50:14 +00:00
|
|
|
}
|
|
|
|
.to_string();
|
2020-08-13 08:29:15 +00:00
|
|
|
|
2020-08-18 06:50:14 +00:00
|
|
|
self.state = JobState::Finished { upid, state };
|
2020-08-13 08:29:15 +00:00
|
|
|
|
|
|
|
self.write_state()
|
|
|
|
}
|
|
|
|
|
2020-08-13 12:30:18 +00:00
|
|
|
pub fn jobtype(&self) -> &str {
|
|
|
|
&self.jobtype
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn jobname(&self) -> &str {
|
|
|
|
&self.jobname
|
|
|
|
}
|
|
|
|
|
2020-08-13 08:29:15 +00:00
|
|
|
fn write_state(&mut self) -> Result<(), Error> {
|
|
|
|
let serialized = serde_json::to_string(&self.state)?;
|
|
|
|
let path = get_path(&self.jobtype, &self.jobname);
|
|
|
|
|
|
|
|
let backup_user = crate::backup::backup_user()?;
|
|
|
|
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644);
|
|
|
|
// set the correct owner/group/permissions while saving file
|
|
|
|
// owner(rw) = backup, group(r)= backup
|
|
|
|
let options = CreateOptions::new()
|
|
|
|
.perm(mode)
|
|
|
|
.owner(backup_user.uid)
|
|
|
|
.group(backup_user.gid);
|
|
|
|
|
2020-08-18 06:50:14 +00:00
|
|
|
replace_file(path, serialized.as_bytes(), options)
|
2020-08-13 08:29:15 +00:00
|
|
|
}
|
|
|
|
}
|