]> git.refcnt.org Git - lugs.git/blobdiff - lreminder/reminder.pl
lreminder: retry after timeout
[lugs.git] / lreminder / reminder.pl
index ef063a124ca5babc372a7e121997d5ce667bf050..eb9d79461e22a221c1d4de2e6fb4ffdfcb32a5e0 100755 (executable)
@@ -15,7 +15,7 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #
 # Author: Steven Schubiger <stsc@refcnt.org>
-# Last modified: Tue Jul 15 21:08:39 CEST 2014
+# Last modified: Fri Jan  8 12:43:13 CET 2016
 
 use strict;
 use warnings;
@@ -34,9 +34,10 @@ use Hook::Output::File ();
 use LUGS::Events::Parser ();
 use Mail::Sendmail qw(sendmail);
 use Text::Wrap::Smart::XS qw(fuzzy_wrap);
+use URI ();
 use WWW::Mechanize ();
 
-my $VERSION = '0.44';
+my $VERSION = '0.51';
 
 #-----------------------
 # Start of configuration
@@ -49,6 +50,8 @@ my $Config = {
     dbase_name => '<hidden>',
     dbase_user => '<hidden>',
     dbase_pass => '<hidden>',
+    sleep_secs => 300,
+    max_tries  => 48,
 };
 
 #---------------------
@@ -56,7 +59,7 @@ my $Config = {
 #---------------------
 
 my $dbh  = DBI->connect("dbi:mysql(RaiseError=>1):$Config->{dbase_name}", $Config->{dbase_user}, $Config->{dbase_pass});
-my $file = File::Basename::basename($Config->{events_url});
+my $file = File::Spec->catfile('tmp', (URI->new($Config->{events_url})->path_segments)[-1]);
 
 my ($test, $run) = (false, false);
 
@@ -88,11 +91,31 @@ sub getopts
 sub fetch_and_write_events
 {
     my $mech = WWW::Mechanize->new;
-    my $http = $mech->get($Config->{events_url});
 
-    open(my $fh, '>', $file) or die "Cannot open $file for writing: $!\n";
-    print {$fh} $http->content;
-    close($fh);
+    my ($http, $retry, $tries);
+    $http = undef;
+
+    do {
+        $retry = false;
+        $tries++;
+        eval {
+            $http = $mech->get($Config->{events_url});
+        } or do {
+            warn "[${\scalar localtime}] $@";
+            $retry = ($tries < $Config->{max_tries}) ? true : false;
+            sleep $Config->{sleep_secs} if $retry;
+        };
+    } while ($retry);
+
+    if (defined $http) {
+        open(my $fh, '>', $file) or die "Cannot open $file for writing: $!\n";
+        print {$fh} $http->content;
+        close($fh);
+    }
+    else {
+        warn "[${\scalar localtime}] ${\File::Basename::basename($0)} not entirely run, no http content\n";
+        exit;
+    }
 }
 
 sub init
@@ -109,11 +132,8 @@ sub init
                 rewrite => '$TEXT - $HREF',
                 fields  => [ qw(location more) ],
             } ],
-            'br' => [ {
-                rewrite => '',
-                fields  => [ qw(more) ],
-            } ],
         },
+        purge_tags => [ qw(location responsible more) ],
         strip_text => [ 'mailto:' ],
     });
     unlink $file;
@@ -188,7 +208,7 @@ sub send_mail
 
     my $month_name = $month_names{$month};
 
-my $message = (<<MSG);
+my $message = (<<"MSG");
 Wann:\t$wday, $simple_day. $month_name $year, $time Uhr
 Was :\t$title
 Wo  :\t$location
@@ -238,9 +258,9 @@ sub info_string
     my $modified = localtime((stat($0))[9]);
 
     $modified =~ s/(?<=\b) (?:\d{2}\:?){3} (?=\b)//x;
-    $modified =~ s/\s+/ /g;
+    $modified =~ s/\s{2,}/ /g;
 
-    my $info = <<EOT;
+    my $info = <<"EOT";
 -- 
 running $script v$VERSION - last modified: $modified
 EOT