]> git.proxmox.com Git - proxmox-backup.git/commitdiff
server/worker_task: let upid_read_status also return the endtime
authorDominik Csapak <d.csapak@proxmox.com>
Thu, 13 Aug 2020 08:29:14 +0000 (10:29 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 13 Aug 2020 09:35:44 +0000 (11:35 +0200)
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>
src/api2/node/tasks.rs
src/server/worker_task.rs

index 0bb043d965e4cc4cddb2a0d12b1249dedca9620d..1e9643ecf76aa266c6e98d064cc2fac489644d1b 100644 (file)
@@ -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());
     };
index 62cceb3dfaaf8fa18a25f2b69a7af7df945bd1b4..da1a877ea6791d2dc711c0000a4c5b00990a7cf4 100644 (file)
@@ -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<TaskState, Error> {
+/// 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<TaskState, Error> {
     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<TaskState, Error> {
         }
     }
 
-    Ok(status)
+    Ok((time, status))
 }
 
 /// Task State
@@ -325,10 +334,10 @@ fn update_active_workers(new_upid: Option<&UPID>) -> Result<Vec<TaskListInfo>, 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)) => {