| 1 | package AnyEvent::Subprocess::Job::Delegate::Timeout; | ||
| 2 | 48 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 48 | $AnyEvent::Subprocess::Job::Delegate::Timeout::VERSION = '1.102912'; | |
| 4 | } | ||
| 5 | # ABSTRACT: Kill a subprocess if it takes too long | ||
| 6 | 48 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 48 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 48 | ▶ | use AnyEvent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 48 | ▶ | use AnyEvent::Subprocess::Running::Delegate::Timeout; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 48 | ▶ | use MooseX::Types::Signal qw(Signal); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 47 | with 'AnyEvent::Subprocess::Job::Delegate'; | |
| 14 | |||
| 15 | 47 | has 'time_limit' => ( | |
| 16 | is => 'ro', | ||
| 17 | isa => 'Int', | ||
| 18 | required => 1, | ||
| 19 | ); | ||
| 20 | |||
| 21 | 47 | has 'kill_with' => ( | |
| 22 | is => 'ro', | ||
| 23 | isa => Signal, | ||
| 24 | coerce => 1, | ||
| 25 | required => 1, | ||
| 26 | default => 'SIGKILL', | ||
| 27 | ); | ||
| 28 | |||
| 29 | 47 | has 'child' => ( | |
| 30 | init_arg => undef, | ||
| 31 | accessor => 'child', | ||
| 32 | ); | ||
| 33 | |||
| 34 | 2 | ▶ | sub build_run_delegates { |
|
Subroutine
build_run_delegates: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 35 | 2 | my $self = shift; | |
| 36 | 2 | my $run; $run = AnyEvent::Subprocess::Running::Delegate::Timeout->new( | |
| 37 | name => $self->name, | ||
| 38 | 2 | ▶ | timer => AnyEvent->timer( after => $self->time_limit, interval => 0, cb => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 39 | 2 | $run->killed_by_timer(1); | |
| 40 | 2 | $self->child->kill( $self->kill_with ); | |
| 41 | 2 | $run->clear_timer; | |
| 42 | 2 | }), | |
| 43 | ); | ||
| 44 | |||
| 45 | 2 | return $run; | |
| 46 | } | ||
| 47 | |||
| 48 | 2 | ▶ | sub parent_setup_hook { |
|
Subroutine
parent_setup_hook: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 49 | 2 | my ($self, $job, $run) = @_; | |
| 50 | 2 | $self->child($run); | |
| 51 | } | ||
| 52 | |||
| 53 | 0 | ▶ | sub build_code_args {} |
|
Subroutine
build_code_args: not called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 54 | 0 | ▶ | sub child_setup_hook {} |
|
Subroutine
child_setup_hook: not called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 55 | 0 | ▶ | sub child_finalize_hook {} |
|
Subroutine
child_finalize_hook: not called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 56 | 2 | ▶ | sub parent_finalize_hook {} |
|
Subroutine
parent_finalize_hook: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 57 | 0 | ▶ | sub receive_child_error {} |
|
Subroutine
receive_child_error: not called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 58 | 0 | ▶ | sub receive_child_result {} |
|
Subroutine
receive_child_result: not called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 59 | |||
| 60 | 47 | __PACKAGE__->meta->make_immutable; | |
| 61 | |||
| 62 | 47 | 1; | |
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | =pod | ||
| 67 | |||
| 68 | =head1 NAME | ||
| 69 | |||
| 70 | AnyEvent::Subprocess::Job::Delegate::Timeout - Kill a subprocess if it takes too long | ||
| 71 | |||
| 72 | =head1 VERSION | ||
| 73 | |||
| 74 | version 1.102912 | ||
| 75 | |||
| 76 | =head1 SYNOPSIS | ||
| 77 | |||
| 78 | my $timed = AnyEvent::Subprocess::Job::Delegate::Timeout->new( | ||
| 79 | name => 'timeout', | ||
| 80 | time_limit => 10, # 10 seconds | ||
| 81 | kill_with => 'FIRE', # may not be available on your OS | ||
| 82 | ); | ||
| 83 | |||
| 84 | my $job = AnyEvent::Subprocess->new( delegates => [$timed], code => ... ); | ||
| 85 | my $run = $job->run; | ||
| 86 | |||
| 87 | Later... | ||
| 88 | |||
| 89 | my $done = ...; | ||
| 90 | say 'your job took too long, so i killed it with fire' | ||
| 91 | if $done->delegate('tiemout')->timed_out; | ||
| 92 | |||
| 93 | =head1 ATTRIBUTES | ||
| 94 | |||
| 95 | =head2 time_limit | ||
| 96 | |||
| 97 | Number of seconds to allow the subprocess to run for. Required. | ||
| 98 | |||
| 99 | =head2 kill_with | ||
| 100 | |||
| 101 | UNIX signal to kill the subprocess with when its time expires. | ||
| 102 | Defaults to SIGKILL. | ||
| 103 | |||
| 104 | =head1 AUTHOR | ||
| 105 | |||
| 106 | Jonathan Rockway <jrockway@cpan.org> | ||
| 107 | |||
| 108 | =head1 COPYRIGHT AND LICENSE | ||
| 109 | |||
| 110 | This software is copyright (c) 2011 by Jonathan Rockway. | ||
| 111 | |||
| 112 | This is free software; you can redistribute it and/or modify it under | ||
| 113 | the same terms as the Perl 5 programming language system itself. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | |||
| 118 | __END__ | ||