Snippets

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

You are viewing an old version of this snippet. View the current version.
Revised by Brian Medley ddb4808
#!/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.