2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2019-12-08 10:27:15 +00:00
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
use super::HttpClient;
|
2020-11-19 09:02:52 +00:00
|
|
|
use crate::tools;
|
2019-12-08 10:27:15 +00:00
|
|
|
|
|
|
|
pub async fn display_task_log(
|
|
|
|
client: HttpClient,
|
|
|
|
upid_str: &str,
|
|
|
|
strip_date: bool,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2020-11-19 09:02:52 +00:00
|
|
|
let path = format!("api2/json/nodes/localhost/tasks/{}/log", tools::percent_encode_component(upid_str));
|
2019-12-08 10:27:15 +00:00
|
|
|
|
|
|
|
let mut start = 1;
|
|
|
|
let limit = 500;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let param = json!({ "start": start, "limit": limit, "test-status": true });
|
|
|
|
let result = client.get(&path, Some(param)).await?;
|
|
|
|
|
|
|
|
let active = result["active"].as_bool().unwrap();
|
|
|
|
let total = result["total"].as_u64().unwrap();
|
|
|
|
let data = result["data"].as_array().unwrap();
|
|
|
|
|
|
|
|
let lines = data.len();
|
|
|
|
|
|
|
|
for item in data {
|
|
|
|
let n = item["n"].as_u64().unwrap();
|
|
|
|
let t = item["t"].as_str().unwrap();
|
|
|
|
if n != start { bail!("got wrong line number in response data ({} != {}", n, start); }
|
|
|
|
if strip_date && t.len() > 27 && &t[25..27] == ": " {
|
|
|
|
let line = &t[27..];
|
|
|
|
println!("{}", line);
|
|
|
|
} else {
|
|
|
|
println!("{}", t);
|
|
|
|
}
|
|
|
|
start += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if start > total {
|
|
|
|
if active {
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(1000));
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2020-10-14 12:22:38 +00:00
|
|
|
} else if lines != limit {
|
|
|
|
bail!("got wrong number of lines from server ({} != {})", lines, limit);
|
2019-12-08 10:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|