cleanup: factor out common client code to view task log/result

This commit is contained in:
Dietmar Maurer 2021-01-29 10:10:04 +01:00
parent 1f2c4713ef
commit 4470eba551
4 changed files with 51 additions and 95 deletions

View File

@ -216,23 +216,6 @@ fn connect_do(server: &str, port: u16, auth_id: &Authid) -> Result<HttpClient, E
HttpClient::new(server, port, auth_id, options)
}
async fn view_task_result(
client: HttpClient,
result: Value,
output_format: &str,
) -> Result<(), Error> {
let data = &result["data"];
if output_format == "text" {
if let Some(upid) = data.as_str() {
display_task_log(client, upid, true).await?;
}
} else {
format_and_print_result(&data, &output_format);
}
Ok(())
}
async fn api_datastore_list_snapshots(
client: &HttpClient,
store: &str,

View File

@ -14,41 +14,6 @@ use proxmox_backup::client::*;
mod proxmox_backup_manager;
use proxmox_backup_manager::*;
async fn view_task_result(
client: HttpClient,
result: Value,
output_format: &str,
) -> Result<(), Error> {
let data = &result["data"];
if output_format == "text" {
if let Some(upid) = data.as_str() {
display_task_log(client, upid, true).await?;
}
} else {
format_and_print_result(&data, &output_format);
}
Ok(())
}
// Note: local workers should print logs to stdout, so there is no need
// to fetch/display logs. We just wait for the worker to finish.
pub async fn wait_for_local_worker(upid_str: &str) -> Result<(), Error> {
let upid: proxmox_backup::server::UPID = upid_str.parse()?;
let sleep_duration = core::time::Duration::new(0, 100_000_000);
loop {
if proxmox_backup::server::worker_is_active_local(&upid) {
tokio::time::sleep(sleep_duration).await;
} else {
break;
}
}
Ok(())
}
#[api(
input: {
properties: {

View File

@ -22,13 +22,9 @@ use proxmox_backup::{
render_bytes_human_readable,
},
client::{
HttpClient,
display_task_log,
connect_to_localhost,
},
server::{
UPID,
worker_is_active_local,
view_task_result,
wait_for_local_worker,
},
api2::{
self,
@ -60,41 +56,6 @@ use proxmox_backup::{
mod proxmox_tape;
use proxmox_tape::*;
async fn view_task_result(
client: HttpClient,
result: Value,
output_format: &str,
) -> Result<(), Error> {
let data = &result["data"];
if output_format == "text" {
if let Some(upid) = data.as_str() {
display_task_log(client, upid, true).await?;
}
} else {
format_and_print_result(&data, &output_format);
}
Ok(())
}
// Note: local workers should print logs to stdout, so there is no need
// to fetch/display logs. We just wait for the worker to finish.
pub async fn wait_for_local_worker(upid_str: &str) -> Result<(), Error> {
let upid: UPID = upid_str.parse()?;
let sleep_duration = core::time::Duration::new(0, 100_000_000);
loop {
if worker_is_active_local(&upid) {
tokio::time::sleep(sleep_duration).await;
} else {
break;
}
}
Ok(())
}
pub fn lookup_drive_name(
param: &Value,
config: &SectionConfigData,

View File

@ -1,12 +1,21 @@
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
use anyhow::{bail, Error};
use serde_json::json;
use serde_json::{json, Value};
use tokio::signal::unix::{signal, SignalKind};
use futures::*;
use proxmox::api::cli::format_and_print_result;
use super::HttpClient;
use crate::tools;
use crate::{
server::{
worker_is_active_local,
UPID,
},
tools,
};
/// Display task log on console
///
@ -94,3 +103,41 @@ pub async fn display_task_log(
Ok(())
}
/// Display task result (upid), or view task log - depending on output format
pub async fn view_task_result(
client: HttpClient,
result: Value,
output_format: &str,
) -> Result<(), Error> {
let data = &result["data"];
if output_format == "text" {
if let Some(upid) = data.as_str() {
display_task_log(client, upid, true).await?;
}
} else {
format_and_print_result(&data, &output_format);
}
Ok(())
}
/// Wait for a locally spanned worker task
///
/// Note: local workers should print logs to stdout, so there is no
/// need to fetch/display logs. We just wait for the worker to finish.
pub async fn wait_for_local_worker(upid_str: &str) -> Result<(), Error> {
let upid: UPID = upid_str.parse()?;
let sleep_duration = core::time::Duration::new(0, 100_000_000);
loop {
if worker_is_active_local(&upid) {
tokio::time::sleep(sleep_duration).await;
} else {
break;
}
}
Ok(())
}