]> git.proxmox.com Git - proxmox-backup.git/blob - src/server/gc_job.rs
namespaces: move max-depth check to api type
[proxmox-backup.git] / src / server / gc_job.rs
1 use anyhow::Error;
2 use std::sync::Arc;
3
4 use proxmox_sys::task_log;
5
6 use pbs_api_types::Authid;
7 use pbs_datastore::DataStore;
8 use proxmox_rest_server::WorkerTask;
9
10 use crate::server::jobstate::Job;
11
12 /// Runs a garbage collection job.
13 pub fn do_garbage_collection_job(
14 mut job: Job,
15 datastore: Arc<DataStore>,
16 auth_id: &Authid,
17 schedule: Option<String>,
18 to_stdout: bool,
19 ) -> Result<String, Error> {
20 let store = datastore.name().to_string();
21
22 let (email, notify) = crate::server::lookup_datastore_notify_settings(&store);
23
24 let worker_type = job.jobtype().to_string();
25 let upid_str = WorkerTask::new_thread(
26 &worker_type,
27 Some(store.clone()),
28 auth_id.to_string(),
29 to_stdout,
30 move |worker| {
31 job.start(&worker.upid().to_string())?;
32
33 task_log!(worker, "starting garbage collection on store {}", store);
34 if let Some(event_str) = schedule {
35 task_log!(worker, "task triggered by schedule '{}'", event_str);
36 }
37
38 let result = datastore.garbage_collection(&*worker, worker.upid());
39
40 let status = worker.create_state(&result);
41
42 if let Err(err) = job.finish(status) {
43 eprintln!(
44 "could not finish job state for {}: {}",
45 job.jobtype().to_string(),
46 err
47 );
48 }
49
50 if let Some(email) = email {
51 let gc_status = datastore.last_gc_status();
52 if let Err(err) =
53 crate::server::send_gc_status(&email, notify, &store, &gc_status, &result)
54 {
55 eprintln!("send gc notification failed: {}", err);
56 }
57 }
58
59 result
60 },
61 )?;
62
63 Ok(upid_str)
64 }