1 package Tak::CommandService;
3 use Capture::Tiny qw(capture);
4 use IPC::System::Simple qw(runx EXIT_ANY);
9 with 'Tak::Role::Service';
12 my ($self, $command) = @_;
14 my ($stdout, $stderr) = capture {
15 $code = runx(EXIT_ANY, @$command);
17 return { stdout => $stdout, stderr => $stderr, exit_code => $code };
20 sub start_stream_exec_request {
21 my ($self, $req, $command) = @_;
23 my $pid = open3(my $in, my $out, $err, @$command)
24 or return $req->failure("Couldn't spawn process: $!");
27 Tak->loop->unwatch_io(handle => $_, on_read_ready => 1)
30 $req->success({ exit_code => $? });
36 on_read_ready => sub {
37 if (sysread($out, $outbuf, 1024, length($outbuf)) > 0) {
38 $req->progress(stdout => $1) while $outbuf =~ s/^(.*)\n//;
40 $req->progress(stdout => $outbuf) if $outbuf;
41 $req->progress(stderr => $errbuf) if $errbuf;
48 on_read_ready => sub {
49 if (sysread($err, $errbuf, 1024, length($errbuf)) > 0) {
50 $req->progress(stderr => $1) while $errbuf =~ s/^(.*)\n//;