AnyEvent-Subprocess

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

examples/backtick.pl
examples/synopsis.pl
lib/AnyEvent/Subprocess.pm
lib/AnyEvent/Subprocess/DefaultDelegates.pm
lib/AnyEvent/Subprocess/Delegate.pm
lib/AnyEvent/Subprocess/Done.pm
lib/AnyEvent/Subprocess/Done/Delegate.pm
lib/AnyEvent/Subprocess/Done/Delegate/CaptureHandle.pm
lib/AnyEvent/Subprocess/Done/Delegate/Handle.pm
lib/AnyEvent/Subprocess/Done/Delegate/State.pm
lib/AnyEvent/Subprocess/Done/Delegate/Timeout.pm
lib/AnyEvent/Subprocess/Easy.pm
lib/AnyEvent/Subprocess/Handle.pm
lib/AnyEvent/Subprocess/Job.pm
lib/AnyEvent/Subprocess/Job/Delegate.pm
lib/AnyEvent/Subprocess/Job/Delegate/Callback.pm
lib/AnyEvent/Subprocess/Job/Delegate/CaptureHandle.pm
lib/AnyEvent/Subprocess/Job/Delegate/CompletionCondvar.pm
lib/AnyEvent/Subprocess/Job/Delegate/Handle.pm
lib/AnyEvent/Subprocess/Job/Delegate/MonitorHandle.pm
lib/AnyEvent/Subprocess/Job/Delegate/PrintError.pm
lib/AnyEvent/Subprocess/Job/Delegate/Pty.pm
lib/AnyEvent/Subprocess/Job/Delegate/Timeout.pm
lib/AnyEvent/Subprocess/Role/WithDelegates.pm
lib/AnyEvent/Subprocess/Role/WithDelegates/Manager.pm
lib/AnyEvent/Subprocess/Running.pm
lib/AnyEvent/Subprocess/Running/Delegate.pm
lib/AnyEvent/Subprocess/Running/Delegate/Callback.pm
lib/AnyEvent/Subprocess/Running/Delegate/CaptureHandle.pm
lib/AnyEvent/Subprocess/Running/Delegate/CompletionCondvar.pm
lib/AnyEvent/Subprocess/Running/Delegate/Handle.pm
lib/AnyEvent/Subprocess/Running/Delegate/MonitorHandle.pm
lib/AnyEvent/Subprocess/Running/Delegate/Timeout.pm
lib/AnyEvent/Subprocess/Types.pm
t/00-info.t
t/basic.t
t/capture-basic.t
t/clone.t
t/death-by-stdin.t
t/easy-qx.t
t/exit-code.t
t/external-process.t
t/handle-leftovers.t

lib/AnyEvent/Subprocess/DefaultDelegates.pm  view on Meta::CPAN


use AnyEvent::Subprocess::Role::WithDelegates::Manager qw(register_delegate);

use AnyEvent::Subprocess::Job::Delegate::Callback;
use AnyEvent::Subprocess::Job::Delegate::CaptureHandle;
use AnyEvent::Subprocess::Job::Delegate::CompletionCondvar;
use AnyEvent::Subprocess::Job::Delegate::Handle;
use AnyEvent::Subprocess::Job::Delegate::MonitorHandle;
use AnyEvent::Subprocess::Job::Delegate::PrintError;
use AnyEvent::Subprocess::Job::Delegate::Pty;
use AnyEvent::Subprocess::Job::Delegate::Timeout;

register_delegate( 'Handle' => 'AnyEvent::Subprocess::Job::Delegate::Handle' );

register_delegate( 'StandardHandles' => sub {
    my $args = shift || {};
    my $prefix = $args->{prefix} || '';
    my $class  = $args->{class}  || 'AnyEvent::Subprocess::Job::Delegate::Handle';

    return (
        $class->new(

lib/AnyEvent/Subprocess/DefaultDelegates.pm  view on Meta::CPAN

    return AnyEvent::Subprocess::Job::Delegate::MonitorHandle->new(%$args);
});

register_delegate('PrintError' => sub {
    my $args = shift || {};
    $args->{name} ||= 'error_printer';

    return AnyEvent::Subprocess::Job::Delegate::PrintError->new(%$args);
});

register_delegate( 'Timeout', sub {
   my $args = shift;
    return AnyEvent::Subprocess::Job::Delegate::Timeout->new(
        name       => $args->{name} || 'timeout',
        time_limit => $args->{time_limit} || $args->{timeout},
        kill_with  => $args->{kill_with} || 9,
    );
});

1;



lib/AnyEvent/Subprocess/DefaultDelegates.pm  view on Meta::CPAN


Calls a list of coderefs whenever a line is read from a handle.

=head2 PrintError

Delegate that calls a callback in the child to print the exception (if
any) the child throws.

Use WithResult if you want to actually get the exception in the parent.

=head1 Timeout

Kill the subprocess with a signal C<kill_with> after C<timeout>
seconds elapse.  See L<AnyEvent::Subprocess::Job::Delegate::Timeout>.

=head1 SEE ALSO

See the test suite to see all of these shortcuts in use.

=head1 AUTHOR

Jonathan Rockway <jrockway@cpan.org>

=head1 COPYRIGHT AND LICENSE

lib/AnyEvent/Subprocess/Done/Delegate/Timeout.pm  view on Meta::CPAN

package AnyEvent::Subprocess::Done::Delegate::Timeout;
BEGIN {
  $AnyEvent::Subprocess::Done::Delegate::Timeout::VERSION = '1.102912';
}
# ABSTRACT: done delegate for a job that can time out
use Moose;
use namespace::autoclean;

with 'AnyEvent::Subprocess::Done::Delegate';

has 'timed_out' => (
    is       => 'ro',
    isa      => 'Bool',

lib/AnyEvent/Subprocess/Done/Delegate/Timeout.pm  view on Meta::CPAN

__PACKAGE__->meta->make_immutable;

1;



=pod

=head1 NAME

AnyEvent::Subprocess::Done::Delegate::Timeout - done delegate for a job that can time out

=head1 VERSION

version 1.102912

=head1 ATTRIBUTES

=head2 timed_out

True if the job was killed because it ran out of time.

lib/AnyEvent/Subprocess/Job/Delegate/Timeout.pm  view on Meta::CPAN

package AnyEvent::Subprocess::Job::Delegate::Timeout;
BEGIN {
  $AnyEvent::Subprocess::Job::Delegate::Timeout::VERSION = '1.102912';
}
# ABSTRACT: Kill a subprocess if it takes too long
use Moose;
use namespace::autoclean;

use AnyEvent;
use AnyEvent::Subprocess::Running::Delegate::Timeout;
use MooseX::Types::Signal qw(Signal);

with 'AnyEvent::Subprocess::Job::Delegate';

has 'time_limit' => (
    is       => 'ro',
    isa      => 'Int',
    required => 1,
);

lib/AnyEvent/Subprocess/Job/Delegate/Timeout.pm  view on Meta::CPAN

    default  => 'SIGKILL',
);

has 'child' => (
    init_arg => undef,
    accessor => 'child',
);

sub build_run_delegates {
    my $self = shift;
    my $run; $run = AnyEvent::Subprocess::Running::Delegate::Timeout->new(
        name  => $self->name,
        timer => AnyEvent->timer( after => $self->time_limit, interval => 0, cb => sub {
            $run->killed_by_timer(1);
            $self->child->kill( $self->kill_with );
            $run->clear_timer;
        }),
    );

    return $run;
}

lib/AnyEvent/Subprocess/Job/Delegate/Timeout.pm  view on Meta::CPAN

__PACKAGE__->meta->make_immutable;

1;



=pod

=head1 NAME

AnyEvent::Subprocess::Job::Delegate::Timeout - Kill a subprocess if it takes too long

=head1 VERSION

version 1.102912

=head1 SYNOPSIS

    my $timed = AnyEvent::Subprocess::Job::Delegate::Timeout->new(
        name       => 'timeout',
        time_limit => 10,     # 10 seconds
        kill_with  => 'FIRE', # may not be available on your OS
    );

    my $job = AnyEvent::Subprocess->new( delegates => [$timed], code => ... );
    my $run = $job->run;

Later...

lib/AnyEvent/Subprocess/Running/Delegate/Timeout.pm  view on Meta::CPAN

package AnyEvent::Subprocess::Running::Delegate::Timeout;
BEGIN {
  $AnyEvent::Subprocess::Running::Delegate::Timeout::VERSION = '1.102912';
}
# ABSTRACT: Running part of Timeout delegate
use Moose;
use namespace::autoclean;
use AnyEvent::Subprocess::Done::Delegate::Timeout;

with 'AnyEvent::Subprocess::Running::Delegate';

has 'timer' => (
    is       => 'ro',
    clearer  => 'clear_timer',
);

has 'killed_by_timer' => (
    init_arg => undef,

lib/AnyEvent/Subprocess/Running/Delegate/Timeout.pm  view on Meta::CPAN

    default  => sub { undef },
);

sub completion_hook {
    my $self = shift;
    $self->clear_timer;
}

sub build_done_delegates {
    my $self = shift;
    return AnyEvent::Subprocess::Done::Delegate::Timeout->new(
        name      => $self->name,
        timed_out => $self->killed_by_timer,
    );
}

sub build_events {}
sub build_code_args {}

__PACKAGE__->meta->make_immutable;

1;

__END__
=pod

=head1 NAME

AnyEvent::Subprocess::Running::Delegate::Timeout - Running part of Timeout delegate

=head1 VERSION

version 1.102912

=head1 AUTHOR

Jonathan Rockway <jrockway@cpan.org>

=head1 COPYRIGHT AND LICENSE

t/timeout.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;

use AnyEvent::Subprocess;

{
    my $proc = AnyEvent::Subprocess->new(
        delegates => [{ Timeout => { timeout => 1 } }, 'CompletionCondvar'],
        code      => sub { sleep 10 },
    );

    my $run = $proc->run;
    my $run_timer = $run->delegate('timeout');
    ok $run_timer->timer, 'has timer';
    ok !$run_timer->killed_by_timer, 'not killed by timer yet';

    my $done = $run->delegate('completion_condvar')->condvar->recv;
    ok $done->delegate('timeout')->timed_out, 'timed out';
    ok !$done->is_success, 'was not a success';
}

{
    my $proc = AnyEvent::Subprocess->new(
        delegates => [{ Timeout => { timeout => 3 } }, 'CompletionCondvar'],
        code      => sub { sleep 1 },
    );

    my $run = $proc->run;
    my $run_timer = $run->delegate('timeout');
    ok $run_timer->timer, 'has timer';
    ok !$run_timer->killed_by_timer, 'not killed by timer yet';

    my $done = $run->delegate('completion_condvar')->condvar->recv;
    ok !$done->delegate('timeout')->timed_out, 'did not time out';



( run in 0.307 second using v1.01-cache-2.11-cpan-4d50c553e7e )