AnyEvent-Processor

 view release on metacpan or  search on metacpan

lib/AnyEvent/Processor.pm  view on Meta::CPAN

package AnyEvent::Processor;
#ABSTRACT: Base class to define an event-driven (AnyEvent) task that could periodically be interrupted by a watcher
$AnyEvent::Processor::VERSION = '0.006';
use Moose;
use Modern::Perl;
use AnyEvent;
use Glib;
use AnyEvent::Processor::Watcher;

with 'AnyEvent::Processor::WatchableTask';


has verbose => ( is => 'rw', isa => 'Int' );

has watcher => ( 
    is => 'rw', 
    isa => 'AnyEvent::Processor::Watcher',
);

has count => ( is => 'rw', isa => 'Int', default => 0 );

has blocking => ( is => 'rw', isa => 'Bool', default => 0 );


sub run {
    my $self = shift;
    if ( $self->blocking ) {
        $self->run_blocking();
    }
    else {
        $self->run_task();
    }
}


sub run_blocking {
    my $self = shift;
    while ( $self->process() ) {
        ;
    }
}


sub run_task {
    my $self = shift;

    $self->start_process();

    if ( $self->verbose ) {
        $self->watcher(
            AnyEvent::Processor::Watcher->new( delay => 1, action => $self )
        ) unless $self->watcher;
        $self->watcher->start();
    }

    my $end_run = AnyEvent->condvar;
    my $idle = AnyEvent->idle( cb => sub {
        unless ( $self->process() ) {
            $self->end_process();
            $self->watcher->stop() if $self->watcher;
            $end_run->send;
        }
    });
    $end_run->recv;
}

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.182 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )