1 //! Generic JobState handling
3 //! A 'Job' can have 3 states
4 //! - Created, when a schedule was created but never executed
5 //! - Started, when a job is running right now
6 //! - Finished, when a job was running in the past
8 //! and is identified by 2 values: jobtype and jobname (e.g. 'syncjob' and 'myfirstsyncjob')
10 //! This module Provides 2 helper structs to handle those coniditons
11 //! 'Job' which handles locking and writing to a file
12 //! 'JobState' which is the actual state
14 //! an example usage would be
16 //! # use anyhow::{bail, Error};
17 //! # use proxmox_backup::server::TaskState;
18 //! # use proxmox_backup::server::jobstate::*;
19 //! # fn some_code() -> TaskState { TaskState::OK { endtime: 0 } }
20 //! # fn code() -> Result<(), Error> {
21 //! // locks the correct file under /var/lib
22 //! // or fails if someone else holds the lock
23 //! let mut job = match Job::new("jobtype", "jobname") {
25 //! Err(err) => bail!("could not lock jobstate"),
28 //! // job holds the lock, we can start it
29 //! job.start("someupid")?;
31 //! let task_state = some_code();
32 //! job.finish(task_state)?;
34 //! // release the lock
41 use std
::path
::{Path, PathBuf}
;
42 use std
::time
::Duration
;
44 use anyhow
::{bail, format_err, Error}
;
45 use proxmox
::tools
::fs
::{
46 create_path
, file_read_optional_string
, open_file_locked
, replace_file
, CreateOptions
,
48 use serde
::{Deserialize, Serialize}
;
51 tools
::systemd
::time
::{
55 api2
::types
::JobScheduleStatus
,
60 worker_is_active_local
,
64 #[serde(rename_all = "kebab-case")]
65 #[derive(Serialize, Deserialize)]
66 /// Represents the State of a specific Job
68 /// A job was created at 'time', but never started/finished
69 Created { time: i64 }
,
70 /// The Job was last started in 'upid',
71 Started { upid: String }
,
72 /// The Job was last started in 'upid', which finished with 'state', and was last updated at 'updated'
80 /// Represents a Job and holds the correct lock
84 /// The State of the job
89 const JOB_STATE_BASEDIR
: &str = "/var/lib/proxmox-backup/jobstates";
91 /// Create jobstate stat dir with correct permission
92 pub fn create_jobstate_dir() -> Result
<(), Error
> {
93 let backup_user
= crate::backup
::backup_user()?
;
94 let opts
= CreateOptions
::new()
95 .owner(backup_user
.uid
)
96 .group(backup_user
.gid
);
98 create_path(JOB_STATE_BASEDIR
, None
, Some(opts
))
99 .map_err(|err
: Error
| format_err
!("unable to create rrdb stat dir - {}", err
))?
;
104 fn get_path(jobtype
: &str, jobname
: &str) -> PathBuf
{
105 let mut path
= PathBuf
::from(JOB_STATE_BASEDIR
);
106 path
.push(format
!("{}-{}.json", jobtype
, jobname
));
110 fn get_lock
<P
>(path
: P
) -> Result
<File
, Error
>
114 let mut path
= path
.as_ref().to_path_buf();
115 path
.set_extension("lck");
116 let lock
= open_file_locked(&path
, Duration
::new(10, 0), true)?
;
117 let backup_user
= crate::backup
::backup_user()?
;
118 nix
::unistd
::chown(&path
, Some(backup_user
.uid
), Some(backup_user
.gid
))?
;
122 /// Removes the statefile of a job, this is useful if we delete a job
123 pub fn remove_state_file(jobtype
: &str, jobname
: &str) -> Result
<(), Error
> {
124 let mut path
= get_path(jobtype
, jobname
);
125 let _lock
= get_lock(&path
)?
;
126 std
::fs
::remove_file(&path
).map_err(|err
| {
128 "cannot remove statefile for {} - {}: {}",
134 path
.set_extension("lck");
136 let _
= std
::fs
::remove_file(&path
).map_err(|err
| {
138 "cannot remove lockfile for {} - {}: {}",
147 /// Creates the statefile with the state 'Created'
148 /// overwrites if it exists already
149 pub fn create_state_file(jobtype
: &str, jobname
: &str) -> Result
<(), Error
> {
150 let mut job
= Job
::new(jobtype
, jobname
)?
;
154 /// Tries to update the state file with the current time
155 /// if the job is currently running, does nothing,
156 pub fn try_update_state_file(jobtype
: &str, jobname
: &str) -> Result
<(), Error
> {
157 let mut job
= match Job
::new(jobtype
, jobname
) {
159 Err(_
) => return Ok(()), // was locked (running), so do not update
161 let time
= proxmox
::tools
::time
::epoch_i64();
163 job
.state
= match JobState
::load(jobtype
, jobname
)?
{
164 JobState
::Created { .. }
=> JobState
::Created { time }
,
165 JobState
::Started { .. }
=> return Ok(()), // currently running (without lock?)
170 } => JobState
::Finished
{
179 /// Returns the last run time of a job by reading the statefile
180 /// Note that this is not locked
181 pub fn last_run_time(jobtype
: &str, jobname
: &str) -> Result
<i64, Error
> {
182 match JobState
::load(jobtype
, jobname
)?
{
183 JobState
::Created { time }
=> Ok(time
),
188 JobState
::Started { upid }
189 | JobState
::Finished
{
194 let upid
: UPID
= upid
196 .map_err(|err
| format_err
!("could not parse upid from state: {}", err
))?
;
203 /// Loads and deserializes the jobstate from type and name.
204 /// When the loaded state indicates a started UPID,
205 /// we go and check if it has already stopped, and
206 /// returning the correct state.
208 /// This does not update the state in the file.
209 pub fn load(jobtype
: &str, jobname
: &str) -> Result
<Self, Error
> {
210 if let Some(state
) = file_read_optional_string(get_path(jobtype
, jobname
))?
{
211 match serde_json
::from_str(&state
)?
{
212 JobState
::Started { upid }
=> {
213 let parsed
: UPID
= upid
215 .map_err(|err
| format_err
!("error parsing upid: {}", err
))?
;
217 if !worker_is_active_local(&parsed
) {
218 let state
= upid_read_status(&parsed
)
219 .map_err(|err
| format_err
!("error reading upid log status: {}", err
))?
;
221 Ok(JobState
::Finished
{
227 Ok(JobState
::Started { upid }
)
233 Ok(JobState
::Created
{
234 time
: proxmox
::tools
::time
::epoch_i64() - 30,
241 /// Creates a new instance of a job with the correct lock held
242 /// (will be hold until the job is dropped again).
244 /// This does not load the state from the file, to do that,
245 /// 'load' must be called
246 pub fn new(jobtype
: &str, jobname
: &str) -> Result
<Self, Error
> {
247 let path
= get_path(jobtype
, jobname
);
249 let _lock
= get_lock(&path
)?
;
252 jobtype
: jobtype
.to_string(),
253 jobname
: jobname
.to_string(),
254 state
: JobState
::Created
{
255 time
: proxmox
::tools
::time
::epoch_i64(),
261 /// Start the job and update the statefile accordingly
262 /// Fails if the job was already started
263 pub fn start(&mut self, upid
: &str) -> Result
<(), Error
> {
264 if let JobState
::Started { .. }
= self.state
{
265 bail
!("cannot start job that is started!");
268 self.state
= JobState
::Started
{
269 upid
: upid
.to_string(),
275 /// Finish the job and update the statefile accordingly with the given taskstate
276 /// Fails if the job was not yet started
277 pub fn finish(&mut self, state
: TaskState
) -> Result
<(), Error
> {
278 let upid
= match &self.state
{
279 JobState
::Created { .. }
=> bail
!("cannot finish when not started"),
280 JobState
::Started { upid }
=> upid
,
281 JobState
::Finished { upid, .. }
=> upid
,
285 self.state
= JobState
::Finished
{
294 pub fn jobtype(&self) -> &str {
298 pub fn jobname(&self) -> &str {
302 fn write_state(&mut self) -> Result
<(), Error
> {
303 let serialized
= serde_json
::to_string(&self.state
)?
;
304 let path
= get_path(&self.jobtype
, &self.jobname
);
306 let backup_user
= crate::backup
::backup_user()?
;
307 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0644);
308 // set the correct owner/group/permissions while saving file
309 // owner(rw) = backup, group(r)= backup
310 let options
= CreateOptions
::new()
312 .owner(backup_user
.uid
)
313 .group(backup_user
.gid
);
315 replace_file(path
, serialized
.as_bytes(), options
)
319 pub fn compute_schedule_status(
320 job_state
: &JobState
,
321 schedule
: Option
<&str>,
322 ) -> Result
<JobScheduleStatus
, Error
> {
323 let (upid
, endtime
, state
, last
) = match job_state
{
324 JobState
::Created { time }
=> (None
, None
, None
, *time
),
325 JobState
::Started { upid }
=> {
326 let parsed_upid
: UPID
= upid
.parse()?
;
327 (Some(upid
), None
, None
, parsed_upid
.starttime
)
334 let last
= updated
.unwrap_or_else(|| state
.endtime());
337 Some(state
.endtime()),
338 Some(state
.to_string()),
344 let mut status
= JobScheduleStatus
::default();
345 status
.last_run_upid
= upid
.map(String
::from
);
346 status
.last_run_state
= state
;
347 status
.last_run_endtime
= endtime
;
349 if let Some(schedule
) = schedule
{
350 if let Ok(event
) = parse_calendar_event(&schedule
) {
352 status
.next_run
= compute_next_event(&event
, last
, false).unwrap_or(None
);