bin/disorder-notify: Keep the log connection alive by sending bytes back.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 2 Jun 2020 10:11:01 +0000 (11:11 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Tue, 2 Jun 2020 16:20:38 +0000 (17:20 +0100)
This was, if it's not already clear, the main objective to the
`select'-based log loop.

bin/disorder-notify

index b632ef4..092d255 100755 (executable)
@@ -220,18 +220,24 @@ sub watch_and_notify0 ($) {
     }
 
     if (!$sk_log) { $loss = "EOF from server"; last WATCH; }
-    select my $rdout = $rdin, undef, undef, undef;
-    READ: for (;;) {
-      my ($b, $n);
-      eval { $n = sysread $sk_log, $b, 4096; };
-      if ($@ && $@->errno == EAGAIN) { last READ; }
-      elsif ($@) { $loss = "error from read: " . $@->errno; last WATCH; }
-      elsif (!$n) { close $sk_log; $sk_log = undef; }
-      else { $buffer .= $b; }
-    }
+    my $nfd = select my $rdout = $rdin, undef, undef, 60;
+    if (!$nfd) {
+      eval { print $sk_log "."; flush $sk_log; };
+      if ($@) { $loss = "error from write: " . $@->errno; last WATCH; }
+      @lines = ();
+    } else {
+      READ: for (;;) {
+       my ($b, $n);
+       eval { $n = sysread $sk_log, $b, 4096; };
+       if ($@ && $@->errno == EAGAIN) { last READ; }
+       elsif ($@) { $loss = "error from read: " . $@->errno; last WATCH; }
+       elsif (!$n) { close $sk_log; $sk_log = undef; }
+       else { $buffer .= $b; }
+      }
 
-    @lines = split /\n/, $buffer, -1;
-    $buffer = pop @lines;
+      @lines = split /\n/, $buffer, -1;
+      $buffer = pop @lines;
+    }
   }
 
   notify "$TITLE state", "Lost connection: $loss";