From ae197dda23b8fac07dfcfc56307936d96c11aaec Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Thu, 13 Aug 2020 10:29:14 +0200 Subject: [PATCH] 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 --- src/api2/node/tasks.rs | 2 +- src/server/worker_task.rs | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/api2/node/tasks.rs b/src/api2/node/tasks.rs index 0bb043d9..1e9643ec 100644 --- a/src/api2/node/tasks.rs +++ b/src/api2/node/tasks.rs @@ -105,7 +105,7 @@ async fn get_task_status( if crate::server::worker_is_active(&upid).await? { result["status"] = Value::from("running"); } 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["exitstatus"] = Value::from(exitstatus.to_string()); }; diff --git a/src/server/worker_task.rs b/src/server/worker_task.rs index 62cceb3d..da1a877e 100644 --- a/src/server/worker_task.rs +++ b/src/server/worker_task.rs @@ -190,9 +190,12 @@ pub fn create_task_log_dirs() -> Result<(), Error> { Ok(()) } -/// Read exits status from task log file -pub fn upid_read_status(upid: &UPID) -> Result { +/// Read endtime (time of last log line) and exitstatus from task log file +/// 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 time = upid.starttime; let path = upid.log_path(); @@ -208,9 +211,15 @@ pub fn upid_read_status(upid: &UPID) -> Result { for line in reader.lines() { let line = line?; - let mut iter = line.splitn(2, ": TASK "); - if iter.next() == None { continue; } - match iter.next() { + let mut iter = line.splitn(2, ": "); + if let Some(time_str) = 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, Some(rest) => { if let Ok(state) = rest.parse() { @@ -220,7 +229,7 @@ pub fn upid_read_status(upid: &UPID) -> Result { } } - Ok(status) + Ok((time, status)) } /// Task State @@ -325,10 +334,10 @@ fn update_active_workers(new_upid: Option<&UPID>) -> Result, E }, None => { println!("Detected stopped UPID {}", upid_str); - let status = upid_read_status(&upid) - .unwrap_or_else(|_| TaskState::Unknown); + let (time, status) = upid_read_status(&upid) + .unwrap_or_else(|_| (Local::now().timestamp(), TaskState::Unknown)); finish_list.push(TaskListInfo { - upid, upid_str, state: Some((Local::now().timestamp(), status)) + upid, upid_str, state: Some((time, status)) }); }, Some((endtime, status)) => {