From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 949D56086B for ; Thu, 13 Aug 2020 10:30:01 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8B8BF181CB for ; Thu, 13 Aug 2020 10:29:31 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id EED101809C for ; Thu, 13 Aug 2020 10:29:23 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id B94D8445E6 for ; Thu, 13 Aug 2020 10:29:23 +0200 (CEST) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Thu, 13 Aug 2020 10:29:18 +0200 Message-Id: <20200813082921.28946-7-d.csapak@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200813082921.28946-1-d.csapak@proxmox.com> References: <20200813082921.28946-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.046 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods NO_DNS_FOR_FROM 0.379 Envelope sender has no MX or A DNS records RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record Subject: [pbs-devel] [PATCH proxmox-backup v3 6/9] api2/pull: extend do_sync_job to also handle schedule and jobstate X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Aug 2020 08:30:01 -0000 so that we can log if triggered by a schedule, and writing to a jobstatefile also correctly polls now the abort_future of the worker, so that users can stop a sync Signed-off-by: Dominik Csapak --- src/api2/admin/sync.rs | 7 +++-- src/api2/pull.rs | 64 ++++++++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/api2/admin/sync.rs b/src/api2/admin/sync.rs index 47824887..c09bea4f 100644 --- a/src/api2/admin/sync.rs +++ b/src/api2/admin/sync.rs @@ -9,7 +9,7 @@ use crate::api2::types::*; use crate::api2::pull::do_sync_job; use crate::config::sync::{self, SyncJobStatus, SyncJobConfig}; use crate::server::UPID; -use crate::config::jobstate::JobState; +use crate::config::jobstate::{Job, JobState}; use crate::tools::systemd::time::{ parse_calendar_event, compute_next_event}; @@ -87,7 +87,10 @@ fn run_sync_job( let userid: Userid = rpcenv.get_user().unwrap().parse()?; - let upid_str = do_sync_job(&id, sync_job, &userid)?; + let mut job = Job::new("syncjob", &id)?; + job.load()?; + + let upid_str = do_sync_job(&id, sync_job, &userid, None, job)?; Ok(upid_str) } diff --git a/src/api2/pull.rs b/src/api2/pull.rs index 3265853b..e8eb35e1 100644 --- a/src/api2/pull.rs +++ b/src/api2/pull.rs @@ -2,6 +2,7 @@ use std::sync::{Arc}; use anyhow::{format_err, Error}; +use futures::{select, future::FutureExt}; use proxmox::api::api; use proxmox::api::{ApiMethod, Router, RpcEnvironment, Permission}; @@ -13,6 +14,7 @@ use crate::api2::types::*; use crate::config::{ remote, sync::SyncJobConfig, + jobstate::Job, acl::{PRIV_DATASTORE_BACKUP, PRIV_DATASTORE_PRUNE, PRIV_REMOTE_READ}, cached_user_info::CachedUserInfo, }; @@ -67,29 +69,61 @@ pub fn do_sync_job( id: &str, sync_job: SyncJobConfig, userid: &Userid, + schedule: Option, + mut job: Job, ) -> Result { let job_id = id.to_string(); + let worker_type = "syncjob"; - 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?; + let upid_str = WorkerTask::spawn( + worker_type, + Some(id.to_string()), + userid.clone(), + false, + move |worker| async move { - worker.log(format!("sync job '{}' start", &job_id)); + job.start(&worker.upid().to_string())?; - crate::client::pull::pull_store( - &worker, - &client, - &src_repo, - tgt_store.clone(), - delete, - Userid::backup_userid().clone(), - ).await?; + let worker2 = worker.clone(); - worker.log(format!("sync job '{}' end", &job_id)); + let worker_future = async move { - Ok(()) - })?; + 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!("Starting datastore sync job '{}'", job_id)); + if let Some(event_str) = schedule { + worker.log(format!("task triggered by schedule '{}'", event_str)); + } + worker.log(format!("Sync datastore '{}' from '{}/{}'", + sync_job.store, sync_job.remote, sync_job.remote_store)); + + 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(()) + }; + + let mut abort_future = worker2.abort_future().map(|_| Err(format_err!("sync aborted"))); + + let res = select!{ + worker = worker_future.fuse() => worker, + abort = abort_future => abort, + }; + + let status = worker2.create_state(&res); + + match job.finish(status) { + Ok(_) => {}, + Err(err) => { + eprintln!("could not finish job state: {}", err); + } + } + + res + })?; Ok(upid_str) } -- 2.20.1