vitess.io/vitess@v0.16.2/tools/wget-retry (about)

     1  #!/usr/bin/env perl
     2  
     3  # AUTHORITY
     4  # DATE
     5  # DIST
     6  # VERSION
     7  
     8  use strict;
     9  use warnings;
    10  
    11  use Getopt::Long;
    12  
    13  my %Opts = (
    14      tries => 0,
    15      waitretry => 10,
    16      exit_statuses => [
    17          split /\s*,\s*/,
    18          (defined $ENV{WGET_RETRY_EXIT_STATUSES} ?
    19               $ENV{WGET_RETRY_EXIT_STATUSES} : "1,3,4,5,6,7,8")],
    20  );
    21  
    22  my @ORIG_ARGV = @ARGV;
    23  Getopt::Long::Configure(
    24      'bundling', 'pass_through', 'no_auto_abbrev', 'permute');
    25  GetOptions(
    26      'help|h|?' => sub {
    27          print <<'_';
    28  Usage: wget-retry [options] <url>...
    29  
    30  Options:
    31    --help, -h, -?   Show this message and exit.
    32    --version        Show program version and exit.
    33  
    34  All the other options will be passed to wget.
    35  
    36  See manpage for more detailed documentation.
    37  _
    38          exit 0;
    39      },
    40      'version' => sub {
    41          no warnings 'once';
    42          print "wget-retry version ", ($main::VERSION || "dev"),
    43              ($main::DATE ? " ($main::DATE)" : ""), "\n";
    44          exit 0;
    45      },
    46  
    47      'tries|t=i' => \$Opts{tries},
    48      'waitretry=i' => \$Opts{waitretry},
    49  );
    50  
    51  my $wget_cmd = $ENV{WGET_RETRY_WGET_CMD} || "wget";
    52  
    53  my $retries = 0;
    54  while (1) {
    55      system {$wget_cmd} $wget_cmd, @ORIG_ARGV;
    56      last unless $?;
    57      my $exit_code = $? >> 8;
    58      if (grep { $exit_code == $_ } @{ $Opts{exit_statuses} }) {
    59          $retries++;
    60          if ($Opts{tries} == 0 || $retries <= $Opts{tries}) {
    61              warn "wget-retry: $wget_cmd exit-code is $exit_code, retrying ($retries) after $Opts{waitretry} second(s) ...\n";
    62              sleep $Opts{waitretry};
    63              next;
    64          } else {
    65              warn "wget-retry: $wget_cmd exit-code is $exit_code, won't retry anymore, exiting\n";
    66              exit $exit_code;
    67          }
    68      } else {
    69          exit $exit_code;
    70      }
    71  }
    72  
    73  # ABSTRACT: Wget wrapper to retry harder
    74  # PODNAME:
    75  
    76  =head1 SYNOPSIS
    77  
    78  Use like you would use B<wget>:
    79  
    80   % wget-retry -c -t0 https://example.com/url1 ...
    81  
    82  
    83  =head1 DESCRIPTION
    84  
    85  By default, B<wget> doesn't retry harder; only upon disconnection in the middle
    86  of downloading (with C<-t>/C<--tries>, e.g. C<-t 0>) and on connection refused
    87  (with C<--retry-connrefused>) but not on other network failures, e.g. DNS
    88  resolution failure (which can happen sometimes).
    89  
    90  This wrapper runs B<wget> then checks its exit code. If exit code indicates
    91  network failure (4) it will re-run wget.
    92  
    93  The number of tries is unlimited, or from the C<-t> (<--tries>) option. The
    94  number of seconds to wait before each try is 10 seconds or from the
    95  C<--waitretry> option.
    96  
    97  
    98  =head1 OPTIONS
    99  
   100  =head2 --help
   101  
   102  Shortcuts: -h, -?.
   103  
   104  =head2 --version
   105  
   106  
   107  =head1 ENVIRONMENT
   108  
   109  =head2 WGET_RETRY_EXIT_STATUSES
   110  
   111  A comma-separated list of exit statuses to retry. For example, C<1,3,4,5,6,7,8>
   112  means generic error (1), file I/O error (3), network failure (4), SSL
   113  verification failure (5), username/password authentication failure (6), protocol
   114  errors (7), as well as error response from server (8) will make wget-retry rerun
   115  wget. The default is 1,3,4,5,6,7,8. For more details on wget exit statuses, see
   116  the wget's manpage.
   117  
   118  =head2 WGET_RETRY_WGET_CMD
   119  
   120  String. Wget command to use. Defaults to C<wget>. Can be used to chain several
   121  wrappers together.
   122  
   123  
   124  =head1 SEE ALSO
   125  
   126  B<wget>.