on Wed, 01 May 2002 19:24:47 GMT, [EMAIL PROTECTED]
(Sterling Price) wrote:
> I have a situation where I'm performing an FTP to transfer some
> files to a less-than-reliable site. I have my FTP code inside an
> eval block so that the program can die and send a message to a
> pager if the FTP fails (code snippet shown below message). This
> works just fine, except that I am getting a lot of unnecessary
> messages to the pager because the site will sometimes not accept a
> connection, but then a few minutes later will be OK. So, instead
> of doing a die to send a page on the first attempt, I'd like to
> set it up so that the connection would be attempted a total of 3
> times before failing and sending a page, pausing a few minutes
> (say 5 minutes) between attempts. I can't quite wrap my mind
> around how to accomplish that though. I know I can use the sleep
> function to do the pause, but how should I restructure the eval
> block(s)?
You can do your own error handling, without using 'die' or 'eval'
which are, well, kinda permanent.
The following (untested) rudimentary code is how I would do this:
#! perl -w
use strict;
use Net::FTP;
use constant MAXATTEMPTS => 3;
use constant TIMETOWAIT => 5*60; # 5 minutes
my $server = '';
my $user = '';
my $password = '';
my $directory = '';
my @files = qw (a b c);
my $attempt = 0;
my $ferrcount = 0;
my $ftp;
my $log = '';
ATTEMPT:
while (++$attempt <= MAXATTEMPTS) {
# Try to get in...
unless ( $ftp = Net::FTP->new($server) and
$ftp->login($user, $password) and
$ftp->cwd($directory) and
$ftp->binary() ) {
$log .= logerr("Attempt $attempt to get in failed");
next ATTEMPT;
}
# OK, we are in, try to transfer files
$ferrcount = 0;
foreach my $file (@files) {
$ferrcount++ unless $ftp->put($file);
}
$ftp->quit();
last unless $ferrcount; # No problems encountered
$log .= logerr("$ferrcount files not transfered");
} continue {
$ftp->quit() if $ftp;
sleep( TIMETOWAIT );
}
if ($ferrcount or $attempt > MAXATTEMPTS) { # Something went wrong
print $log; # Here you would page
}
sub logerr {
my $msg = shift;
return scalar localtime() . " - $msg\n";
}
--
felix
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]