use MIME::Entity;
use Pod::Usage;
use Config::Auto;
+use Sys::SigAction qw/timeout_call/;
# Try and load a config file first, and then allow the command line
# to over ride what is in the config file.
my $ua = LWP::UserAgent->new;
$ua->timeout(30);
$ua->env_proxy;
- my $response;
- eval {
- local $SIG{ALRM} = sub { die "alarm\n" };
- alarm(30);
- $response = $ua->get($url);
- alarm(0);
- };
+ my $response = ua_request_with_timeout($ua, $url);
die "Sorry, failed to fetch $url: Connection to MythTV timed out.\n"
- if $@;
+ unless defined $response;
die "Sorry, failed to fetch $url:\n" . $response->status_line . "\n"
unless $response->is_success;
return $_[0];
}
+# Takes a LWP::UserAgent, and a HTTP::Request, returns a HTTP::Request
+# Based on:
+# http://stackoverflow.com/questions/73308/true-timeout-on-lwpuseragent-request-method
+sub ua_request_with_timeout {
+ my ($ua, $url) = @_;
+
+ # Get whatever timeout is set for LWP and use that to
+ # enforce a maximum timeout per request in case of server
+ # deadlock. (This has happened.)
+ our $res = undef;
+ if( timeout_call( $ua->timeout(), sub {$res = $ua->get($url);}) ) {
+ return undef;
+ } else {
+ return $res;
+ }
+}
+
# Beautify numbers by sticking commas in.
sub commify {
my ($num) = shift;