From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox-backup v2 5/5] file-restore: add 'timeout' and 'json-error' parameter
Date: Tue, 26 Apr 2022 12:13:58 +0200 [thread overview]
Message-ID: <20220426101401.3216096-6-d.csapak@proxmox.com> (raw)
In-Reply-To: <20220426101401.3216096-1-d.csapak@proxmox.com>
timeout limits the code with the given timeout in seconds, and
'json-error' return json to stdout when the call returns an error like
this:
{
"msg": "error message",
"error": true,
"code": <HTTP_STATUS_CODE>, // if it was an http error
}
with both options set, a client can more easily determine if the call
ran into a timeout (since it will return a 503 error), and can poll
it again
both is done behind new parameters, so that we can stay backwards-compatible
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
proxmox-file-restore/src/main.rs | 62 ++++++++++++++++++++++++++++++--
1 file changed, 59 insertions(+), 3 deletions(-)
diff --git a/proxmox-file-restore/src/main.rs b/proxmox-file-restore/src/main.rs
index 4b327f6b..6b5e65b9 100644
--- a/proxmox-file-restore/src/main.rs
+++ b/proxmox-file-restore/src/main.rs
@@ -11,6 +11,7 @@ use proxmox_router::cli::{
get_output_format, run_cli_command, CliCommand, CliCommandMap, CliEnvironment, ColumnConfig,
OUTPUT_FORMAT,
};
+use proxmox_router::{http_err, HttpError};
use proxmox_schema::api;
use proxmox_sys::fs::{create_path, CreateOptions};
use pxar::accessor::aio::Accessor;
@@ -211,6 +212,18 @@ async fn list_files(
schema: OUTPUT_FORMAT,
optional: true,
},
+ "json-error": {
+ type: Boolean,
+ description: "If set, errors are returned as json instead of writing to stderr",
+ optional: true,
+ default: false,
+ },
+ "timeout": {
+ type: Integer,
+ description: "Defines the maximum time the call can should take.",
+ minimum: 1,
+ optional: true,
+ },
}
},
returns: {
@@ -222,7 +235,14 @@ async fn list_files(
}
)]
/// List a directory from a backup snapshot.
-async fn list(snapshot: String, path: String, base64: bool, param: Value) -> Result<(), Error> {
+async fn list(
+ snapshot: String,
+ path: String,
+ base64: bool,
+ json_error: bool,
+ timeout: Option<u64>,
+ param: Value,
+) -> Result<(), Error> {
let repo = extract_repository_from_value(¶m)?;
let snapshot: BackupDir = snapshot.parse()?;
let path = parse_path(path, base64)?;
@@ -246,7 +266,43 @@ async fn list(snapshot: String, path: String, base64: bool, param: Value) -> Res
None => None,
};
- let result = list_files(repo, snapshot, path, crypt_config, keyfile, driver).await?;
+ let result = if let Some(timeout) = timeout {
+ match tokio::time::timeout(
+ std::time::Duration::from_secs(timeout),
+ list_files(repo, snapshot, path, crypt_config, keyfile, driver),
+ )
+ .await
+ {
+ Ok(res) => res,
+ Err(_) => Err(http_err!(SERVICE_UNAVAILABLE, "list not finished in time")),
+ }
+ } else {
+ list_files(repo, snapshot, path, crypt_config, keyfile, driver).await
+ };
+
+ let output_format = get_output_format(¶m);
+
+ if let Err(err) = result {
+ if !json_error {
+ return Err(err);
+ }
+ let (msg, code) = match err.downcast_ref::<HttpError>() {
+ Some(HttpError { code, message }) => (message.clone(), Some(code)),
+ None => (err.to_string(), None),
+ };
+ let mut json_err = json!({
+ "error": true,
+ "message": msg,
+ });
+ if let Some(code) = code {
+ json_err["code"] = Value::from(code.as_u16());
+ }
+ match output_format.as_ref() {
+ "json-pretty" => println!("{}", serde_json::to_string_pretty(&json_err)?),
+ _ => println!("{}", serde_json::to_string(&json_err)?),
+ }
+ return Ok(());
+ }
let options = default_table_format_options()
.sortby("type", false)
@@ -258,7 +314,7 @@ async fn list(snapshot: String, path: String, base64: bool, param: Value) -> Res
let output_format = get_output_format(¶m);
format_and_print_result_full(
- &mut json!(result),
+ &mut json!(result.unwrap()),
&API_METHOD_LIST.returns,
&output_format,
&options,
--
2.30.2
next prev parent reply other threads:[~2022-04-26 10:14 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-26 10:13 [pve-devel] [PATCH proxmox-backup/common/storage/wt v2] improve file-restore timeout behaviour Dominik Csapak
2022-04-26 10:13 ` [pve-devel] [PATCH proxmox-backup v2 1/5] restore-daemon: start disk initialization in parallel to the api Dominik Csapak
2022-04-26 10:13 ` [pve-devel] [PATCH proxmox-backup v2 2/5] restore-daemon: put blocking code into 'block_in_place' Dominik Csapak
2022-04-26 10:13 ` [pve-devel] [PATCH proxmox-backup v2 3/5] restore-daemon: avoid auto-mounting zpools Dominik Csapak
2022-04-26 10:13 ` [pve-devel] [PATCH proxmox-backup v2 4/5] file-restore: factor out 'list_files' Dominik Csapak
2022-04-26 10:13 ` Dominik Csapak [this message]
2022-04-26 10:13 ` [pve-devel] [PATCH common v2 1/1] PBSClient: file_restore_list: add json-error and timeout parameter Dominik Csapak
2022-04-26 10:14 ` [pve-devel] [PATCH storage v2 1/1] api: FileRestore: decode and return proper error with new file-restore params Dominik Csapak
2022-04-26 10:14 ` [pve-devel] [PATCH widget-toolkit v2 1/1] window/FileBrowser: try reload again when getting a 503 error Dominik Csapak
2022-04-27 17:43 ` [pve-devel] partiall-applied: [PATCH proxmox-backup/common/storage/wt v2] improve file-restore timeout behaviour Thomas Lamprecht
2022-04-27 17:43 ` [pbs-devel] partiall-applied: [pve-devel] " Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220426101401.3216096-6-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal