Hi Stevieb!

As a suggestion, your code that mentions "500" could be simplified.

for (1 .. API_ERRORS) { # If a timeout (ie. code 500) occurs, or # some kind of other error, repeat the API call $response = $self->mech->request($request); if ($response->is_success) { # sucessful processing code here... return $response_data; } # all non-success code cause pause, then re-try # elsif ($response->code == 500) { # next; #} sleep(1); }
Some sites will give a 404 or whatever and then a repeat of the same request will succeed. I don't see the need to differentiate between 500 and some other kind of error, at least not in the code that you show. For some sites, I have found that a brief pause "sleep (1)" helps. Sometimes in very, very rare situations, mechanize just gets "stuck" and no amount of retries will fix it.

Update: I looked back at some code from ~8 years ago:
I like your code better, the question should be how to simulate anything other than success rather than just a 500 error code.

my $m2= WWW::Mechanize->new(); # New Mechanize Object for detailed res +ults # may help save memory?? TBD... # update (it did) my $success=0; my $tries=0; while (! $success and $tries++ < 10) { eval { $m2->get($fullurl); }; if (! $@) { $success = 1; } else { print STDERR "Error: Retry Attempt $tries of 10\n"; print LOG "Error: Retry Attempt $tries of 10\n"; sleep (3); } } die "aborted Web Site Error: $!" unless $success; #ultimate failure!! +PROGRAM ABORT !!!!
This probably is not typical, but in my app, a website error happens about once per 2,000 requests. One retry is almost always sufficient. I have never seen a successful attempt on the 3rd retry. "ultimate failure" happens way less than once in a million website accesses, but it does happen. I have no explanation for that. This is a "chron" job and it will run again once per hour and pick up where it left off. Anyway there is more than just "time out" or "success". Other error codes can and do come back and more likely than not, a simple retry will fix them.

In reply to Re: Forcing WWW::Mechanize to timeout by Marshall
in thread Forcing WWW::Mechanize to timeout by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.