blib/lib/AnyEvent/DateTime/Cron.pm

filter: Statementstmt 17.8% 20 / 112 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 35.2% 6 / 17 Podpod 100% 7 / 7 totaltotal 16.4% 33 / 201 CC 34 SCAR 38.6
CC
34
Cov
12%
add
110.0
__ANON__
72.0
delete
42.0
CRAP 47.6
↑ Summary
1 package AnyEvent::DateTime::Cron;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use DateTime();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use DateTime::Event::Cron();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use DateTime::Event::Cron::Quartz();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use AnyEvent();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 our $VERSION = 0.08;
10  
11 #===================================
12 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
13 #===================================
14 0 my ( $class, %params ) = @_;
15  
16 0 foreach my $key ( keys %params ) {
17 0 ▶ die "Unknown param '$key'" unless $key =~ /^(time_zone|quartz)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^(time_zone|quartz)$/ 0 0 0
18 }
19  
20 $params{time_zone} = DateTime::TimeZone->new(name => $params{time_zone})
21 0 ▶ if $params{time_zone};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"time_zone"} 0 0 0
22  
23 0 ▶ $params{quartz} = 0 unless defined $params{quartz};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"quartz"} 0 0 0
24  
25 return bless {
26 _jobs => {},
27 _debug => 0,
28 _id => 0,
29 _running => 0,
30 _time_zone => $params{time_zone},
31 _quartz => $params{quartz},
32 0 }, $class;
33 }
34  
35 #===================================
36 0 ▶sub add {
Subroutine add: not called CC=10 SCAR=47.0
Pod documented
37 #===================================
38 0 my $self = shift;
39 0 ▶ my @args = ref $_[0] eq 'ARRAY' ? @{ shift() } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
40 0 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
41 0 my $cron = shift @args;
42 0 my ( $cb, %params );
43 0 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
44 0 my $key = shift @args;
45 0 ▶ if ( ref $key eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq "CODE") 0 0 0
46 0 $cb = $key;
47 0 last;
48 }
49 0 ▶ die "Unknown param '$key'"
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^(name|single)$/ 0 0 0
50 unless $key =~ /^(name|single)$/;
51 0 $params{$key} = shift @args;
52 }
53 0 ▶ die "No callback found for cron entry '$cron'"
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
54 unless $cb;
55  
56 0 my $event;
57 0 ▶ if ($self->{_quartz}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_quartz'}) { } 0 0 0
58 0 $event = DateTime::Event::Cron::Quartz->new($cron);
59 }
60 else {
61 0 $event = DateTime::Event::Cron->new($cron);
62 }
63  
64 0 my $id = ++$self->{_id};
65 0 ▶ $params{name} ||= $id;
Condition0%0/3
$params{"name"} ||= $id
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"name"} ||= $id
$params{"name"} $id
Truth table0%0/3
A ||= B
A= $params{"name"}B= $id
ABresult
1X1
011
000
66 0 my $job = $self->{_jobs}{$id} = {
67 event => $event,
68 cb => $cb,
69 id => $id,
70 watchers => {},
71 %params,
72 };
73  
74 $self->_schedule($job)
75 0 ▶ if $self->{_running};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_running"} 0 0 0
76 }
77 0 return $self;
78 }
79  
80 #===================================
81 0 ▶sub delete {
Subroutine delete: not called CC=6 SCAR=37.4
Pod documented
82 #===================================
83 0 my $self = shift;
84 0 ▶ my @ids = ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
85  
86 0 for (@ids) {
87 print STDERR "Deleting job '$_'\n"
88 0 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
89  
90 0 ▶ if ( my $job = delete $self->{_jobs}{$_} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $job = delete $self->{'_jobs'}{$_}) { } 0 0 0
elsif ($self->{'_debug'}) { } 0 0 0
91 0 $job->{watchers} = {};
92 }
93 elsif ( $self->{_debug} ) {
94 0 print STDERR "Job '$_' not found\n";
95 }
96 }
97 0 return $self;
98 }
99  
100 #===================================
101 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
102 #===================================
103 0 my $self = shift;
104 0 my $cv = $self->{_cv} = AnyEvent->condvar;
105  
106 0 ▶ $cv->begin( sub { $self->stop } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
107  
108 $self->{_signal} = AnyEvent->signal(
109 signal => 'TERM',
110 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
111 0 ▶ print STDERR "Shutting down\n" if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
112 0 $cv->end;
113 }
114 0 );
115 0 $self->{_running} = 1;
116 0 $self->_schedule( values %{ $self->{_jobs} } );
117  
118 0 return $cv;
119 }
120  
121 #===================================
122 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod documented
123 #===================================
124 0 my $self = shift;
125 0 $_->{watchers} = {} for values %{ $self->{_jobs} };
126  
127 0 my $cv = delete $self->{_cv};
128 0 delete $self->{_signal};
129 0 $self->{_running} = 0;
130  
131 0 $cv->send;
132 0 return $self;
133 }
134  
135 #===================================
136 0 ▶sub _schedule {
Subroutine _schedule: not called CC=6 SCAR=37.4
137 #===================================
138 0 my $self = shift;
139  
140 0 my $time_zone = $self->{_time_zone};
141  
142 0 AnyEvent->now_update();
143 0 my $now_epoch = AnyEvent->now;
144 0 my $now = DateTime->from_epoch( epoch => $now_epoch );
145 0 my $debug = $self->{_debug};
146  
147 0 ▶ $now->set_time_zone($time_zone) if $time_zone;
Branch: 1 branch
BranchTrueFalseTotal
if $time_zone 0 0 0
148  
149 0 for my $job (@_) {
150 0 my $name = $job->{name};
151  
152 0 my $next_run;
153 0 ▶ if ($self->{_quartz}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_quartz'}) { } 0 0 0
154 0 $next_run = $job->{event}->get_next_valid_time_after($now);
155 }
156 else {
157 0 $next_run = $job->{event}->next($now);
158 }
159  
160 0 ▶ $next_run->set_time_zone($time_zone) if $time_zone;
Branch: 1 branch
BranchTrueFalseTotal
if $time_zone 0 0 0
161  
162 0 my $next_epoch = $next_run->epoch;
163 0 my $delay = $next_epoch - $now_epoch;
164  
165 0 ▶ print STDERR "Scheduling job '$name' for: $next_run\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
166 if $debug;
167  
168 0 ▶ my $run_event = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
169 0 ▶ print STDERR "Starting job '$name'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
170 if $debug;
171  
172 0 $self->{_cv}->begin;
173 0 delete $job->{watchers}{$next_epoch};
174  
175 0 $self->_schedule($job);
176  
177 0 ▶ if ( $job->{single} && $job->{running}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{'single'} and $job->{'running'}++) { } 0 0 0
Condition0%0/3
$job->{"single"} and $job->{"running"}++
!ll&&!rl&&r
000
MC/DC0%0/2
$job->{"single"} and $job->{"running"}++
$job->{"single"} $job->{"running"}++
Truth table0%0/3
A and B
A= $job->{"single"}B= $job->{"running"}++
ABresult
0X0
100
111
178 0 ▶ print STDERR "Skipping job '$name' - still running\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
179 if $debug;
180 }
181 else {
182 0 ▶ eval { $job->{cb}->( $self->{_cv}, $job ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $job->{"cb"}->($self->{"_cv"}, $job); 1 } } or warn $@ || "Unknown error" 0 0 0
Condition0%0/2
$@ || 'Unknown error'
l!l
00
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
183 or warn $@ || 'Unknown error';
184 0 delete $job->{running};
185 0 ▶ print STDERR "Finished job '$name'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
186 if $debug;
187 }
188  
189 0 $self->{_cv}->end;
190 0 };
191  
192 0 $job->{watchers}{$next_epoch} = AnyEvent->timer(
193 after => $delay,
194 cb => $run_event
195 );
196 }
197 }
198  
199 #===================================
200 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
201 #===================================
202 0 my $self = shift;
203 0 ▶ $self->{_debug} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
204 0 return $self;
205 }
206  
207 #===================================
208 0 ▶sub jobs { shift->{_jobs} }
Subroutine jobs: not called CC=1 SCAR=6.9
Pod documented
209 #===================================
210  
211 2 1;
212  
213 =pod
214  
215 =encoding UTF-8
216  
217 =head1 NAME
218  
219 AnyEvent::DateTime::Cron - AnyEvent crontab with DateTime::Event::Cron
220  
221 =head1 VERSION
222  
223 version 0.08
224  
225 =head1 SYNOPSIS
226  
227 AnyEvent::DateTime::Cron->new()
228 ->add(
229 '* * * * *' => sub { warn "Every minute"},
230 '*/2 * * * *' => sub { warn "Every second minute"},
231 )
232 ->start
233 ->recv
234  
235 $cron = AnyEvent::DateTime::Cron->new();
236 $cron->debug(1)->add(
237 '* * * * *', name => 'job_name', single => 1, sub {'foo'},
238 ...
239 );
240  
241 $cron->delete($job_id,$job_id...)
242  
243 $cv = $cron->start;
244 $cv->recv;
245  
246 AnyEvent::DateTime::Cron->new(time_zone => 'local');
247 ->add(
248 '* * * * *' => sub { warn "Every minute"},
249 '*/2 * * * *' => sub { warn "Every second minute"},
250 )
251 ->start
252 ->recv
253  
254 =head1 DESCRIPTION
255  
256 L<AnyEvent::DateTime::Cron> is an L<AnyEvent> based crontab, which supports
257 all crontab formats recognised by L<DateTime::Event::Cron>.
258  
259 It allows you to shut down a running instance gracefully, by waiting for
260 any running cron jobs to finish before exiting.
261  
262 =head1 METHODS
263  
264 =head2 new()
265  
266 $cron = AnyEvent::DateTime::Cron->new(
267 time_zone => ...
268 quartz => 0/1
269 );
270  
271 Creates a new L<AnyEvent::DateTime::Cron> instance - takes optional parameters
272 time_zone and quartz.
273  
274 time_zone can will be used to set the time_zone for any DateTime objects that
275 are used internally.
276  
277 if quartz is set to a true value then this class will use switch to using
278 L<DateTime::Event::Cron::Quartz> internally, which will allow the use of seconds
279 in the cron expression. See the DateTime::Event::Cron::Quartz for details on
280 writing a proper quartz cron expression.
281  
282 =head2 add()
283  
284 $cron->add(
285 '* * * * *', sub {...},
286 '* * * * *', name => 'job_name', single => 1, sub {...},
287 ...
288 );
289  
290 Use C<add()> to add new cron jobs. It accepts a list of crontab entries,
291 optional paremeters and callbacks.
292  
293 The C<name> parameter is useful for debugging, otherwise the auto-assigned
294 C<ID> is used instead.
295  
296 The C<single> parameter, if C<true>, will only allow a single instance of
297 a job to run at any one time.
298  
299 New jobs can be added before running, or while running.
300  
301 See L</"CALLBACKS"> for more.
302  
303 =head2 delete()
304  
305 $cron->delete($job_id,$job_id,....)
306  
307 Delete one or more existing jobs, before starting or while running.
308  
309 =head2 start()
310  
311 my $cv = $cron->start;
312 $cv->recv;
313  
314 Schedules all jobs to start at the next scheduled time, and returns an
315 L<AnyEvent condvar|http://metacpan.org/module/AnyEvent#CONDITION-VARIABLES>.
316  
317 The cron loop can be started by calling C<recv()> on the condvar.
318  
319 =head2 stop()
320  
321 $cron->stop()
322  
323 Used to shutdown the cron loop gracefully. You can also shutdown the cron loop
324 by sending a C<TERM> signal to the process.
325  
326 =head2 jobs()
327  
328 $job = $cron->jobs
329  
330 Returns a hashref containing all the current cron jobs.
331  
332 =head2 debug()
333  
334 $cron->debug(1|0)
335  
336 Turn on debugging.
337  
338 =head1 CALLBACKS
339  
340 A callback is a coderef (eg an anonymous subroutine) which will be called
341 every time your job is triggered. Callbacks should use C<AnyEvent> themselves,
342 so that they run asynchronously, otherwise they can block the execution
343 of the cron loop, delaying other jobs.
344  
345 Two parameters are passed to your callback: the main C<$cv> of the cron loop,
346 and the C<$job_description> which contains various details about the current
347 job.
348  
349 The C<$cv> is the most important parameter, as it allows you to control how
350 your cron loop will shut down. If your callback doesn't use
351 C<AnyEvent> and is blocking, then your callback will complete before it
352 returns to the cron loop.
353  
354 However, if your callback is running asynchronously (and it really should),
355 then you can block the cron loop from responding to a L</"stop()"> request
356 until your job has completed:
357  
358 sub {
359 my $cv = shift;
360 $cv->begin;
361 do_something_asynchronous( cb => sub { $cv->end })
362 }
363  
364 Callbacks are called inside an C<eval> so if they throw an error, they
365 will warn, but won't cause the cron loop to exit.
366  
367 =head1 AUTHORS
368  
369 =over 4
370  
371 =item *
372  
373 Clinton Gormley <drtech@cpan.org>
374  
375 =item *
376  
377 Andy Gorman <agorman@cpan.org>
378  
379 =back
380  
381 =head1 COPYRIGHT AND LICENSE
382  
383 This software is copyright (c) 2013 by Clinton Gormley.
384  
385 This is free software; you can redistribute it and/or modify it under
386 the same terms as the Perl 5 programming language system itself.
387  
388 =cut
389  
390 __END__
↑ Summary