AnyEvent-Timer-Cron
view release on metacpan or search on metacpan
"t",
"xt"
]
},
"prereqs" : {
"build" : {},
"configure" : {},
"runtime" : {
"requires" : {
"AnyEvent" : "0",
"DateTime" : "0",
"DateTime::Event::Cron" : "0",
"Moo" : "1.000007",
"Safe::Isa" : "0",
"Task::Weaken" : "0",
"namespace::clean" : "0",
"perl" : "5.008"
}
},
"test" : {
"requires" : {
"Test::More" : "0.94"
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: '1.4'
name: AnyEvent-Timer-Cron
no_index:
directory:
- t
- xt
requires:
AnyEvent: '0'
DateTime: '0'
DateTime::Event::Cron: '0'
Moo: '1.000007'
Safe::Isa: '0'
Task::Weaken: '0'
namespace::clean: '0'
perl: '5.008'
resources:
bugtracker: https://rt.cpan.org/Dist/Display.html?Name=AnyEvent-Timer-Cron
license: http://dev.perl.org/licenses/
repository: git://github.com/haarg/AnyEvent-Timer-Cron.git
version: '0.002000'
Makefile.PL view on Meta::CPAN
prereqs => {
test => { requires => {
'Test::More' => 0.94,
} },
runtime => { requires => {
'perl' => '5.008',
'Moo' => '1.000007',
'AnyEvent' => 0,
'Task::Weaken' => 0,
'Safe::Isa' => 0,
'DateTime' => 0,
'DateTime::Event::Cron' => 0,
'namespace::clean' => 0,
} },
},
resources => {
repository => {
url => 'git://github.com/haarg/AnyEvent-Timer-Cron.git',
web => 'http://github.com/haarg/AnyEvent-Timer-Cron',
type => 'git',
},
bugtracker => {
work, instead providing the simplest implementation, and using
AnyEvent's standard conventions for timer lifetime.
METHODS
new( cron => $cron, cb => sub {} )
Creates a new cron timer. The callback will be called continually
according to the cron rules until the object is destroyed.
cron
Required. A cron rule, either in string form or as a
DateTime::Event::Cron, DateTime::Event::Cron::Quartz, or
DateTime::Set object.
cb Required. The callback to call for the cron events.
time_zone
A cron rule will be calculated under the specified time zone. If not
specified, events will be calculated using UTC.
SEE ALSO
AnyEvent::Cron
AnyEvent::DateTime::Cron
AUTHOR
haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org>
CONTRIBUTORS
keedi - Keedi Kim (cpan:KEEDI) <keedi@cpan.org>
COPYRIGHT
Copyright (c) 2013 the AnyEvent::Timer::Cron "AUTHOR" and "CONTRIBUTORS"
as listed above.
lib/AnyEvent/Timer/Cron.pm view on Meta::CPAN
package AnyEvent::Timer::Cron;
use Moo;
our $VERSION = '0.002000';
$VERSION = eval $VERSION;
use AnyEvent;
use Scalar::Util qw(weaken);
use Safe::Isa;
use DateTime;
use DateTime::Event::Cron;
use namespace::clean;
has 'cb' => (is => 'ro', required => 1);
has 'time_zone' => (is => 'ro');
has '_cron' => (
is => 'ro',
required => 1,
init_arg => 'cron',
coerce => sub {
my $cron = shift;
if (!ref $cron) {
$cron = DateTime::Event::Cron->new($cron);
}
if ($cron->$_can('next')) {
return sub { $cron->next(@_) };
}
elsif ($cron->$_can('get_next_valid_time_after')) {
return sub { $cron->get_next_valid_time_after(@_) };
}
die "Invalid cron!";
},
);
has '_timer' => (is => 'rw');
sub BUILD {
my $self = shift;
$self->create_timer;
}
sub create_timer {
my $self = shift;
weaken $self;
my $now = DateTime->from_epoch(epoch => AnyEvent->now);
$now->set_time_zone( $self->time_zone ) if $self->time_zone;
my $next = $self->next_event($now);
return
if not $next;
my $interval = $next->subtract_datetime_absolute($now)->in_units('nanoseconds') / 1_000_000_000;
$self->_timer(AnyEvent->timer(
after => $interval,
cb => sub {
$self->{cb}->();
$self && $self->create_timer;
},
));
}
sub next_event {
my $self = shift;
my $now = shift || DateTime->from_epoch(epoch => AnyEvent->now);
$now->set_time_zone( $self->time_zone ) if $self->time_zone;
$self->_cron->($now);
}
1;
__END__
=head1 NAME
AnyEvent::Timer::Cron - cron based timers for AnyEvent
lib/AnyEvent/Timer/Cron.pm view on Meta::CPAN
=head2 new( cron => $cron, cb => sub {} )
Creates a new cron timer. The callback will be called continually
according to the cron rules until the object is destroyed.
=over 4
=item cron
Required. A cron rule, either in string form or as a
L<DateTime::Event::Cron>, L<DateTime::Event::Cron::Quartz>, or
L<DateTime::Set> object.
=item cb
Required. The callback to call for the cron events.
=item time_zone
A cron rule will be calculated under the specified time zone. If not specified,
events will be calculated using UTC.
=back
=head1 SEE ALSO
=over 4
=item L<AnyEvent::Cron>
=item L<AnyEvent::DateTime::Cron>
=back
=head1 AUTHOR
haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org>
=head2 CONTRIBUTORS
keedi - Keedi Kim (cpan:KEEDI) <keedi@cpan.org>
( run in 0.376 second using v1.01-cache-2.11-cpan-05444aca049 )