proxy: split out code to run garbage collection job

This commit is contained in:
Dietmar Maurer 2020-10-30 10:54:31 +01:00
parent b15751bf55
commit 3b707fbb8f
3 changed files with 68 additions and 34 deletions

View File

@ -248,8 +248,6 @@ async fn schedule_datastore_garbage_collection() {
}, },
}; };
let email = server::lookup_user_email(Userid::root_userid());
let config = match datastore::config() { let config = match datastore::config() {
Err(err) => { Err(err) => {
eprintln!("unable to read datastore config - {}", err); eprintln!("unable to read datastore config - {}", err);
@ -329,39 +327,10 @@ async fn schedule_datastore_garbage_collection() {
Err(_) => continue, // could not get lock Err(_) => continue, // could not get lock
}; };
let store2 = store.clone(); let auth_id = Authid::backup_auth_id();
let email2 = email.clone();
if let Err(err) = WorkerTask::new_thread( if let Err(err) = crate::server::do_garbage_collection_job(job, datastore, auth_id, Some(event_str)) {
worker_type, eprintln!("unable to start garbage collection job on datastore {} - {}", store, err);
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);
} }
} }
} }

View File

@ -38,5 +38,8 @@ pub use verify_job::*;
mod prune_job; mod prune_job;
pub use prune_job::*; pub use prune_job::*;
mod gc_job;
pub use gc_job::*;
mod email_notifications; mod email_notifications;
pub use email_notifications::*; pub use email_notifications::*;

62
src/server/gc_job.rs Normal file
View File

@ -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<DataStore>,
auth_id: &Authid,
schedule: Option<String>,
) -> Result<String, Error> {
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)
}