public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Daniel Tschlatscher <d.tschlatscher@proxmox.com>
To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com,
	pmg-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH manager v3] make task log downloadable in the PVE manager backend
Date: Tue, 11 Oct 2022 15:59:02 +0200	[thread overview]
Message-ID: <20221011135902.610515-4-d.tschlatscher@proxmox.com> (raw)
In-Reply-To: <20221011135902.610515-1-d.tschlatscher@proxmox.com>

The read tasklog API call now streams the log file if the limit
parameter is set to 0. If not, it should behave the same as before.
This is done in preparation for the task log download button to be
added in the TaskViewer.

Signed-off-by: Daniel Tschlatscher <d.tschlatscher@proxmox.com>
---

changes from v2
* replaced helper call with inline implementation
* changed compression cutoff to 1024
* replaced stat($filename)→size call with "-s"

 PVE/API2/Tasks.pm | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/Tasks.pm b/PVE/API2/Tasks.pm
index 9cd1e56b..3bc00704 100644
--- a/PVE/API2/Tasks.pm
+++ b/PVE/API2/Tasks.pm
@@ -349,7 +349,7 @@ __PACKAGE__->register_method({
 		minimum => 0,
 		default => 50,
 		optional => 1,
-		description => "The maximum amount of lines that should be printed.",
+		description => "The maximum amount of lines that should be printed. A limit of 0 streams the file directly, otherwise lines are json encoded.",
 	    },
 	},
     },
@@ -387,6 +387,29 @@ __PACKAGE__->register_method({
 	    $rpcenv->check($user, "/nodes/$node", [ 'Sys.Audit' ]);
 	}
 
+	if ($limit == 0) {
+	    # 1024 is a practical cutoff for the size distribution of our log files.
+	    my $use_compression = ( -s $filename ) > 1024;
+
+	    my $fh;
+	    if ($use_compression) {
+		open($fh, "-|", "/usr/bin/gzip", "-c", "$filename")
+		    or die "Could not create compressed file stream for $filename - $!";
+	    } else {
+		open($fh, '<', $filename) or die "Could not open file $filename - $!";
+	    }
+
+	    return {
+		download => {
+		    fh => $fh,
+		    stream => 1,
+		    'content-encoding' => $use_compression ? 'gzip' : undef,
+		    'content-type' => "text/plain",
+		    'content-disposition' => "attachment; filename=\"".$param->{upid}."\"",
+		},
+	    },
+	}
+
 	my ($count, $lines) = PVE::Tools::dump_logfile($filename, $start, $limit);
 
 	$rpcenv->set_result_attrib('total', $count);
-- 
2.30.2





      parent reply	other threads:[~2022-10-11 14:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-11 13:58 [pve-devel] [PATCH widget-manager/backup/pmg v3] fix: #3971 Tasklog download button Daniel Tschlatscher
2022-10-11 13:59 ` [pve-devel] [PATCH backup v3] make tasklog downloadable in the backup server backend Daniel Tschlatscher
2022-10-11 13:59 ` [pve-devel] [PATCH pmg-api v3] make tasklog downloadable in the PMG backend Daniel Tschlatscher
2022-10-11 13:59 ` Daniel Tschlatscher [this message]

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=20221011135902.610515-4-d.tschlatscher@proxmox.com \
    --to=d.tschlatscher@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    --cc=pmg-devel@lists.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 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