Gossamer Forum
Home : General : Perl Programming :

using alarm() to timeout

Quote Reply
using alarm() to timeout
I am working on a perl script right now and wondered if I could get some help. The script is below. This script uses LWP::Simple to check URLs it gets from a MySQL database. I would like for it to timeout and consider a URL not working if it can't be validated in 20 seconds or less. Does anyone know how to use perl's alarm() function to make this happen? I tried a variety of things with no success. Maybe LWP even has a built-in function for timeout that I don't know about.

Code:
#!/usr/bin/perl

use LWP::Simple;
use DBI;

$db_database = "info";
$db_uid = "root";
$db_pwd = "password";

$dbh = DBI->connect ("DBI:mysql:$db_database".$mysqlsock, $db_uid, $db_pwd) or die("could not connect to db\n");
$sth = $dbh->prepare("SELECT url FROM files");
$sth -> execute();
$numrows = $sth->rows;

for ($i = 0; $i = $numrows; ++$i) {
$url = ($sth->fetchrow_array);
if((head($url)) or (get($url))) {
$validity = "works";
} else {
$validity = "broke";
$valid_update = $dbh->do("UPDATE files SET valid = valid + 1 WHERE url = '$url'");
}
print "$validity - $url\n";
}
Thanks! Ryan McKillen
Quote Reply
Re: using alarm() to timeout In reply to
I have integrated codes written by Bmxer for the LWP Add Checker codes located at the following URL:

http://lookhard.hypermart.net/links-mods/

into my jump.cgi that sends users to an error page if the link is inaccessible and also notifies the admin about the problem link...cuts out having to use badlink.cgi or deadlink.cgi scripts.

Regards,

Eliot Lee

Quote Reply
Re: using alarm() to timeout In reply to
Thanks for the URL. Apparently LWP::UserAgent is better suited for this task than LWP::Simple. I do have a couple of concerns though. My script considers http://www.macentosh.fsnet.co.uk/jan.zip not working, but if you check the link from your browser, it obviously is. Secondly, it checks the links at a rate of about one per minute. My code is pasted below, can someone take a look?

Code:
#!/usr/bin/perl

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Headers;
use DBI;

$db_database = "info";
$db_uid = "root";
$db_pwd = "password";

$dbh = DBI->connect ("DBI:mysql:$db_database".$mysqlsock, $db_uid, $db_pwd) or die("could not connect to db\n");
$sth = $dbh->prepare("SELECT url FROM files");
$sth -> execute();
$numrows = $sth->rows;

print "\n";
for ($i = 0; $i = $numrows; ++$i) {
my ($ua,$req,$res);
$ua = LWP::UserAgent->new();
$ua->timeout(20);
$ua->agent("eeBOT");
$req = new HTTP::Request 'GET' => $url;
$res = $ua->request($req);
$mes = $ua->request($req);
$code = $res->code;
$message = $res->message;
$url = ($sth->fetchrow_array);
if (($res->is_success) or ($status eq "ok")) {
print "$url\nlink works\n\n";
} else {
print "$url\nlink sucks\n\n";
$valid_update = $dbh->do("UPDATE files SET valid = valid + 1 WHERE url = '$url'");
}
}
Thanks! Ryan McKillen

Quote Reply
Re: using alarm() to timeout In reply to
Actually...timeout period is set correctly to 20 in the following line of codes:

Code:

$ua->timeout(20);


(Check your watch...Wink)

Keep in mind that LWP::UserAgent is relatively accurate...it will capture all errors...if you only want 404 errors to print the error page, then you will have to change the following codes:

Code:

} else {
print "$url\nlink sucks\n\n";
$valid_update = $dbh->do("UPDATE files SET valid = valid + 1 WHERE url = '$url'");
}


to the following:

Code:

} if ($res->error eq '404') {
print "$url\nlink sucks\n\n";
$valid_update = $dbh->do("UPDATE files SET valid = valid + 1 WHERE url = '$url'");
}


Regards,

Eliot Lee