Snippets

Brian Medley Fancy file tai'r that will POST each line somewhere

Updated by Brian Medley

File fancy_tail.pl Modified

  • Ignore whitespace
  • Hide word diff
 }
 
 $fh->autoflush(1);
+$fh->sysseek(0, 2);
+
+my @lines = ();
 
 my $reactor = Mojo::IOLoop->singleton->reactor;
 $reactor->io($fh =>
             $line .= $buf;
 
             if ("\n" eq $buf) {
-                warn($line);
-
-                my $ua = Mojo::UserAgent->new;
+                warn("> $line");
 
-                $ua->post('http://127.0.0.1:300/v1/api/parse' => json => {line => $line} => sub {});
+                push(@lines, $line);
 
                 $line = undef;
             }
     }
 )->watch($fh, 1, 0);
 
-my $id = $reactor->recurring(10 => sub {
-    say("Heartbeat");
+my $id = $reactor->recurring(60 => sub {
+    my $ua = Mojo::UserAgent->new;
+    
+    foreach my $line (@lines) {
+        warn(">> $line");
+        $ua->post('http://127.0.0.1:300/v1/api/parse' => json => {line => $line} => sub {});
+    }
+
+    @lines = ();
 });
 
 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Created by Brian Medley

File fancy_tail.pl Added

  • Ignore whitespace
  • Hide word diff
+#!/opt/perl
+
+use Mojo::Base -strict;
+
+use Mojo::UserAgent;
+use Mojo::IOLoop;
+
+use IO::File;
+
+my $fh = IO::File->new();
+unless ($fh->open("< tail.me")) {
+    die;
+}
+
+$fh->autoflush(1);
+
+my $reactor = Mojo::IOLoop->singleton->reactor;
+$reactor->io($fh =>
+    sub {
+        state $line = undef;
+
+        my $reactor = shift;
+
+        while (my $ret = sysread($fh, my $buf, 1)) {
+            $line .= $buf;
+
+            if ("\n" eq $buf) {
+                warn($line);
+
+                my $ua = Mojo::UserAgent->new;
+
+                $ua->post('http://127.0.0.1:300/v1/api/parse' => json => {line => $line} => sub {});
+
+                $line = undef;
+            }
+        }
+    }
+)->watch($fh, 1, 0);
+
+my $id = $reactor->recurring(10 => sub {
+    say("Heartbeat");
+});
+
+Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.