| 1 | package Tak::CommandService; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Capture::Tiny qw(capture); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use IPC::System::Simple qw(runx EXIT_ANY); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use IPC::Open3; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Symbol qw(gensym); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | with 'Tak::Role::Service'; | |||||||||
| 10 | |||||||||||
| 11 | sub handle_exec { | ||||||||||
| 12 | 0 | ▶ | my ($self, $command) = @_; | ||||||||
|
Subroutine
handle_exec: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 13 | 0 | my $code; | |||||||||
| 14 | my ($stdout, $stderr) = capture { | ||||||||||
| 15 | 0 | ▶ | $code = runx(EXIT_ANY, @$command); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 16 | 0 | }; | |||||||||
| 17 | 0 | return { stdout => $stdout, stderr => $stderr, exit_code => $code }; | |||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | sub start_stream_exec_request { | ||||||||||
| 21 | 0 | ▶ | my ($self, $req, $command) = @_; | ||||||||
|
Subroutine
start_stream_exec_request: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 22 | 0 | my $err = gensym; | |||||||||
| 23 | 0 | ▶ | my $pid = open3(my $in, my $out, $err, @$command) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | or return $req->failure("Couldn't spawn process: $!"); | ||||||||||
| 25 | 0 | close($in); # bye | |||||||||
| 26 | my $done = sub { | ||||||||||
| 27 | Tak->loop->unwatch_io(handle => $_, on_read_ready => 1) | ||||||||||
| 28 | 0 | ▶ | for ($out, $err); | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 29 | 0 | waitpid($pid, 0); | |||||||||
| 30 | 0 | $req->success({ exit_code => $? }); | |||||||||
| 31 | 0 | }; | |||||||||
| 32 | 0 | my $outbuf = ''; | |||||||||
| 33 | 0 | my $errbuf = ''; | |||||||||
| 34 | Tak->loop->watch_io( | ||||||||||
| 35 | handle => $out, | ||||||||||
| 36 | on_read_ready => sub { | ||||||||||
| 37 | 0 | ▶ | if (sysread($out, $outbuf, 1024, length($outbuf)) > 0) { | ||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
| |||||||||||
| 38 | 0 | ▶ | $req->progress(stdout => $1) while $outbuf =~ s/^(.*)\n//; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | } else { | ||||||||||
| 40 | 0 | ▶ | $req->progress(stdout => $outbuf) if $outbuf; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | ▶ | $req->progress(stderr => $errbuf) if $errbuf; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | $done->(); | |||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | 0 | ); | |||||||||
| 46 | Tak->loop->watch_io( | ||||||||||
| 47 | handle => $err, | ||||||||||
| 48 | on_read_ready => sub { | ||||||||||
| 49 | 0 | ▶ | if (sysread($err, $errbuf, 1024, length($errbuf)) > 0) { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||
| 50 | 0 | ▶ | $req->progress(stderr => $1) while $errbuf =~ s/^(.*)\n//; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | 0 | ); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 1 | 1; | |||||||||