> -----Original Message----- > From: Chas. Owens [mailto:[EMAIL PROTECTED] > Sent: Monday, March 24, 2008 11:42 > To: Wagner, David --- Senior Programmer Analyst --- WGO > Cc: Perl Beginners > Subject: Re: Looking for example of how to keep an FTP > processing running if the Ip Address is down or unavailable > > On Mon, Mar 24, 2008 at 2:25 PM, Wagner, David --- Senior Programmer > Analyst --- WGO <[EMAIL PROTECTED]> wrote: > > I have a polling process that runs 24x7 ( internal site ) > and for the > > most part has no problems except that they shutdown the > the internal > > site every three or four weeks and my process then dies. > > > > I have a simple setup: > > > > $MyFtp = Net::FTP->new($GlblInfo{ipaddr}, Debug => 1); > > > > $MyFtp->login($GlblInfo{logon},$GlblInfo{pw}); > > > > Yes. There is no checking on the new and what > happens is that > > new fails and then when it tries the login, I get a failure for the > > method login. So how do I surround the new so that if it > fails, I can > > go to sleep for say five minutes and then try again until > I hit some > > limit or timeframe? > snip > > Throw the login into an eval {} to prevent the die from killing the > script and use a loop for max tries: > > use constant MAX_TRIES => 3; > use constant WAIT_BEFORE_RETRY => 60*5; > > my $try = 1; > until (eval { $MyFtp->login($GlblInfo{logon},$GlblInfo{pw}; 1}) { > die "login to $GlblInfo{ipaddr} as $GlblInfo{logon} failed (try > $try): $@"; if ++$try == MAX_TRIES; > warn "login to $GlblInfo{ipaddr} as $GlblInfo{logon} > failed (try $try): $@"; > sleep WAIT_BEFORE_RETRY; > } > > -- > Chas. Owens > wonkden.net > The most important skill a programmer can have is the ability to read. > Chas, I tried the following text script setup and am using a good and bad ip addr. When it is a good Ip Addr, it connects and then quits. When I try the bad ip addr, it just waits a moment and gets out. Obviously I have something wrong, but it is not apparent from the output given to me. You can replace the good.ip.addr.com with a valid ip addr to connect to.
Nothing prints out except debug prints of 'trying ipaddr' and if $MyFtp is defined, then prints that out otherwise nothing on a failure. None of the other commands within the sleep, ertc. Wags ;) ========================================== #!perl use strict; use warnings; use Net::FTP; use constant MAX_TRIES => 3; use constant WAIT_BEFORE_RETRY => 5*5; my @MIA = qw(good.ip.addr.com bad.ip.addr.com); my $MyIpAddr = [EMAIL PROTECTED]; my $MyWrkIpAddr = q[]; my @TI = (); my $TimeInfo = [EMAIL PROTECTED]; my $diff = 0; get_time( $TimeInfo ); my $MyWrkIdx; my $try; my $MyFtp; while ( 1 ) { set_indx($MyWrkIdx); $MyWrkIpAddr = $MyIpAddr->[$MyWrkIdx]; $try = 1; printf "*1*Trying IpAddr: $MyWrkIpAddr\n"; until (eval { $MyFtp = Net::FTP->new($MyWrkIpAddr, Debug => 1); 1} ) { die "Connecting to $MyWrkIpAddr failed (try $try): $@"; if (++$try == MAX_TRIES ) { warn "Connecting to $MyWrkIpAddr failed (try $try): $@"; sleep WAIT_BEFORE_RETRY; set_indx($MyWrkIdx); $MyWrkIpAddr = $MyIpAddr->[$MyWrkIdx]; } } printf "Connected to $MyWrkIpAddr successfully.\n" if ( defined $MyFtp ); last; } # ## ### ## # sub set_indx { my ($MyWrkIdx) = @_; get_time($TimeInfo); if ( $TimeInfo->[0] % 2 ) { $MyWrkIdx = 1; } else { $MyWrkIdx = 0; } $_[0] = $MyWrkIdx; 1; } sub get_time { my ( $TimeInfo, $MyUseTime ) = @_; $diff = 86400 * $diff; # 0 1 2 3 4 5 6 7 8 # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - $diff); # 9 => YearModulo # 10 => Time used in calculations # my $MyPointInTime = time - $diff; $MyPointInTime = $MyUseTime if ( defined $MyUseTime ); @{$TimeInfo} = localtime( $MyPointInTime ); $TimeInfo->[4]++; $TimeInfo->[9] = $TimeInfo->[5] % 100; # Year Modulo, last two digits of year $TimeInfo->[10] = $MyPointInTime; $diff = 0; 1; } # end of get_time # ## ### ## # ********************************************************************** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. ********************************************************************** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/