AnyEvent-Cron
view release on metacpan or search on metacpan
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)),
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'
},
is => 'rw',
isa => 'ArrayRef' ,
default => sub { [ ] }
;
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+)$} ) {
my ( $hour, $minute ) = ( $1, $2 );
$self->add_job({
time => { hour => $hour, minute => $minute },
cb => $cb,
%args,
});
}
when( m{^\s*(\d+)\s*(\w+)} ) {
my ( $number, $unit ) = ( $1, $2 );
my $seconds = $number * $_Expiration_Units{$unit};
$self->add_job({
seconds => $seconds,
cb => $cb,
( run in 1.620 second using v1.01-cache-2.11-cpan-39bf76dae61 )