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

@ -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(())
}