| 1 | package Net::Amazon::MechanicalTurk::Command::AddRetry; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | our $VERSION = '1.00'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::Amazon::MechanicalTurk::Command::AddRetry - Adds retry support for MechanicalTurk API calls. | ||
| 11 | |||
| 12 | This module adds the addRetry method to the Net::Amazon::MechanicalTurk class. | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | # Tells the MechanicalTurk client to retry API calls for all operations, | ||
| 17 | # up to 5 times with 10 second interval delays, any time a ServiceUnavailable | ||
| 18 | # error occurs. | ||
| 19 | |||
| 20 | $mturk->addRetry( | ||
| 21 | operations => qr/./i, | ||
| 22 | errorCodes => qr/ServiceUnavailable/i, | ||
| 23 | maxTries => 5, | ||
| 24 | delay => 10 | ||
| 25 | ); | ||
| 26 | |||
| 27 | =head1 C<addRetry> | ||
| 28 | |||
| 29 | addRetry | ||
| 30 | |||
| 31 | Add retry for operations based on error codes. The following parameters | ||
| 32 | are required: | ||
| 33 | |||
| 34 | operations - A regular expression matching the operations the retry should be for. | ||
| 35 | errorCodes - A regular expression matching the errorCodes the retry should be for. | ||
| 36 | maxTries - The maximum number of times the operation will be retried, before | ||
| 37 | letting the error propogate. | ||
| 38 | delay - The number of seconds to wait between each retry. The number may be | ||
| 39 | fractional. | ||
| 40 | |||
| 41 | Note: using the qr// operator to pass regular expressions is the preferred method. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | sub addRetry { | ||
| 46 | my $mturk = shift; | ||
| 47 | my %params = @_; | ||
| 48 | foreach my $param (qw{ errorCodes operations delay maxTries }) { | ||
| 49 | if (!exists $params{$param}) { | ||
| 50 | Carp::croak("Missing required parameter $param."); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | if ($params{maxTries} < 1) { | ||
| 54 | Carp::croak("Invalid value for maxTries $params{maxTries}."); | ||
| 55 | } | ||
| 56 | $mturk->filterChain->addFilter(\&retryCallFilter, \%params); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub retryCallFilter { | ||
| 60 | my ($chain, $targetParams, $retryParams) = @_; | ||
| 61 | my ($mturk, $operation, $callParams) = @$targetParams; | ||
| 62 | |||
| 63 | if ($operation =~ $retryParams->{operations}) { | ||
| 64 | my $count = 0; | ||
| 65 | while (1) { | ||
| 66 | $count++; | ||
| 67 | my $result = eval { $chain->() }; | ||
| 68 | if ($@) { | ||
| 69 | if ($count >= $retryParams->{maxTries}) { | ||
| 70 | die $@; | ||
| 71 | } | ||
| 72 | my $error = ($mturk->response) ? $mturk->response->errorCode : ''; | ||
| 73 | if (defined($error) and $error =~ $retryParams->{errorCodes}) { | ||
| 74 | warn "Operation $operation failed with error $error.\n" . | ||
| 75 | "$@\n" . | ||
| 76 | "Will retry after " . $retryParams->{delay} . " seconds.\n"; | ||
| 77 | # Work-around to sleep for fractional seconds. | ||
| 78 | select(undef, undef, undef, $retryParams->{delay}); | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | die $@; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | return $result; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | return $chain->(); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | return 1; |