Recently the WorldPay payment system has been down, and I was wondering if I could check if WorldPay was up (using Perl) and if it is forward them to WorldPay. This is simple.
But if the server is not working, I don't want it to hang for 30 seconds or a minute with the customer waiting. Is it possible to request a timeout value?
#!/usr/bin/perl
use LWP::Simple qw(get);
if ( get("WORLD PAY URL GOES HERE") ) {
print 'ok';
} else {
print 'error';
}
But that way you perl script will hang and wait till WorldPay will respond. Timeout feature seems to be disabled in that module (at least for windows). I've tried it before with no luck.
use Net::Ping;
use Time::HiRes qw( gettimeofday tv_interval);
$p = Net::Ping->new("tcp", 10);
$p->{port_num} = getservbyname($ref->{'PORT'}, "tcp");
$t0 = [gettimeofday];
$serverstate = "DOWN" unless $p->ping($ref->{'HOST'});
$pingtime = tv_interval($t0) unless ($serverstate eq "DOWN");
$p->close;
undef($p);
It's not the prettiest thing in the world, but it works reasonably well.
The docs for Net::Ping should get you on your way. For what you're doing, you might not bother with the Time::HiRes stuff.
#!/usr/bin/perl
use Net::Telnet;
$connection=Net::Telnet->new(Timeout => 5, Host => "www.example.com", Port=>80, Errmode => sub {&error;});
sub error {
print "Connection Failed!\n";
}