api/{pull, sync}: refactor to do_sync_job

and move the pull parameters into the worker, so that the task log
contains the error if there is one

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2020-08-13 10:29:17 +02:00 committed by Dietmar Maurer
parent 664d8a2765
commit 42b68f72e6
2 changed files with 35 additions and 22 deletions

View File

@ -6,7 +6,7 @@ use proxmox::api::router::SubdirMap;
use proxmox::{list_subdirs_api_method, sortable}; use proxmox::{list_subdirs_api_method, sortable};
use crate::api2::types::*; use crate::api2::types::*;
use crate::api2::pull::{get_pull_parameters}; use crate::api2::pull::do_sync_job;
use crate::config::sync::{self, SyncJobStatus, SyncJobConfig}; use crate::config::sync::{self, SyncJobStatus, SyncJobConfig};
use crate::server::UPID; use crate::server::UPID;
use crate::config::jobstate::JobState; use crate::config::jobstate::JobState;
@ -76,7 +76,7 @@ pub fn list_sync_jobs(
} }
)] )]
/// Runs the sync jobs manually. /// Runs the sync jobs manually.
async fn run_sync_job( fn run_sync_job(
id: String, id: String,
_info: &ApiMethod, _info: &ApiMethod,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
@ -87,26 +87,7 @@ async fn run_sync_job(
let userid: Userid = rpcenv.get_user().unwrap().parse()?; let userid: Userid = rpcenv.get_user().unwrap().parse()?;
let delete = sync_job.remove_vanished.unwrap_or(true); let upid_str = do_sync_job(&id, sync_job, &userid)?;
let (client, src_repo, tgt_store) = get_pull_parameters(&sync_job.store, &sync_job.remote, &sync_job.remote_store).await?;
let upid_str = WorkerTask::spawn("syncjob", Some(id.clone()), userid, false, move |worker| async move {
worker.log(format!("sync job '{}' start", &id));
crate::client::pull::pull_store(
&worker,
&client,
&src_repo,
tgt_store.clone(),
delete,
Userid::backup_userid().clone(),
).await?;
worker.log(format!("sync job '{}' end", &id));
Ok(())
})?;
Ok(upid_str) Ok(upid_str)
} }

View File

@ -12,6 +12,7 @@ use crate::client::{HttpClient, HttpClientOptions, BackupRepository, pull::pull_
use crate::api2::types::*; use crate::api2::types::*;
use crate::config::{ use crate::config::{
remote, remote,
sync::SyncJobConfig,
acl::{PRIV_DATASTORE_BACKUP, PRIV_DATASTORE_PRUNE, PRIV_REMOTE_READ}, acl::{PRIV_DATASTORE_BACKUP, PRIV_DATASTORE_PRUNE, PRIV_REMOTE_READ},
cached_user_info::CachedUserInfo, cached_user_info::CachedUserInfo,
}; };
@ -62,6 +63,37 @@ pub async fn get_pull_parameters(
Ok((client, src_repo, tgt_store)) Ok((client, src_repo, tgt_store))
} }
pub fn do_sync_job(
id: &str,
sync_job: SyncJobConfig,
userid: &Userid,
) -> Result<String, Error> {
let job_id = id.to_string();
let upid_str = WorkerTask::spawn("syncjob", Some(id.to_string()), userid.clone(), false, move |worker| async move {
let delete = sync_job.remove_vanished.unwrap_or(true);
let (client, src_repo, tgt_store) = get_pull_parameters(&sync_job.store, &sync_job.remote, &sync_job.remote_store).await?;
worker.log(format!("sync job '{}' start", &job_id));
crate::client::pull::pull_store(
&worker,
&client,
&src_repo,
tgt_store.clone(),
delete,
Userid::backup_userid().clone(),
).await?;
worker.log(format!("sync job '{}' end", &job_id));
Ok(())
})?;
Ok(upid_str)
}
#[api( #[api(
input: { input: {
properties: { properties: {