]> git.proxmox.com Git - pmg-api.git/commitdiff
api: journal: stream the journal data to the client
authorDominik Csapak <d.csapak@proxmox.com>
Wed, 24 Nov 2021 14:48:52 +0000 (15:48 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 24 Nov 2021 17:46:44 +0000 (18:46 +0100)
instead of accumulating the whole output of 'mini-journalreader' in
the api call (this can be quite big), use the download mechanic of the
http-server to stream the output to the client.

we lose some error handling possibilities, but we do not have
to allocate anything here, and since perl does not free memory after
allocating[0] this is our desired behaviour.

to keep api compatiblitiy, we need to give the journalreader the '-j'
flag to let it output json.

also tell the http server that the encoding is gzip and pipe
the output through it.

0: https://perldoc.perl.org/perlfaq3#How-can-I-free-an-array-or-hash-so-my-program-shrinks?

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
src/PMG/API2/Nodes.pm

index daa90c42cd11bbd640ea0a73c8891fcd2dd5fb9e..472a5f3a45e22d5224d73200049a00b8c027afe1 100644 (file)
@@ -334,19 +334,25 @@ __PACKAGE__->register_method({
     code => sub {
        my ($param) = @_;
 
-       my $cmd = ["/usr/bin/mini-journalreader"];
+       my $cmd = ["/usr/bin/mini-journalreader", "-j"];
        push @$cmd, '-n', $param->{lastentries} if $param->{lastentries};
        push @$cmd, '-b', $param->{since} if $param->{since};
        push @$cmd, '-e', $param->{until} if $param->{until};
-       push @$cmd, '-f', $param->{startcursor} if $param->{startcursor};
-       push @$cmd, '-t', $param->{endcursor} if $param->{endcursor};
+       push @$cmd, '-f', PVE::Tools::shellquote($param->{startcursor}) if $param->{startcursor};
+       push @$cmd, '-t', PVE::Tools::shellquote($param->{endcursor}) if $param->{endcursor};
+       push @$cmd, ' | gzip ';
 
-       my $lines = [];
-       my $parser = sub { push @$lines, shift };
+       open(my $fh, "-|", join(' ', @$cmd))
+           or die "could not start mini-journalreader";
 
-       PVE::Tools::run_command($cmd, outfunc => $parser);
-
-       return $lines;
+       return {
+           download => {
+               fh => $fh,
+               stream => 1,
+               'content-type' => 'application/json',
+               'content-encoding' => 'gzip',
+           },
+       },
     }});
 
 my $shell_cmd_map = {