diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs index 745961b7..329d7f57 100644 --- a/src/bin/proxmox-backup-proxy.rs +++ b/src/bin/proxmox-backup-proxy.rs @@ -248,8 +248,6 @@ async fn schedule_datastore_garbage_collection() { }, }; - let email = server::lookup_user_email(Userid::root_userid()); - let config = match datastore::config() { Err(err) => { eprintln!("unable to read datastore config - {}", err); @@ -329,39 +327,10 @@ async fn schedule_datastore_garbage_collection() { Err(_) => continue, // could not get lock }; - let store2 = store.clone(); - let email2 = email.clone(); + let auth_id = Authid::backup_auth_id(); - if let Err(err) = WorkerTask::new_thread( - worker_type, - Some(store.clone()), - Authid::backup_auth_id().clone(), - false, - move |worker| { - job.start(&worker.upid().to_string())?; - - worker.log(format!("starting garbage collection on store {}", store)); - worker.log(format!("task triggered by schedule '{}'", event_str)); - - let result = datastore.garbage_collection(&*worker, worker.upid()); - - let status = worker.create_state(&result); - - if let Err(err) = job.finish(status) { - eprintln!("could not finish job state for {}: {}", worker_type, err); - } - - if let Some(email2) = email2 { - let gc_status = datastore.last_gc_status(); - if let Err(err) = crate::server::send_gc_status(&email2, datastore.name(), &gc_status, &result) { - eprintln!("send gc notification failed: {}", err); - } - } - - result - } - ) { - eprintln!("unable to start garbage collection on store {} - {}", store2, err); + if let Err(err) = crate::server::do_garbage_collection_job(job, datastore, auth_id, Some(event_str)) { + eprintln!("unable to start garbage collection job on datastore {} - {}", store, err); } } } diff --git a/src/server.rs b/src/server.rs index eddff722..f0db500e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -38,5 +38,8 @@ pub use verify_job::*; mod prune_job; pub use prune_job::*; +mod gc_job; +pub use gc_job::*; + mod email_notifications; pub use email_notifications::*; diff --git a/src/server/gc_job.rs b/src/server/gc_job.rs new file mode 100644 index 00000000..dabbb237 --- /dev/null +++ b/src/server/gc_job.rs @@ -0,0 +1,62 @@ +use std::sync::Arc; +use anyhow::Error; + +use crate::{ + server::WorkerTask, + api2::types::*, + server::jobstate::Job, + backup::DataStore, +}; + +/// Runs a garbage collection job. +pub fn do_garbage_collection_job( + mut job: Job, + datastore: Arc, + auth_id: &Authid, + schedule: Option, +) -> Result { + + let email = crate::server::lookup_user_email(auth_id.user()); + + let store = datastore.name().to_string(); + + let worker_type = job.jobtype().to_string(); + let upid_str = WorkerTask::new_thread( + &worker_type, + Some(store.clone()), + auth_id.clone(), + false, + move |worker| { + job.start(&worker.upid().to_string())?; + + worker.log(format!("starting garbage collection on store {}", store)); + if let Some(event_str) = schedule { + worker.log(format!("task triggered by schedule '{}'", event_str)); + } + + let result = datastore.garbage_collection(&*worker, worker.upid()); + + let status = worker.create_state(&result); + + match job.finish(status) { + Err(err) => eprintln!( + "could not finish job state for {}: {}", + job.jobtype().to_string(), + err + ), + Ok(_) => (), + } + + if let Some(email) = email { + let gc_status = datastore.last_gc_status(); + if let Err(err) = crate::server::send_gc_status(&email, &store, &gc_status, &result) { + eprintln!("send gc notification failed: {}", err); + } + } + + result + } + )?; + + Ok(upid_str) +}