AnyEvent-Cron

 view release on metacpan or  search on metacpan

META.yml  view on Meta::CPAN

meta-spec:
  url: http://module-build.sourceforge.net/META-spec-v1.4.html
  version: 1.4
name: AnyEvent-Cron
no_index:
  directory:
    - inc
    - t
requires:
  AnyEvent: 0
  DateTime: 0
  DateTime::Event::Cron: 0
  Moose: 0
  Try::Tiny: 0
  perl: 5.12.0
resources:
  license: http://dev.perl.org/licenses/
version: 0.03

Makefile.PL  view on Meta::CPAN

use inc::Module::Install;
name     'AnyEvent-Cron';
all_from 'lib/AnyEvent/Cron.pm';
author   q{Cornelius <cornelius.howl_at_gmail.com>};
license  'perl';

build_requires 'Test::More';

requires 'DateTime';
requires 'AnyEvent';
requires 'Moose';
requires 'Try::Tiny';
requires 'DateTime::Event::Cron';
auto_install;
WriteAll;

README.md  view on Meta::CPAN

            ignore_floating => 1
    );

    # 00:00 (hour:minute)
    $cron->add("00:00" => sub { warn "zero"; })

        # hour : minute : second 
        ->add( "*:*:10" => sub { })
        ->add( "1:*:*" => sub { })

        ->add( DateTime->now => sub { warn "datetime now" } )
        ->run();

or:

    $cron->add({  
        type => 'interval',
        second => 0 ,
        triggered => 0,
        callback => sub { 
            warn "SECOND INTERVAL TRIGGERD";
        },
    },{  
        type => 'interval',
        hour => DateTime->now->hour , 
        minute =>  DateTime->now->minute ,
        callback => sub { 
            warn "HOUR+MINUTE INTERVAL TRIGGERD";
        },
    });

    $cron->add({
        type => 'datetime' ,
        callback => sub { warn "DATETIME TRIGGED"  },
        datetime => (sub { 
                return DateTime->now->add_duration( DateTime::Duration->new( minutes => 0 ) ); })->()
        });

    my $cv = AnyEvent->condvar;
    $cv->recv;


## INSTALLATION

To install this module, run the following commands:

eg/simple-cron.pl  view on Meta::CPAN

#!/usr/bin/env perl
use AnyEvent::Cron;
my $cron = AnyEvent::Cron->new( verbose => 1 );

# 00:00 (hour:minute)
$cron->add("00:00" => sub { warn "zero"; });

$cron->add( DateTime->now => sub { warn "datetime now" } );

$cron->add({  
    type => 'interval',
    second => 0 ,
    triggered => 0,
    callback => sub { 
        warn "SECOND INTERVAL TRIGGERD";
    },
},{  
    type => 'interval',
    hour => DateTime->now->hour , 
    minute =>  DateTime->now->minute ,
    callback => sub { 
        warn "HOUR+MINUTE INTERVAL TRIGGERD";
    },
},{  
    type => 'interval',
    hour => DateTime->now->hour ,
    callback => sub { 
        warn "HOUR INTERVAL TRIGGERD";
    },
},{  
    type => 'interval',
    minute => DateTime->now->minute ,
    callback => sub { 
        warn "MINUTE INTERVAL TRIGGERD";
    },
},{
    type => 'datetime' ,
    callback => sub { warn "DATETIME TRIGGED"  },
    datetime => (sub { 
            # my $dt = DateTime->now->add_duration( DateTime::Duration->new( minutes => 0 ) );
            my $dt = DateTime->now;
            # $dt->set_second(0);
            # $dt->set_nanosecond(0);
            warn "Next trigger: ", $dt;
            return $dt; })->()
})->run();

my $cv = AnyEvent->condvar;
$cv->recv;

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

package AnyEvent::Cron;
use warnings;
use strict;
use DateTime;
use AnyEvent;
use Moose;
use Try::Tiny;
use DateTime::Event::Cron;
use v5.12;

our $VERSION = '0.03';


# map of expiration formats to their respective time in seconds
my %_Expiration_Units = ( map(($_,             1), qw(s second seconds sec)),
                          map(($_,            60), qw(m minute minutes min)),
                          map(($_,         60*60), qw(h hour hours)),
                          map(($_,      60*60*24), qw(d day days)),

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

has timers => ( is => 'rw', isa => 'ArrayRef' , default => sub { [ ] } );

use Scalar::Util qw(refaddr);

sub add {
    my $self = shift;
    my ( $timespec, $cb , %args ) = @_;

    # try to create with crontab format
    try {
        my $cron_event = DateTime::Event::Cron->new($timespec);
        $self->add_job({
            event => $cron_event,
            cb => $cb,
            %args
        });
    } 
    catch {
        given ( $timespec ) {
            # hour:minute per day
            when( m{^(\d+):(\d+)$} ) {

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

        $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} ) {
                $next->add( years => 1 );
            }
            elsif( $time->{day} ) {
                $next->add( months => 1 );
            }
            elsif( $time->{hour} ) {
                $next->add( days => 1 );
            }
            elsif( $time->{minute} ) {

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

        ->run();

    my $cv = AnyEvent->condvar;
    $cv->recv;


=head1 METHODS

=head2 add( "12:36" => sub {     } )

=head2 add( DateTime->now => sub {     } )

=head1 AUTHOR

Cornelius, C<< <cornelius.howl_at_gmail.com> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-anyevent-cron at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-Cron>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.



( run in 0.392 second using v1.01-cache-2.11-cpan-05444aca049 )