AnyEvent-DateTime-Cron
view release on metacpan or search on metacpan
lib/AnyEvent/DateTime/Cron.pm view on Meta::CPAN
)
->start
->recv
$cron = AnyEvent::DateTime::Cron->new();
$cron->debug(1)->add(
'* * * * *', name => 'job_name', single => 1, sub {'foo'},
...
);
$cron->delete($job_id,$job_id...)
$cv = $cron->start;
$cv->recv;
AnyEvent::DateTime::Cron->new(time_zone => 'local');
->add(
'* * * * *' => sub { warn "Every minute"},
'*/2 * * * *' => sub { warn "Every second minute"},
)
->start
->recv
=head1 DESCRIPTION
L<AnyEvent::DateTime::Cron> is an L<AnyEvent> based crontab, which supports
all crontab formats recognised by L<DateTime::Event::Cron>.
It allows you to shut down a running instance gracefully, by waiting for
any running cron jobs to finish before exiting.
=head1 METHODS
=head2 new()
$cron = AnyEvent::DateTime::Cron->new(
time_zone => ...
quartz => 0/1
);
Creates a new L<AnyEvent::DateTime::Cron> instance - takes optional parameters
time_zone and quartz.
time_zone can will be used to set the time_zone for any DateTime objects that
are used internally.
if quartz is set to a true value then this class will use switch to using
L<DateTime::Event::Cron::Quartz> internally, which will allow the use of seconds
in the cron expression. See the DateTime::Event::Cron::Quartz for details on
writing a proper quartz cron expression.
=head2 add()
$cron->add(
'* * * * *', sub {...},
'* * * * *', name => 'job_name', single => 1, sub {...},
...
);
Use C<add()> to add new cron jobs. It accepts a list of crontab entries,
optional paremeters and callbacks.
The C<name> parameter is useful for debugging, otherwise the auto-assigned
C<ID> is used instead.
The C<single> parameter, if C<true>, will only allow a single instance of
a job to run at any one time.
New jobs can be added before running, or while running.
See L</"CALLBACKS"> for more.
=head2 delete()
$cron->delete($job_id,$job_id,....)
Delete one or more existing jobs, before starting or while running.
=head2 start()
my $cv = $cron->start;
$cv->recv;
Schedules all jobs to start at the next scheduled time, and returns an
L<AnyEvent condvar|http://metacpan.org/module/AnyEvent#CONDITION-VARIABLES>.
The cron loop can be started by calling C<recv()> on the condvar.
=head2 stop()
$cron->stop()
Used to shutdown the cron loop gracefully. You can also shutdown the cron loop
by sending a C<TERM> signal to the process.
=head2 jobs()
$job = $cron->jobs
Returns a hashref containing all the current cron jobs.
=head2 debug()
$cron->debug(1|0)
Turn on debugging.
=head1 CALLBACKS
A callback is a coderef (eg an anonymous subroutine) which will be called
every time your job is triggered. Callbacks should use C<AnyEvent> themselves,
so that they run asynchronously, otherwise they can block the execution
of the cron loop, delaying other jobs.
Two parameters are passed to your callback: the main C<$cv> of the cron loop,
and the C<$job_description> which contains various details about the current
job.
The C<$cv> is the most important parameter, as it allows you to control how
your cron loop will shut down. If your callback doesn't use
C<AnyEvent> and is blocking, then your callback will complete before it
( run in 4.121 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )