| 1 | # | ||||||||||||||
| 2 | # This file is part of Action-Retry | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # This software is copyright (c) 2013 by Damien "dams" Krotkine. | ||||||||||||||
| 5 | # | ||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 8 | # | ||||||||||||||
| 9 | package Action::Retry; | ||||||||||||||
| 10 | { | ||||||||||||||
| 11 | 6 | $Action::Retry::VERSION = '0.24'; | |||||||||||||
| 12 | } | ||||||||||||||
| 13 | |||||||||||||||
| 14 | # ABSTRACT: Module to try to perform an action, with various ways of retrying and sleeping between retries. | ||||||||||||||
| 15 | |||||||||||||||
| 16 | 6 | ▶ | use Module::Runtime qw(use_module); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 6 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 6 | ▶ | use Time::HiRes qw(usleep gettimeofday); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 6 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | |||||||||||||||
| 21 | 6 | ▶ | use base 'Exporter'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 6 | our @EXPORT_OK = qw(retry); | |||||||||||||
| 23 | # export by default if run from command line | ||||||||||||||
| 24 | 6 | ▶ | our @EXPORT = ((caller())[1] eq '-e' ? @EXPORT_OK : ()); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | 6 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | |||||||||||||||
| 29 | |||||||||||||||
| 30 | has attempt_code => ( | ||||||||||||||
| 31 | is => 'ro', | ||||||||||||||
| 32 | required => 1, | ||||||||||||||
| 33 | 12 | ▶ | isa => sub { ref $_[0] eq 'CODE' }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 34 | 6 | ); | |||||||||||||
| 35 | |||||||||||||||
| 36 | |||||||||||||||
| 37 | has retry_if_code => ( | ||||||||||||||
| 38 | is => 'ro', | ||||||||||||||
| 39 | required => 1, | ||||||||||||||
| 40 | 12 | ▶ | isa => sub { ref $_[0] eq 'CODE' }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | 10 | ▶ | default => sub { sub { $_[0] }; }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | 6 | ); | |||||||||||||
| 43 | |||||||||||||||
| 44 | |||||||||||||||
| 45 | has on_failure_code => ( | ||||||||||||||
| 46 | is => 'ro', | ||||||||||||||
| 47 | 0 | ▶ | isa => sub { ref $_[0] eq 'CODE' }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 48 | 6 | predicate => 1, | |||||||||||||
| 49 | ); | ||||||||||||||
| 50 | |||||||||||||||
| 51 | |||||||||||||||
| 52 | has strategy => ( | ||||||||||||||
| 53 | is => 'ro', | ||||||||||||||
| 54 | 0 | ▶ | default => sub { 'Constant' }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 55 | 12 | ▶ | coerce => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||
| 56 | 12 | my $attr = $_[0]; | |||||||||||||
| 57 | 12 | ▶ | blessed($attr) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | and return $attr; | ||||||||||||||
| 59 | 12 | my $class_name = $attr; | |||||||||||||
| 60 | 12 | my $constructor_params = {}; | |||||||||||||
| 61 | 12 | ▶ | if (ref $attr eq 'HASH') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 12 | $class_name = (keys %$attr)[0]; | |||||||||||||
| 63 | 12 | $constructor_params = $attr->{$class_name}; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | 12 | ▶ | $class_name = $class_name =~ /^\+(.+)$/ ? $1 : "Action::Retry::Strategy::$class_name"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 12 | return use_module($class_name)->new($constructor_params); | |||||||||||||
| 67 | }, | ||||||||||||||
| 68 | 12 | ▶ | isa => sub { $_[0]->does('Action::Retry::Strategy') or croak 'Should consume the Action::Retry::Strategy role' }, | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||
| 69 | 6 | ); | |||||||||||||
| 70 | |||||||||||||||
| 71 | |||||||||||||||
| 72 | has non_blocking => ( | ||||||||||||||
| 73 | is => 'ro', | ||||||||||||||
| 74 | 11 | ▶ | default => sub { 0 }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 75 | 6 | ); | |||||||||||||
| 76 | |||||||||||||||
| 77 | # For non blocking mode, store the timestamp after which we can retry | ||||||||||||||
| 78 | has _needs_sleeping_until => ( | ||||||||||||||
| 79 | is => 'rw', | ||||||||||||||
| 80 | 12 | ▶ | default => sub { 0 }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 81 | 6 | init_arg => undef, | |||||||||||||
| 82 | ); | ||||||||||||||
| 83 | |||||||||||||||
| 84 | |||||||||||||||
| 85 | 15 | ▶ | sub run { | ||||||||||||
|
Subroutine
run: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||
| 86 | 15 | my $self = shift; | |||||||||||||
| 87 | |||||||||||||||
| 88 | 15 | while(1) { | |||||||||||||
| 89 | |||||||||||||||
| 90 | 111 | ▶ | if (my $timestamp = $self->_needs_sleeping_until) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | # we can't retry until we have waited enough time | ||||||||||||||
| 92 | 5 | my ($seconds, $microseconds) = gettimeofday; | |||||||||||||
| 93 | 5 | ▶ | $seconds * 1000 + int($microseconds / 1000) >= $timestamp | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | or return; | ||||||||||||||
| 95 | 1 | $self->_needs_sleeping_until(0); | |||||||||||||
| 96 | 1 | $self->strategy->next_step; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 107 | my $error; | |||||||||||||
| 100 | my @attempt_result; | ||||||||||||||
| 101 | 107 | my $attempt_result; | |||||||||||||
| 102 | 107 | my $wantarray; | |||||||||||||
| 103 | |||||||||||||||
| 104 | 107 | ▶ | if (wantarray) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 105 | 5 | $wantarray = 1; | |||||||||||||
| 106 | 5 | @attempt_result = eval { $self->attempt_code->(@_) }; | |||||||||||||
| 107 | 5 | $error = $@; | |||||||||||||
| 108 | } elsif ( ! defined wantarray ) { | ||||||||||||||
| 109 | 97 | eval { $self->attempt_code->(@_) }; | |||||||||||||
| 110 | 97 | $error = $@; | |||||||||||||
| 111 | } else { | ||||||||||||||
| 112 | 5 | $attempt_result = eval { $self->attempt_code->(@_) }; | |||||||||||||
| 113 | 5 | $error = $@; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 107 | ▶ | my $h = { action_retry => $self, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | attempt_result => ( $wantarray ? \@attempt_result : $attempt_result ), | ||||||||||||||
| 118 | attempt_parameters => \@_, | ||||||||||||||
| 119 | }; | ||||||||||||||
| 120 | |||||||||||||||
| 121 | |||||||||||||||
| 122 | 107 | ▶ | $self->retry_if_code->($error, $h ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 123 | or $self->strategy->reset, $@ = $error, return ( $wantarray ? @attempt_result : $attempt_result ); | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 105 | ▶ | if (! $self->strategy->needs_to_retry) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 9 | $self->strategy->reset; | |||||||||||||
| 127 | 9 | ▶ | $self->has_on_failure_code | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | and return $self->on_failure_code->($error, $h); | ||||||||||||||
| 129 | 9 | return; | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | 96 | ▶ | if ($self->non_blocking) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 2 | my ($seconds, $microseconds) = gettimeofday; | |||||||||||||
| 134 | 2 | $self->_needs_sleeping_until($seconds * 1000 + int($microseconds / 1000) + $self->strategy->compute_sleep_time); | |||||||||||||
| 135 | } else { | ||||||||||||||
| 136 | 94 | usleep($self->strategy->compute_sleep_time * 1000); | |||||||||||||
| 137 | 94 | $self->strategy->next_step; | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | } | ||||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | |||||||||||||||
| 143 | 4 | ▶ | sub retry (&;@) { | ||||||||||||
|
Subroutine
retry: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 144 | 4 | my $code = shift; | |||||||||||||
| 145 | 4 | ▶ | @_ % 2 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | and croak "arguments to retry must be a CodeRef, and an even number of key / values"; | ||||||||||||||
| 147 | 4 | my %args = @_; | |||||||||||||
| 148 | 4 | Action::Retry->new( attempt_code => $code, %args )->run(); | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | |||||||||||||||
| 152 | 6 | 1; | |||||||||||||
| 153 | |||||||||||||||
| 154 | __END__ | ||||||||||||||