Skip to content

Instantly share code, notes, and snippets.

@jhthorsen
Created January 8, 2015 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhthorsen/a08f6e3d4e8901d7861b to your computer and use it in GitHub Desktop.
Save jhthorsen/a08f6e3d4e8901d7861b to your computer and use it in GitHub Desktop.
package Mojo::IOLoop::Tail;
use Mojo::Base "Mojo::IOLoop::Stream";
has check_timeout => 0.01;
sub close {
my $self = shift;
return unless my $handle = $self->{handle};
seek $handle, 0, 1;
$self->stop;
}
sub new {
my ($class, $fh) = @_;
my ($self, $tid);
unless (ref $fh) {
open my $FH, "<", $fh or die "Open $fh: $!";
$fh = $FH;
}
seek $fh, 0, 2; # seek to end of file
$self = $class->SUPER::new($fh);
$self->{tail_tid} = $self->reactor->recurring($self->check_timeout, sub { $self and $self->start });
$self;
}
sub DESTROY {
my $self = shift;
my $reactor = $self->reactor;
$self->SUPER::close;
$reactor->remove($self->{tail_tid}) if $reactor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment