datastore/prune schedules: use JobState for tracking of schedules

like the sync jobs, so that if an admin configures a schedule it
really starts the next time that time is reached not immediately

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2020-09-18 16:03:52 +02:00 committed by Dietmar Maurer
parent 9d3f183ba9
commit 9866de5e3d
2 changed files with 60 additions and 31 deletions

View File

@ -131,6 +131,8 @@ pub fn create_datastore(param: Value) -> Result<(), Error> {
datastore::save_config(&config)?; datastore::save_config(&config)?;
crate::config::jobstate::create_state_file("prune", &datastore.name)?;
Ok(()) Ok(())
} }
@ -312,7 +314,11 @@ pub fn update_datastore(
} }
if gc_schedule.is_some() { data.gc_schedule = gc_schedule; } if gc_schedule.is_some() { data.gc_schedule = gc_schedule; }
if prune_schedule.is_some() { data.prune_schedule = prune_schedule; } let mut prune_schedule_changed = false;
if prune_schedule.is_some() {
prune_schedule_changed = true;
data.prune_schedule = prune_schedule;
}
if verify_schedule.is_some() { data.verify_schedule = verify_schedule; } if verify_schedule.is_some() { data.verify_schedule = verify_schedule; }
if keep_last.is_some() { data.keep_last = keep_last; } if keep_last.is_some() { data.keep_last = keep_last; }
@ -326,6 +332,12 @@ pub fn update_datastore(
datastore::save_config(&config)?; datastore::save_config(&config)?;
// we want to reset the statefile, to avoid an immediate sync in some cases
// (e.g. going from monthly to weekly in the second week of the month)
if prune_schedule_changed {
crate::config::jobstate::create_state_file("prune", &name)?;
}
Ok(()) Ok(())
} }
@ -365,6 +377,8 @@ pub fn delete_datastore(name: String, digest: Option<String>) -> Result<(), Erro
datastore::save_config(&config)?; datastore::save_config(&config)?;
crate::config::jobstate::remove_state_file("prune", &name)?;
Ok(()) Ok(())
} }

View File

@ -337,7 +337,10 @@ async fn schedule_datastore_prune() {
use proxmox_backup::backup::{ use proxmox_backup::backup::{
PruneOptions, DataStore, BackupGroup, compute_prune_info}; PruneOptions, DataStore, BackupGroup, compute_prune_info};
use proxmox_backup::server::{WorkerTask}; use proxmox_backup::server::{WorkerTask};
use proxmox_backup::config::datastore::{self, DataStoreConfig}; use proxmox_backup::config::{
jobstate::{self, Job},
datastore::{self, DataStoreConfig}
};
use proxmox_backup::tools::systemd::time::{ use proxmox_backup::tools::systemd::time::{
parse_calendar_event, compute_next_event}; parse_calendar_event, compute_next_event};
@ -394,16 +397,10 @@ async fn schedule_datastore_prune() {
let worker_type = "prune"; let worker_type = "prune";
let last = match lookup_last_worker(worker_type, &store) { let last = match jobstate::last_run_time(worker_type, &store) {
Ok(Some(upid)) => { Ok(time) => time,
if proxmox_backup::server::worker_is_active_local(&upid) {
continue;
}
upid.starttime
}
Ok(None) => 0,
Err(err) => { Err(err) => {
eprintln!("lookup_last_job_start failed: {}", err); eprintln!("could not get last run time of {} {}: {}", worker_type, store, err);
continue; continue;
} }
}; };
@ -421,6 +418,11 @@ async fn schedule_datastore_prune() {
if next > now { continue; } if next > now { continue; }
let mut job = match Job::new(worker_type, &store) {
Ok(job) => job,
Err(_) => continue, // could not get lock
};
let store2 = store.clone(); let store2 = store.clone();
if let Err(err) = WorkerTask::new_thread( if let Err(err) = WorkerTask::new_thread(
@ -429,6 +431,11 @@ async fn schedule_datastore_prune() {
Userid::backup_userid().clone(), Userid::backup_userid().clone(),
false, false,
move |worker| { move |worker| {
job.start(&worker.upid().to_string())?;
let result = {
worker.log(format!("Starting datastore prune on store \"{}\"", store)); worker.log(format!("Starting datastore prune on store \"{}\"", store));
worker.log(format!("task triggered by schedule '{}'", event_str)); worker.log(format!("task triggered by schedule '{}'", event_str));
worker.log(format!("retention options: {}", prune_options.cli_options_string())); worker.log(format!("retention options: {}", prune_options.cli_options_string()));
@ -455,8 +462,16 @@ async fn schedule_datastore_prune() {
} }
} }
} }
Ok(()) Ok(())
};
let status = worker.create_state(&result);
if let Err(err) = job.finish(status) {
eprintln!("could not finish job state for {}: {}", worker_type, err);
}
result
} }
) { ) {
eprintln!("unable to start datastore prune on store {} - {}", store2, err); eprintln!("unable to start datastore prune on store {} - {}", store2, err);