public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox-backup v2] fix #4343: updated `view_task_result` to bail on task failure
@ 2023-08-24 12:45 Gabriel Goller
  2023-09-07 16:11 ` Thomas Lamprecht
  0 siblings, 1 reply; 3+ messages in thread
From: Gabriel Goller @ 2023-08-24 12:45 UTC (permalink / raw)
  To: pbs-devel

Now we make an additional request on `api2/json/.../tasks/{upid}/status` to
get the `exitstatus` of the task. This allows us to `bail` and thus
get a non-zero exit code in the cli.

Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
---

changes v1:
 - check on each iteration if the task is finished, then bubble up the 
   original error of the task. This was inpractical though, so we print
   the error and return a generic error.

 pbs-client/src/task_log.rs | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/pbs-client/src/task_log.rs b/pbs-client/src/task_log.rs
index 0bbb928d..a406a7a4 100644
--- a/pbs-client/src/task_log.rs
+++ b/pbs-client/src/task_log.rs
@@ -76,9 +76,8 @@ pub async fn display_task_log(
                 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}");
+                if strip_date {
+                    println!("{}", try_strip_date(t));
                 } else {
                     println!("{t}");
                 }
@@ -96,6 +95,14 @@ pub async fn display_task_log(
             }
         }
 
+        let status_path = format!("api2/json/nodes/localhost/tasks/{upid_encoded}/status");
+        let status_result = client.get(&status_path, None).await?;
+        if status_result["data"]["status"].as_str() == Some("stopped")
+            && status_result["data"]["exitstatus"].as_str() != Some("OK")
+        {
+            bail!("task failed");
+        }
+
         Ok(())
     };
 
@@ -127,3 +134,11 @@ pub async fn view_task_result(
 
     Ok(())
 }
+
+fn try_strip_date(log_msg: &str) -> &str {
+    if log_msg.len() > 27 && &log_msg[25..27] == ": " {
+        &log_msg[27..]
+    } else {
+        log_msg
+    }
+}
-- 
2.39.2





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [pbs-devel] [PATCH proxmox-backup v2] fix #4343: updated `view_task_result` to bail on task failure
  2023-08-24 12:45 [pbs-devel] [PATCH proxmox-backup v2] fix #4343: updated `view_task_result` to bail on task failure Gabriel Goller
@ 2023-09-07 16:11 ` Thomas Lamprecht
  2023-09-08 13:30   ` Gabriel Goller
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Lamprecht @ 2023-09-07 16:11 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Gabriel Goller

On 24/08/2023 14:45, Gabriel Goller wrote:
> Now we make an additional request on `api2/json/.../tasks/{upid}/status` to
> get the `exitstatus` of the task. This allows us to `bail` and thus
> get a non-zero exit code in the cli.
> 

seems OK now, but.

> Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
> ---
> 
> changes v1:
>  - check on each iteration if the task is finished, then bubble up the 
>    original error of the task. This was inpractical though, so we print
>    the error and return a generic error.
> 
>  pbs-client/src/task_log.rs | 21 ++++++++++++++++++---
>  1 file changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/pbs-client/src/task_log.rs b/pbs-client/src/task_log.rs
> index 0bbb928d..a406a7a4 100644
> --- a/pbs-client/src/task_log.rs
> +++ b/pbs-client/src/task_log.rs
> @@ -76,9 +76,8 @@ pub async fn display_task_log(
>                  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}");
> +                if strip_date {
> +                    println!("{}", try_strip_date(t));

I don't see how adding the new helper below and using it here is relevant to
what you want to accomplish as of your commit message?

Please but such unrelated clean-ups in a separate patch.

>                  } else {
>                      println!("{t}");
>                  }
> @@ -96,6 +95,14 @@ pub async fn display_task_log(
>              }
>          }
>  
> +        let status_path = format!("api2/json/nodes/localhost/tasks/{upid_encoded}/status");
> +        let status_result = client.get(&status_path, None).await?;
> +        if status_result["data"]["status"].as_str() == Some("stopped")
> +            && status_result["data"]["exitstatus"].as_str() != Some("OK")
> +        {
> +            bail!("task failed");
> +        }
> +
>          Ok(())
>      };
>  
> @@ -127,3 +134,11 @@ pub async fn view_task_result(
>  
>      Ok(())
>  }
> +
> +fn try_strip_date(log_msg: &str) -> &str {
> +    if log_msg.len() > 27 && &log_msg[25..27] == ": " {
> +        &log_msg[27..]
> +    } else {
> +        log_msg
> +    }
> +}





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [pbs-devel] [PATCH proxmox-backup v2] fix #4343: updated `view_task_result` to bail on task failure
  2023-09-07 16:11 ` Thomas Lamprecht
@ 2023-09-08 13:30   ` Gabriel Goller
  0 siblings, 0 replies; 3+ messages in thread
From: Gabriel Goller @ 2023-09-08 13:30 UTC (permalink / raw)
  To: Thomas Lamprecht, Proxmox Backup Server development discussion

On 9/7/23 18:11, Thomas Lamprecht wrote:

> On 24/08/2023 14:45, Gabriel Goller wrote:
>> Now we make an additional request on `api2/json/.../tasks/{upid}/status` to
>> get the `exitstatus` of the task. This allows us to `bail` and thus
>> get a non-zero exit code in the cli.
>>
> seems OK now, but.
>
>> Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
>> ---
>>
>> changes v1:
>>   - check on each iteration if the task is finished, then bubble up the
>>     original error of the task. This was inpractical though, so we print
>>     the error and return a generic error.
>>
>>   pbs-client/src/task_log.rs | 21 ++++++++++++++++++---
>>   1 file changed, 18 insertions(+), 3 deletions(-)
>>
>> diff --git a/pbs-client/src/task_log.rs b/pbs-client/src/task_log.rs
>> index 0bbb928d..a406a7a4 100644
>> --- a/pbs-client/src/task_log.rs
>> +++ b/pbs-client/src/task_log.rs
>> @@ -76,9 +76,8 @@ pub async fn display_task_log(
>>                   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}");
>> +                if strip_date {
>> +                    println!("{}", try_strip_date(t));
> I don't see how adding the new helper below and using it here is relevant to
> what you want to accomplish as of your commit message?
>
> Please but such unrelated clean-ups in a separate patch.

Relics from the previous patch ;)
Removed it in the new series.

>>                   } else {
>>                       println!("{t}");
>>                   }
>> @@ -96,6 +95,14 @@ pub async fn display_task_log(
>>               }
>>           }
>>   
>> +        let status_path = format!("api2/json/nodes/localhost/tasks/{upid_encoded}/status");
>> +        let status_result = client.get(&status_path, None).await?;
>> +        if status_result["data"]["status"].as_str() == Some("stopped")
>> +            && status_result["data"]["exitstatus"].as_str() != Some("OK")
>> +        {
>> +            bail!("task failed");
>> +        }
>> +
>>           Ok(())
>>       };
>>   
>> @@ -127,3 +134,11 @@ pub async fn view_task_result(
>>   
>>       Ok(())
>>   }
>> +
>> +fn try_strip_date(log_msg: &str) -> &str {
>> +    if log_msg.len() > 27 && &log_msg[25..27] == ": " {
>> +        &log_msg[27..]
>> +    } else {
>> +        log_msg
>> +    }
>> +}




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-09-08 13:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-24 12:45 [pbs-devel] [PATCH proxmox-backup v2] fix #4343: updated `view_task_result` to bail on task failure Gabriel Goller
2023-09-07 16:11 ` Thomas Lamprecht
2023-09-08 13:30   ` Gabriel Goller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal