Created
January 8, 2015 11:00
-
-
Save jhthorsen/a08f6e3d4e8901d7861b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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