@@ -5,35 +5,55 @@ use warnings;
5
5
use base qw/ Catalyst::View/ ;
6
6
use Hydra::Helper::CatalystUtils;
7
7
8
+ sub tail {
9
+ my ($filehandle , $n ) = @_ ;
10
+ my @lines ;
11
+ my $line_count = 0;
12
+
13
+ while (my $line = <$filehandle >) {
14
+ $lines [$line_count % $n ] = $line ;
15
+ $line_count ++;
16
+ }
17
+
18
+ my $start = $line_count > $n ? $line_count % $n : 0;
19
+ my $end = $line_count > $n ? $n : $line_count ;
20
+
21
+ my $result = " " ;
22
+ for my $i (0 .. $end - 1) {
23
+ $result .= $lines [($start + $i ) % $n ];
24
+ }
25
+ return $result ;
26
+ }
27
+
8
28
sub process {
9
29
my ($self , $c ) = @_ ;
10
30
11
31
my $logPath = $c -> stash-> {logPath };
12
32
13
33
$c -> response-> content_type(' text/plain; charset=utf-8' );
14
34
15
- my $fh = IO::Handle-> new();
35
+ my $logFh = IO::Handle-> new();
16
36
17
- my $tail = int ($c -> stash-> {tail } // " 0" );
37
+ my $tailLines = int ($c -> stash-> {tail } // " 0" );
18
38
19
39
if ($logPath =~ / \. zst$ / ) {
20
- my $doTail = $tail ? " | tail -n '$tail '" : " " ;
21
- open ($fh , " -|" , " zstd -dc < '$logPath ' $doTail " ) or die ;
40
+ open ($logFh , " -|" , " zstd" , " -dc" , $logPath ) or die ;
22
41
} elsif ($logPath =~ / \. bz2$ / ) {
23
- my $doTail = $tail ? " | tail -n '$tail '" : " " ;
24
- open ($fh , " -|" , " bzip2 -dc < '$logPath ' $doTail " ) or die ;
42
+ open ($logFh , " -|" , " bzip2" , " -dc" , $logPath ) or die ;
25
43
} else {
26
- if ($tail ) {
27
- open ($fh , " -|" , " tail -n '$tail ' '$logPath '" ) or die ;
28
- } else {
29
- open ($fh , " <" , $logPath ) or die ;
30
- }
44
+ open ($logFh , " <" , $logPath ) or die ;
31
45
}
32
- binmode ($fh );
33
46
34
47
setCacheHeaders($c , 365 * 24 * 60 * 60) if $c -> stash-> {finished };
35
48
36
- $c -> response-> body($fh );
49
+ if ($tailLines > 0) {
50
+ my $logEnd = tail($logFh , $tailLines );
51
+ $c -> response-> body($logEnd );
52
+ return 1;
53
+ }
54
+
55
+ binmode ($logFh );
56
+ $c -> response-> body($logFh );
37
57
38
58
return 1;
39
59
}
0 commit comments