server/worker_task: let upid_read_status also return the endtime

the endtime should be the timestamp of the last log line
or if there is no log at all, the starttime

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2020-08-13 10:29:14 +02:00 committed by Dietmar Maurer
parent 4c116bafb8
commit ae197dda23
2 changed files with 19 additions and 10 deletions

View File

@ -105,7 +105,7 @@ async fn get_task_status(
if crate::server::worker_is_active(&upid).await? { if crate::server::worker_is_active(&upid).await? {
result["status"] = Value::from("running"); result["status"] = Value::from("running");
} else { } else {
let exitstatus = crate::server::upid_read_status(&upid).unwrap_or(TaskState::Unknown); let (_, exitstatus) = crate::server::upid_read_status(&upid).unwrap_or((0, TaskState::Unknown));
result["status"] = Value::from("stopped"); result["status"] = Value::from("stopped");
result["exitstatus"] = Value::from(exitstatus.to_string()); result["exitstatus"] = Value::from(exitstatus.to_string());
}; };

View File

@ -190,9 +190,12 @@ pub fn create_task_log_dirs() -> Result<(), Error> {
Ok(()) Ok(())
} }
/// Read exits status from task log file /// Read endtime (time of last log line) and exitstatus from task log file
pub fn upid_read_status(upid: &UPID) -> Result<TaskState, Error> { /// If there is not a single line with at valid datetime, we assume the
/// starttime to be the endtime
pub fn upid_read_status(upid: &UPID) -> Result<(i64, TaskState), Error> {
let mut status = TaskState::Unknown; let mut status = TaskState::Unknown;
let mut time = upid.starttime;
let path = upid.log_path(); let path = upid.log_path();
@ -208,9 +211,15 @@ pub fn upid_read_status(upid: &UPID) -> Result<TaskState, Error> {
for line in reader.lines() { for line in reader.lines() {
let line = line?; let line = line?;
let mut iter = line.splitn(2, ": TASK "); let mut iter = line.splitn(2, ": ");
if iter.next() == None { continue; } if let Some(time_str) = iter.next() {
match iter.next() { time = chrono::DateTime::parse_from_rfc3339(time_str)
.map_err(|err| format_err!("cannot parse '{}': {}", time_str, err))?
.timestamp();
} else {
continue;
}
match iter.next().and_then(|rest| rest.strip_prefix("TASK ")) {
None => continue, None => continue,
Some(rest) => { Some(rest) => {
if let Ok(state) = rest.parse() { if let Ok(state) = rest.parse() {
@ -220,7 +229,7 @@ pub fn upid_read_status(upid: &UPID) -> Result<TaskState, Error> {
} }
} }
Ok(status) Ok((time, status))
} }
/// Task State /// Task State
@ -325,10 +334,10 @@ fn update_active_workers(new_upid: Option<&UPID>) -> Result<Vec<TaskListInfo>, E
}, },
None => { None => {
println!("Detected stopped UPID {}", upid_str); println!("Detected stopped UPID {}", upid_str);
let status = upid_read_status(&upid) let (time, status) = upid_read_status(&upid)
.unwrap_or_else(|_| TaskState::Unknown); .unwrap_or_else(|_| (Local::now().timestamp(), TaskState::Unknown));
finish_list.push(TaskListInfo { finish_list.push(TaskListInfo {
upid, upid_str, state: Some((Local::now().timestamp(), status)) upid, upid_str, state: Some((time, status))
}); });
}, },
Some((endtime, status)) => { Some((endtime, status)) => {