| 1 | package AnyEvent::Retry::Coro; | ||
| 2 | BEGIN { | ||
| 3 | $AnyEvent::Retry::Coro::VERSION = '0.03'; | ||
| 4 | } | ||
| 5 | # ABSTRACT: AnyEvent::Retry for jobs that run in separate threads | ||
| 6 | use Moose; | ||
| 7 | use Coro; | ||
| 8 | use Scalar::Util qw(weaken); | ||
| 9 | use Try::Tiny; | ||
| 10 | |||
| 11 | use true; | ||
| 12 | use namespace::autoclean; | ||
| 13 | |||
| 14 | extends 'AnyEvent::Retry'; | ||
| 15 | |||
| 16 | has '+on_failure' => ( | ||
| 17 | init_arg => undef, | ||
| 18 | required => 0, | ||
| 19 | writer => 'set_failure_cb', | ||
| 20 | ); | ||
| 21 | |||
| 22 | has '+on_success' => ( | ||
| 23 | init_arg => undef, | ||
| 24 | required => 0, | ||
| 25 | writer => 'set_success_cb', | ||
| 26 | ); | ||
| 27 | |||
| 28 | has 'running_coro' => ( | ||
| 29 | init_arg => undef, | ||
| 30 | accessor => 'running_coro', | ||
| 31 | clearer => 'clear_running_coro', | ||
| 32 | ); | ||
| 33 | |||
| 34 | before start => sub { | ||
| 35 | my $self = shift; | ||
| 36 | Scalar::Util::weaken($self); | ||
| 37 | my $cb = Coro::rouse_cb; | ||
| 38 | $self->set_failure_cb( sub { $cb->( error => @_ ) } ); | ||
| 39 | $self->set_success_cb( sub { $cb->( success => @_ ) } ); | ||
| 40 | }; | ||
| 41 | |||
| 42 | override run_code => sub { | ||
| 43 | my $self = shift; | ||
| 44 | my @result = try { | ||
| 45 | my $result = $self->try->(); | ||
| 46 | return (($result ? 1 : 0), 'success', $result); | ||
| 47 | } | ||
| 48 | catch { | ||
| 49 | warn $_; | ||
| 50 | return (0, 'error', $_); | ||
| 51 | } | ||
| 52 | }; | ||
| 53 | |||
| 54 | override handle_tick => sub { | ||
| 55 | my ($self, $i) = @_; | ||
| 56 | weaken $self; | ||
| 57 | $self->running_coro(async { | ||
| 58 | $self->handle_result($self->run_code); | ||
| 59 | $self->clear_running_coro if defined $self; | ||
| 60 | }); | ||
| 61 | }; | ||
| 62 | |||
| 63 | sub DEMOLISH { | ||
| 64 | my $self = shift; | ||
| 65 | $self->running_coro->throw('DEMOLISH'); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub wait { | ||
| 69 | my ($status, @args) = Coro::rouse_wait(); | ||
| 70 | return $args[0] if $status eq 'success'; | ||
| 71 | die $args[1]; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub run { | ||
| 75 | my $self = shift; | ||
| 76 | $self->start; | ||
| 77 | |||
| 78 | # this is so DEMOLISH still works right | ||
| 79 | my $class = $self->meta->name; | ||
| 80 | undef $self; | ||
| 81 | $class->wait; | ||
| 82 | } | ||
| 83 | |||
| 84 | __PACKAGE__->meta->make_immutable; | ||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | =pod | ||
| 89 | |||
| 90 | =head1 NAME | ||
| 91 | |||
| 92 | AnyEvent::Retry::Coro - AnyEvent::Retry for jobs that run in separate threads | ||
| 93 | |||
| 94 | =head1 VERSION | ||
| 95 | |||
| 96 | version 0.03 | ||
| 97 | |||
| 98 | =head1 SYNOPSIS | ||
| 99 | |||
| 100 | use Coro; | ||
| 101 | |||
| 102 | my $r = AnyEvent::Retry::Coro->new( | ||
| 103 | max_tries => 100, # eventually give up | ||
| 104 | interval => { Constant => { interval => 1 } }, # try every second | ||
| 105 | try => { | ||
| 106 | die 'out of cake!' if $cake-- < 0; | ||
| 107 | return do_science(); | ||
| 108 | }, | ||
| 109 | |||
| 110 | ); | ||
| 111 | |||
| 112 | my $neat_gun = $r->run; # keep on trying until you run out of cake | ||
| 113 | |||
| 114 | =head1 DESCRIPTION | ||
| 115 | |||
| 116 | This module makes L<AnyEvent::Retry> work nicely with L<Coro>. You | ||
| 117 | don't need to provide success or failure callbacks anymore, and your | ||
| 118 | task to retry just needs C<die> or return a result. | ||
| 119 | |||
| 120 | =head1 METHODS | ||
| 121 | |||
| 122 | =head2 run | ||
| 123 | |||
| 124 | This runs the task, blocking the thread until a result is available. | ||
| 125 | If your task encounters an error, this will die. If it's sucessful, | ||
| 126 | it returns the result. | ||
| 127 | |||
| 128 | =head2 wait | ||
| 129 | |||
| 130 | Allows you to run the task without blocking: | ||
| 131 | |||
| 132 | $r->start; | ||
| 133 | ...; # do anything | ||
| 134 | my $result = $r->wait; # block here | ||
| 135 | |||
| 136 | C<run> is implemented exactly like the above. | ||
| 137 | |||
| 138 | =head1 AUTHOR | ||
| 139 | |||
| 140 | Jonathan Rockway <jrockway@cpan.org> | ||
| 141 | |||
| 142 | =head1 COPYRIGHT AND LICENSE | ||
| 143 | |||
| 144 | This software is copyright (c) 2010 by Jonathan Rockway. | ||
| 145 | |||
| 146 | This is free software; you can redistribute it and/or modify it under | ||
| 147 | the same terms as the Perl 5 programming language system itself. | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | |||
| 152 | __END__ |