Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompressLog: Add zstd compression #1404

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/lib/Hydra/View/NixLog.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,55 @@ use warnings;
use base qw/Catalyst::View/;
use Hydra::Helper::CatalystUtils;

sub tail {
my ($filehandle, $n) = @_;
my @lines;
my $line_count = 0;

while (my $line = <$filehandle>) {
$lines[$line_count % $n] = $line;
$line_count++;
}

my $start = $line_count > $n ? $line_count % $n : 0;
my $end = $line_count > $n ? $n : $line_count;

my $result = "";
for my $i (0 .. $end - 1) {
$result .= $lines[($start + $i) % $n];
}
return $result;
}

sub process {
my ($self, $c) = @_;

my $logPath = $c->stash->{logPath};

$c->response->content_type('text/plain; charset=utf-8');

my $fh = IO::Handle->new();
my $logFh = IO::Handle->new();

my $tail = int($c->stash->{tail} // "0");
my $tailLines = int($c->stash->{tail} // "0");

if ($logPath =~ /\.zst$/) {
my $doTail = $tail ? "| tail -n '$tail'" : "";
open($fh, "-|", "zstd -dc < '$logPath' $doTail") or die;
open($logFh, "-|", "zstd", "-dc", $logPath) or die;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SuperSandro2000 sorry, I got nerd snipped, when seeing shells being spawned with external inputs. Could you test this variant?

} elsif ($logPath =~ /\.bz2$/) {
my $doTail = $tail ? "| tail -n '$tail'" : "";
open($fh, "-|", "bzip2 -dc < '$logPath' $doTail") or die;
open($logFh, "-|", "bzip2", "-dc", $logPath) or die;
} else {
if ($tail) {
open($fh, "-|", "tail -n '$tail' '$logPath'") or die;
} else {
open($fh, "<", $logPath) or die;
}
open($logFh, "<", $logPath) or die;
}
binmode($fh);

setCacheHeaders($c, 365 * 24 * 60 * 60) if $c->stash->{finished};

$c->response->body($fh);
if ($tailLines > 0) {
my $logEnd = tail($logFh, $tailLines);
$c->response->body($logEnd);
return 1;
}

binmode($logFh);
$c->response->body($logFh);

return 1;
}
Expand Down
Loading