AnyEvent-Retry

 view release on metacpan or  search on metacpan

lib/AnyEvent/Retry.pm  view on Meta::CPAN

BEGIN {
  $AnyEvent::Retry::VERSION = '0.03';
}
# ABSTRACT: try something until it works
use Moose;
use MooseX::Types::Common::Numeric qw(PositiveNum);
use AnyEvent::Retry::Types qw(Interval);

use AnyEvent;
use Try::Tiny;
use Scalar::Util qw(weaken);

use true;
use namespace::autoclean;

has 'after' => (
    is      => 'ro',
    isa     => PositiveNum,
    default => 0,
);

lib/AnyEvent/Retry.pm  view on Meta::CPAN

        $self->_sent_result(1);
        $self->on_failure->(demolish => 'DEMOLISH');
    }
}

# set a timer to call handle_tick in the future
sub set_timer {
    my ($self, $time, $i) = @_;
    return $self->handle_tick($i) if $time <= 0;

    weaken $self;
    $self->_set_timer(
        AnyEvent->timer( after => $time, cb => sub {
            $self->kill_timer;
            $self->handle_tick($i);
        }),
    );

    return;
}

lib/AnyEvent/Retry.pm  view on Meta::CPAN

    # $max_tries, so set the timer and do the whole thing again
    $self->set_timer( $next_time, $next_i );
    return;
}

# start the user's code running, with a continuation-passing-style
# callback to call when the result is ready
sub run_code {
    my ($self) = @_;

    # we weaken $self here so that if the user does "undef $retry", we
    # DEMOLISH the object and silently discard the results of the
    # running code.  feel free to subclass if want to keep the class
    # alive arbitrarily.
    weaken $self;

    my $success = sub {
        my $result = shift;
        return unless defined $self;
        $self->handle_result(($result ? 1 : 0), 'success', $result);
        return;
    };

    my $error = sub {
        my $msg = shift;

lib/AnyEvent/Retry/Coro.pm  view on Meta::CPAN

package AnyEvent::Retry::Coro;
BEGIN {
  $AnyEvent::Retry::Coro::VERSION = '0.03';
}
# ABSTRACT: AnyEvent::Retry for jobs that run in separate threads
use Moose;
use Coro;
use Scalar::Util qw(weaken);
use Try::Tiny;

use true;
use namespace::autoclean;

extends 'AnyEvent::Retry';

has '+on_failure' => (
    init_arg => undef,
    required => 0,

lib/AnyEvent/Retry/Coro.pm  view on Meta::CPAN

);

has 'running_coro' => (
    init_arg => undef,
    accessor => 'running_coro',
    clearer  => 'clear_running_coro',
);

before start => sub {
    my $self = shift;
    Scalar::Util::weaken($self);
    my $cb = Coro::rouse_cb;
    $self->set_failure_cb( sub { $cb->( error   => @_ ) } );
    $self->set_success_cb( sub { $cb->( success => @_ ) } );
};

override run_code => sub {
    my $self = shift;
    my @result = try {
        my $result = $self->try->();
        return (($result ? 1 : 0), 'success', $result);
    }
    catch {
        warn $_;
        return (0, 'error', $_);
    }
};

override handle_tick => sub {
    my ($self, $i) = @_;
    weaken $self;
    $self->running_coro(async {
        $self->handle_result($self->run_code);
        $self->clear_running_coro if defined $self;
    });
};

sub DEMOLISH {
    my $self = shift;
    $self->running_coro->throw('DEMOLISH');
}



( run in 1.003 second using v1.01-cache-2.11-cpan-65fba6d93b7 )