[pmg-devel] [PATCH pmg-api v3] make tasklog downloadable in the PMG backend

Daniel Tschlatscher d.tschlatscher at proxmox.com
Tue Oct 11 15:59:01 CEST 2022


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.

I saw an opportunity here to clear some redundant code for displaying
the tasklog and replaced it with a call to dump_logfile(), akin to how
this is handled in pve-manager.

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

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

 src/PMG/API2/Tasks.pm | 43 ++++++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 19 deletions(-)

diff --git a/src/PMG/API2/Tasks.pm b/src/PMG/API2/Tasks.pm
index 598fb4d..da53f48 100644
--- a/src/PMG/API2/Tasks.pm
+++ b/src/PMG/API2/Tasks.pm
@@ -243,6 +243,7 @@ __PACKAGE__->register_method({
 		type => 'integer',
 		minimum => 0,
 		optional => 1,
+		description => "The maximum amount of lines that should be printed. A limit of 0 streams the file directly, otherwise lines are json encoded.",
 	    },
 	},
     },
@@ -272,30 +273,34 @@ __PACKAGE__->register_method({
 
 	my $restenv = PMG::RESTEnvironment->get();
 
-	my $fh = IO::File->new($filename, "r");
-	raise_param_exc({ upid => "no such task - unable to open file - $!" }) if !$fh;
+	my $start = $param->{start} // 0;
+	my $limit = $param->{limit} // 50;
 
-	my $start = $param->{start} || 0;
-	my $limit = $param->{limit} || 50;
-	my $count = 0;
-	my $line;
-	while (defined ($line = <$fh>)) {
-	    next if $count++ < $start;
-	    next if $limit <= 0;
-	    chomp $line;
-	    push @$lines, { n => $count, t => $line};
-	    $limit--;
-	}
+	if ($limit == 0) {
+	    # 1024 is a practical cutoff for the size distribution of our log files.
+	    my $use_compression = ( -s $filename ) > 1024;
 
-	close($fh);
+	    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 - $!";
+	    }
 
-	# HACK: ExtJS store.guaranteeRange() does not like empty array
-	# so we add a line
-	if (!$count) {
-	    $count++;
-	    push @$lines, { n => $count, t => "no content"};
+	    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);
+
 	$restenv->set_result_attrib('total', $count);
 
 	return $lines;
-- 
2.30.2





More information about the pmg-devel mailing list