cleanup: factor out common client code to view task log/result
This commit is contained in:
parent
1f2c4713ef
commit
4470eba551
|
@ -216,23 +216,6 @@ fn connect_do(server: &str, port: u16, auth_id: &Authid) -> Result<HttpClient, E
|
||||||
HttpClient::new(server, port, auth_id, options)
|
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(
|
async fn api_datastore_list_snapshots(
|
||||||
client: &HttpClient,
|
client: &HttpClient,
|
||||||
store: &str,
|
store: &str,
|
||||||
|
|
|
@ -14,41 +14,6 @@ use proxmox_backup::client::*;
|
||||||
mod proxmox_backup_manager;
|
mod proxmox_backup_manager;
|
||||||
use 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(
|
#[api(
|
||||||
input: {
|
input: {
|
||||||
properties: {
|
properties: {
|
||||||
|
|
|
@ -22,13 +22,9 @@ use proxmox_backup::{
|
||||||
render_bytes_human_readable,
|
render_bytes_human_readable,
|
||||||
},
|
},
|
||||||
client::{
|
client::{
|
||||||
HttpClient,
|
|
||||||
display_task_log,
|
|
||||||
connect_to_localhost,
|
connect_to_localhost,
|
||||||
},
|
view_task_result,
|
||||||
server::{
|
wait_for_local_worker,
|
||||||
UPID,
|
|
||||||
worker_is_active_local,
|
|
||||||
},
|
},
|
||||||
api2::{
|
api2::{
|
||||||
self,
|
self,
|
||||||
|
@ -60,41 +56,6 @@ use proxmox_backup::{
|
||||||
mod proxmox_tape;
|
mod proxmox_tape;
|
||||||
use 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(
|
pub fn lookup_drive_name(
|
||||||
param: &Value,
|
param: &Value,
|
||||||
config: &SectionConfigData,
|
config: &SectionConfigData,
|
||||||
|
|
|
@ -1,12 +1,21 @@
|
||||||
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
|
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
|
||||||
|
|
||||||
use anyhow::{bail, Error};
|
use anyhow::{bail, Error};
|
||||||
use serde_json::json;
|
use serde_json::{json, Value};
|
||||||
use tokio::signal::unix::{signal, SignalKind};
|
use tokio::signal::unix::{signal, SignalKind};
|
||||||
use futures::*;
|
use futures::*;
|
||||||
|
|
||||||
|
use proxmox::api::cli::format_and_print_result;
|
||||||
|
|
||||||
use super::HttpClient;
|
use super::HttpClient;
|
||||||
use crate::tools;
|
use crate::{
|
||||||
|
server::{
|
||||||
|
worker_is_active_local,
|
||||||
|
UPID,
|
||||||
|
},
|
||||||
|
tools,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Display task log on console
|
/// Display task log on console
|
||||||
///
|
///
|
||||||
|
@ -94,3 +103,41 @@ pub async fn display_task_log(
|
||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue