blib/lib/AnyEvent/Cron.pm

filter: Statementstmt 79.2% 84 / 106 Branchbran 27.7% 10 / 36 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 95.4% 21 / 22 Podpod 50.0% 1 / 2 totaltotal 67.2% 117 / 174 CC 22 SCAR 25.1
CC
22
Cov
65%
_schedule
31.5
_call_event
12.0
__ANON__
11.6
CRAP 12.3
↑ Summary
1 package AnyEvent::Cron;
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DateTime::Event::Cron;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.03';
12  
13  
14 # map of expiration formats to their respective time in seconds
15 2 my %_Expiration_Units = ( map(($_, 1), qw(s second seconds sec)),
16 map(($_, 60), qw(m minute minutes min)),
17 map(($_, 60*60), qw(h hour hours)),
18 map(($_, 60*60*24), qw(d day days)),
19 map(($_, 60*60*24*7), qw(w week weeks)),
20 map(($_, 60*60*24*30), qw(M month months)),
21 map(($_, 60*60*24*365), qw(y year years)) );
22  
23 has interval =>
24 1 ▶ ( is => 'rw' , isa => 'Int' , default => sub { 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 has verbose =>
27 1 ▶ ( is => 'rw' , isa => 'Bool' , default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 has debug =>
30 1 ▶ ( is => 'rw' , isa => 'Bool' , default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32 # TODO:
33 has ignore_floating =>
34 1 ▶ ( is => 'rw', isa => 'Bool' , default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
35  
36 has jobs =>
37 traits => ['Array'],
38 handles => {
39 add_job => 'push'
40 },
41 is => 'rw',
42 isa => 'ArrayRef' ,
43 1 ▶ default => sub { [ ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
44  
45 2 ;
46  
47  
48 1 ▶has timers => ( is => 'rw', isa => 'ArrayRef' , default => sub { [ ] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
49  
50 2 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 2 ▶sub add {
Subroutine add: called CC=1 SCAR=0.2
Pod documented
53 2 my $self = shift;
54 2 my ( $timespec, $cb , %args ) = @_;
55  
56 # try to create with crontab format
57 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 2 my $cron_event = DateTime::Event::Cron->new($timespec);
59 1 $self->add_job({
60 event => $cron_event,
61 cb => $cb,
62 %args
63 });
64 }
65 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.6
66 1 given ( $timespec ) {
67 # hour:minute per day
68 1 when( m{^(\d+):(\d+)$} ) {
69 0 my ( $hour, $minute ) = ( $1, $2 );
70 0 $self->add_job({
71 time => { hour => $hour, minute => $minute },
72 cb => $cb,
73 %args,
74 });
75 }
76 1 when( m{^\s*(\d+)\s*(\w+)} ) {
77 1 my ( $number, $unit ) = ( $1, $2 );
78 1 my $seconds = $number * $_Expiration_Units{$unit};
79 1 $self->add_job({
80 seconds => $seconds,
81 cb => $cb,
82 %args
83 });
84 # $self->create_interval_event( { second => $seconds, callback => $cb } );
85 }
86 0 default {
87 0 die 'time string format is not supported.';
88 }
89 }
90 2 };
91 2 return $self;
92 }
93  
94 0 ▶sub _call_event {
Subroutine _call_event: not called CC=3 SCAR=24.8
95 0 my ( $self, $e, $dt ) = @_;
96 0 ▶ unless ( $e->{triggered} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($e->{"triggered"}) 0 0 0
97 0 ▶ print $e->{name} . " triggered\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
98 0 $e->{callback}->( $self, $e, $dt );
99 0 $e->{triggered} = 1;
100 }
101 }
102  
103 3 ▶sub _schedule {
Subroutine _schedule: called CC=12 SCAR=34.5
104 3 my ($self,$job) = @_;
105  
106 3 AnyEvent->now_update();
107 3 my $now_epoch = AnyEvent->now;
108 3 my $next_epoch;
109 my $delay;
110 3 my $name = $job->{name};
111 3 my $debug = $job->{debug};
112  
113 3 ▶ if( $job->{event} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($job->{'event'}) { } 2 1 3
elsif ($job->{'seconds'}) { } 1 0 1
elsif ($job->{'time'}) { } 0 0 0
114 2 my $event = $job->{event};
115 2 $next_epoch = $event->next->epoch; # set next schedule time
116 2 $delay = $next_epoch - $now_epoch;
117 2 ▶ warn "delay:",$delay if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
118 }
119 elsif( $job->{seconds} ) {
120 1 $next_epoch = $now_epoch + $job->{seconds};
121 1 $delay = $next_epoch - $now_epoch;
122 1 ▶ warn "delay:",$delay if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
123 }
124 elsif( $job->{time} ) {
125 0 my $time = $job->{time};
126 0 my $now = DateTime->from_epoch( epoch => $now_epoch ); # depends on now
127 0 my $next = $now->clone;
128 0 $next->set( %$time );
129  
130 # now > the scheduled time
131 0 ▶ if( DateTime->compare( $now, $next ) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("DateTime"->compare($now, $next) == 1) 0 0 0
132 0 ▶ if( $time->{month} ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($time->{'month'}) { } 0 0 0
elsif ($time->{'day'}) { } 0 0 0
elsif ($time->{'hour'}) { } 0 0 0
elsif ($time->{'minute'}) { } 0 0 0
elsif ($time->{'second'}) { } 0 0 0
133 0 $next->add( years => 1 );
134 }
135 elsif( $time->{day} ) {
136 0 $next->add( months => 1 );
137 }
138 elsif( $time->{hour} ) {
139 0 $next->add( days => 1 );
140 }
141 elsif( $time->{minute} ) {
142 0 $next->add( hours => 1 );
143 }
144 elsif( $time->{second} ) {
145 0 $next->add( minutes => 1 );
146 }
147 else {
148 0 die 'unknown spec';
149 }
150 }
151 }
152  
153 3 $job->{next}{ $next_epoch } = 1;
154 $job->{watchers}{$next_epoch} = AnyEvent->timer(
155 after => $delay,
156 2 ▶ cb => sub {
Subroutine __ANON__: called CC=8 SCAR=24.5
157 2 $self->{_cv}->begin;
158 2 delete $job->{watchers}{$next_epoch};
159  
160 2 ▶ $self->_schedule($job) unless $job->{once};
Branch: 1 branch
BranchTrueFalseTotal
unless $job->{"once"} 1 1 2
161  
162 2 ▶ if ( $job->{single} && $job->{running}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{'single'} and $job->{'running'}++) { } 0 2 2
Condition33%1/3
$job->{"single"} and $job->{"running"}++
!ll&&!rl&&r
200
MC/DC0%0/2
$job->{"single"} and $job->{"running"}++
$job->{"single"} $job->{"running"}++
Truth table33%1/3
A and B
A= $job->{"single"}B= $job->{"running"}++
ABresult
0X0
100
111
163 0 ▶ print STDERR "Skipping job '$name' - still running\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
164 if $debug;
165 }
166 else {
167 2 ▶ eval { $job->{cb}->( $self->{_cv}, $job ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $job->{"cb"}->($self->{"_cv"}, $job); 1 } } or warn $@ || "Unknown error" 2 0 2
Condition0%0/2
$@ || 'Unknown error'
l!l
00
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
168 or warn $@ || 'Unknown error';
169 2 delete $job->{running};
170 2 ▶ print STDERR "Finished job '$name'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
171 if $debug;
172 }
173 2 $self->{_cv}->end;
174 }
175 3 );
176 }
177  
178 1 ▶sub run {
Subroutine run: called CC=2 SCAR=6.9
Pod undocumented
179 1 my $self = shift;
180 1 my $cv = $self->{_cv} = AnyEvent->condvar;
181 1 for my $job ( @{ $self->jobs } ) {
182 2 $self->_schedule($job);
183 }
184 }
185  
186  
187 2 1;
188 __END__
↑ Summary