Changes are:
authorAndrew DeFaria <Andrew@DeFaria.com>
Wed, 5 May 2021 17:44:47 +0000 (10:44 -0700)
committerAndrew DeFaria <Andrew@DeFaria.com>
Wed, 5 May 2021 17:44:47 +0000 (10:44 -0700)
. Added another greeting to announceEmail.pl
. Removed a Perl::Critic warning for Unils.pm
. Changed message for Speak to use [silent]
. Created announceEmail sleep script

Actually not a sleep script but rather it is a script that needs to be
added to /lib/systemd/systemd-sleep (symlinked) so that when Ubuntu
20.04 comes out of suspend mode a USR2 signal is sent to all
announceEmail.pl processes. They then wake up and reconnect to their
IMAP hosts. This is necessary because the network is down during suspend
and Mail::IMAPTalk doesn't notice this so email announcements stop
happening. If instead Mail::IMAPTalk would have noticed this and
returned an error I could re-establish the connection in
announceEmail.pl but it doesn't.

Notice also that it takes 10-30 seconds to re-establish the network
connection after suspend so we just put a 60 second sleep in this script
rather than implementing a more complicated polling loop looking for the
network to come back up.

bin/announceEmail [new file with mode: 0755]
bin/announceEmail.pl
lib/Clearcase.pm
lib/Speak.pm
lib/Utils.pm

diff --git a/bin/announceEmail b/bin/announceEmail
new file mode 100755 (executable)
index 0000000..deab422
--- /dev/null
@@ -0,0 +1,12 @@
+#!/bin/bash
+case $1 in
+  post)
+    # If we send the USR2 signal before the network comes back up then 
+    # announceEmail.pl will die trying to re-establish its connection to the
+    # IMAP server. So instead we use this cheap method of sleeping for a minute
+    # as the network should be back by then. A better method would be to poll
+    # looking for the network to come back up but that's tricker to implement.
+    sleep 60
+    killall -USR2 announceEmail.pl
+    ;;
+esac
index 2826ed3..d21003b 100755 (executable)
@@ -42,7 +42,7 @@ $Date: 2019/04/04 13:40:10 $
    -h|elp        Detailed help
    -v|erbose     Verbose mode (Default: -verbose)
    -de|bug       Turn on debugging (Default: Off)
+
    -user|name    User name to log in with (Default: $USER)
    -p|assword    Password to use (Default: prompted)
    -i|map        IMAP server to talk to (Default: defaria.com)
@@ -54,6 +54,10 @@ $Date: 2019/04/04 13:40:10 $
    -uses|sl      Whether or not to use SSL to connect (Default: False)
    -useb|locking Whether to block on socket (Default: False)
 
+ Signals:
+   $SIG{USR1}:   Toggles debug option
+   $SIG{USR2}:   Reestablishes connection to IMAP server
+
 =head1 DESCRIPTION
 
 This script will connect to an IMAP server, login and then monitor the user's
@@ -98,6 +102,7 @@ my @greetings = (
   'I was looking in your inbox and found a message',
   'Not sure you want to hear this message',
   'Good news',
+  "What's this? A new message",
 );
 
 my %opts = (
@@ -123,7 +128,17 @@ sub interrupted {
   return;
 } # interrupted
 
+sub Connect2IMAP;
+
+sub restart {
+  $log->dbug("Re-establishing connection to $opts{imap} as $opts{username}");
+  Connect2IMAP;
+
+  goto MONITORMAIL;
+} # restart
+
 $SIG{USR1} = \&interrupted;
+$SIG{USR2} = \&restart;
 
 sub unseenMsgs() {
   $IMAP->select('inbox') or
@@ -135,6 +150,9 @@ sub unseenMsgs() {
 sub Connect2IMAP() {
   $log->dbug("Connecting to $opts{imap} as $opts{username}");
 
+  # Destroy any old connections
+  undef $IMAP;
+
   $IMAP = Mail::IMAPTalk->new(
     Server      => $opts{imap},
     Username    => $opts{username},
@@ -236,27 +254,9 @@ sub MonitorMail() {
   # If we return from idle then the server went away for some reason. With Gmail
   # the server seems to time out around 30-40 minutes. Here we simply reconnect
   # to the imap server and continue to MonitorMail.
-  $log->dbug("MonitorMail: Connection to $opts{imap} ended - lasted "
-           . howlong $startTime);
-
-  # Destroy current IMAP connection
-  $log->dbug("MonitorMail: Destroying IMAP connection to $opts{imap}");
+  restart;
 
-  undef $IMAP;
-
-  # Re-establish connection
-  Connect2IMAP;
-
-  $log->dbug("MonitorMail: Reconnected to IMAP server $opts{imap}");
-
-  # MonitorMail again - the dreaded goto! Seems the cleanest way to restart
-  # in this instance. I could call MonitorMail() recursively but that would
-  # leave junk on the stack.
-  $log->dbug('MonitorMail: Going back to the top of the loop');
-
-  goto MONITORMAIL;
-
-  return; # To make perlcritic happy
+  return;
 } # MonitorMail
 
 END {
index e9b58ff..e34bf48 100644 (file)
@@ -150,7 +150,7 @@ sub DESTROY {
 
   # Call old signal handler (if any)
   &$oldHandler if $oldHandler;
-  
+
   return;
 } # DESTROY
 
@@ -523,7 +523,7 @@ sub execute {
 Sends a command to the cleartool coprocess. If not running a cleartool coprocess
 is started and managed. The coprocess is implemented as a coprocess using IPC
 for communication that will exist until the object is destroyed. Stdin and
-stdout/stderr are therefore pipes and can be fed. The execute method feds the
+stdout/stderr are therefore pipes and can be fed. The execute method feeds the
 input pipe and returns status and output from the output pipe.
 
 Using execute can speed up execution of repeative cleartool invocations
@@ -594,10 +594,10 @@ Array of output lines from the cleartool command execution.
     # Simple check to see if we can execute cleartool
     @output = `$cleartool -ver 2>&1`;
     @output = ();
-        
+
     return (-1, 'Clearcase not installed')
       unless $? == 0;
-          
+
     $clearpid = open3 ($clearin, $clearout, $clearout, $cleartool, "-status");
 
     return (-1, ('Clearcase not installed')) unless $clearpid;
@@ -631,7 +631,7 @@ Array of output lines from the cleartool command execution.
   $self->{lastcmd} = 'cleartool ' . $cmd;
   $self->{status}  = $status;
   $self->{output}  = join "\n", @output;
-  
+
   return ($status, @output);
 } # execute
 
index c18c2f7..d462490 100644 (file)
@@ -104,10 +104,11 @@ Returns:
 =cut
 
   if (-f "$FindBin::Bin/shh") {
+    $msg .= ' [silent]';
     if ($log) {
-      $log->msg("Not speaking because we were asked to be quiet - $msg");
+      $log->msg($msg);
     } else {
-      verbose "Not speaking because we were asked to be quiet - $msg";
+      verbose $msg;
     } # if
 
     return;
index 29e020c..446d2e7 100644 (file)
@@ -471,7 +471,7 @@ In a scalar context just the 1 minute load average.
   # TODO: Make it work on Windows...
   return if $^O =~ /win/i;
 
-  open my $loadAvg, '/proc/loadavg'
+  open my $loadAvg, '<', '/proc/loadavg'
     or croak "Unable to open /proc/loadavg\n";
 
   my $load = <$loadAvg>;