2020-10-30 09:54:31 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use anyhow::Error;
|
|
|
|
|
2021-09-10 10:25:32 +00:00
|
|
|
use pbs_api_types::Authid;
|
|
|
|
|
2020-10-30 09:54:31 +00:00
|
|
|
use crate::{
|
|
|
|
server::WorkerTask,
|
|
|
|
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>,
|
2020-11-02 11:34:37 +00:00
|
|
|
to_stdout: bool,
|
2020-10-30 09:54:31 +00:00
|
|
|
) -> Result<String, Error> {
|
|
|
|
|
|
|
|
let store = datastore.name().to_string();
|
|
|
|
|
2020-11-04 10:27:57 +00:00
|
|
|
let (email, notify) = crate::server::lookup_datastore_notify_settings(&store);
|
|
|
|
|
2020-10-30 09:54:31 +00:00
|
|
|
let worker_type = job.jobtype().to_string();
|
|
|
|
let upid_str = WorkerTask::new_thread(
|
|
|
|
&worker_type,
|
|
|
|
Some(store.clone()),
|
|
|
|
auth_id.clone(),
|
2020-11-02 11:34:37 +00:00
|
|
|
to_stdout,
|
2020-10-30 09:54:31 +00:00
|
|
|
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);
|
|
|
|
|
2021-01-19 11:09:33 +00:00
|
|
|
if let Err(err) = job.finish(status) {
|
|
|
|
eprintln!(
|
2020-10-30 09:54:31 +00:00
|
|
|
"could not finish job state for {}: {}",
|
|
|
|
job.jobtype().to_string(),
|
|
|
|
err
|
2021-01-19 11:09:33 +00:00
|
|
|
);
|
2020-10-30 09:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(email) = email {
|
|
|
|
let gc_status = datastore.last_gc_status();
|
2020-11-04 10:27:57 +00:00
|
|
|
if let Err(err) = crate::server::send_gc_status(&email, notify, &store, &gc_status, &result) {
|
2020-10-30 09:54:31 +00:00
|
|
|
eprintln!("send gc notification failed: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(upid_str)
|
|
|
|
}
|