AnyEvent-Cron
view release on metacpan or search on metacpan
# AnyEvent-Cron
my $cron = AnyEvent::Cron->new(
verbose => 1,
debug => 1,
after => 1,
interval => 1,
ignore_floating => 1
);
# 00:00 (hour:minute)
$cron->add("00:00" => sub { warn "zero"; })
# hour : minute : second
->add( "*:*:10" => sub { })
lib/AnyEvent/Cron.pm view on Meta::CPAN
map(($_, 60*60*24*7), qw(w week weeks)),
map(($_, 60*60*24*30), qw(M month months)),
map(($_, 60*60*24*365), qw(y year years)) );
has interval =>
( is => 'rw' , isa => 'Int' , default => sub { 1 } );
has verbose =>
( is => 'rw' , isa => 'Bool' , default => sub { 0 } );
has debug =>
( is => 'rw' , isa => 'Bool' , default => sub { 0 } );
# TODO:
has ignore_floating =>
( is => 'rw', isa => 'Bool' , default => sub { 0 } );
has jobs =>
traits => ['Array'],
handles => {
add_job => 'push'
lib/AnyEvent/Cron.pm view on Meta::CPAN
}
sub _schedule {
my ($self,$job) = @_;
AnyEvent->now_update();
my $now_epoch = AnyEvent->now;
my $next_epoch;
my $delay;
my $name = $job->{name};
my $debug = $job->{debug};
if( $job->{event} ) {
my $event = $job->{event};
$next_epoch = $event->next->epoch; # set next schedule time
$delay = $next_epoch - $now_epoch;
warn "delay:",$delay if $debug;
}
elsif( $job->{seconds} ) {
$next_epoch = $now_epoch + $job->{seconds};
$delay = $next_epoch - $now_epoch;
warn "delay:",$delay if $debug;
}
elsif( $job->{time} ) {
my $time = $job->{time};
my $now = DateTime->from_epoch( epoch => $now_epoch ); # depends on now
my $next = $now->clone;
$next->set( %$time );
# now > the scheduled time
if( DateTime->compare( $now, $next ) == 1 ) {
if( $time->{month} ) {
lib/AnyEvent/Cron.pm view on Meta::CPAN
$job->{watchers}{$next_epoch} = AnyEvent->timer(
after => $delay,
cb => sub {
$self->{_cv}->begin;
delete $job->{watchers}{$next_epoch};
$self->_schedule($job) unless $job->{once};
if ( $job->{single} && $job->{running}++ ) {
print STDERR "Skipping job '$name' - still running\n"
if $debug;
}
else {
eval { $job->{cb}->( $self->{_cv}, $job ); 1 }
or warn $@ || 'Unknown error';
delete $job->{running};
print STDERR "Finished job '$name'\n"
if $debug;
}
$self->{_cv}->end;
}
);
}
sub run {
my $self = shift;
my $cv = $self->{_cv} = AnyEvent->condvar;
for my $job ( @{ $self->jobs } ) {
lib/AnyEvent/Cron.pm view on Meta::CPAN
__END__
=head1 NAME
AnyEvent::Cron - Crontab in AnyEvent! provide an interface to register event on specified time.
=head1 SYNOPSIS
my $cron = AnyEvent::Cron->new(
verbose => 1,
debug => 1,
ignore_floating => 1
);
# 00:00 (hour:minute)
$cron->add("00:00" => sub { warn "zero"; })
->add( '* * * * *' => sub { } )
->add( '1 seconds' => sub { } )
->add( '3 days' => sub { } )
->run();
( run in 0.617 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )