blib/lib/IO/Async/Timer/Periodic.pm

filter: Statementstmt 98.5% 70 / 71 Branchbran 80.9% 34 / 42 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 91.9% 125 / 136 CC 24 SCAR 20.2
CC
24
Cov
92%
configure
13.7
_reschedule
7.0
__ANON__
5.0
CRAP 7.6
« blib/lib/IO/Async/LoopTests.pm ↑ Summary blib/lib/IO/Async/OS.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2009-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Timer::Periodic 0.805;
7  
8 4 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use base qw( IO::Async::Timer );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 C<IO::Async::Timer::Periodic> - event callback at regular intervals
17  
18 =head1 SYNOPSIS
19  
20 =for highlighter language=perl
21  
22 use IO::Async::Timer::Periodic;
23  
24 use IO::Async::Loop;
25 my $loop = IO::Async::Loop->new;
26  
27 my $timer = IO::Async::Timer::Periodic->new(
28 interval => 60,
29  
30 on_tick => sub {
31 print "You've had a minute\n";
32 },
33 );
34  
35 $timer->start;
36  
37 $loop->add( $timer );
38  
39 $loop->run;
40  
41 =head1 DESCRIPTION
42  
43 This subclass of L<IO::Async::Timer> implements repeating events at regular
44 clock intervals. The timing may or may not be subject to how long it takes the
45 callback to execute. Iterations may be rescheduled runs at fixed regular
46 intervals beginning at the time the timer was started, or by a fixed delay
47 after the previous code has finished executing.
48  
49 For a C<Timer> object that only runs a callback once, after a given delay, see
50 instead L<IO::Async::Timer::Countdown>. A Countdown timer can also be used to
51 create repeating events that fire at a fixed delay after the previous event
52 has finished processing. See als the examples in
53 C<IO::Async::Timer::Countdown>.
54  
55 =cut
56  
57 =head1 EVENTS
58  
59 The following events are invoked, either using subclass methods or CODE
60 references in parameters:
61  
62 =head2 on_tick
63  
64 Invoked on each interval of the timer.
65  
66 =cut
67  
68 =head1 PARAMETERS
69  
70 The following named parameters may be passed to C<new> or C<configure>:
71  
72 =head2 on_tick => CODE
73  
74 CODE reference for the C<on_tick> event.
75  
76 =head2 interval => NUM
77  
78 The interval in seconds between invocations of the callback or method. Cannot
79 be changed if the timer is running.
80  
81 =head2 first_interval => NUM
82  
83 Optional. If defined, the interval in seconds after calling the C<start>
84 method before the first invocation of the callback or method. Thereafter, the
85 regular C<interval> will be used. If not supplied, the first interval will be
86 the same as the others.
87  
88 Even if this value is zero, the first invocation will be made asynchronously,
89 by the containing C<Loop> object, and not synchronously by the C<start> method
90 itself.
91  
92 =head2 reschedule => STRING
93  
94 Optional. Must be one of C<hard>, C<skip> or C<drift>. Defines the algorithm
95 used to reschedule the next invocation.
96  
97 C<hard> schedules each iteration at the fixed interval from the previous
98 iteration's schedule time, ensuring a regular repeating event.
99  
100 C<skip> schedules similarly to C<hard>, but skips over times that have already
101 passed. This matters if the duration is particularly short and there's a
102 possibility that times may be missed, or if the entire process is stopped and
103 resumed by C<SIGSTOP> or similar.
104  
105 C<drift> schedules each iteration at the fixed interval from the time that the
106 previous iteration's event handler returns. This allows it to slowly drift over
107 time and become desynchronised with other events of the same interval or
108 multiples/fractions of it.
109  
110 Once constructed, the timer object will need to be added to the C<Loop> before
111 it will work. It will also need to be started by the C<start> method.
112  
113 =cut
114  
115 sub _init
116 {
117 15 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
118 15 $self->SUPER::_init( @_ );
119  
120 15 $self->{reschedule} = "hard";
121 }
122  
123 sub configure
124 {
125 33 ▶ my $self = shift;
Subroutine configure: called CC=13 SCAR=26.2
Pod documented
126 33 my %params = @_;
127  
128 33 ▶ if( exists $params{on_tick} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"on_tick"}) 5 28 33
129 5 my $on_tick = delete $params{on_tick};
130 5 ▶ ref $on_tick or croak "Expected 'on_tick' as a reference";
Branch: 1 branch
BranchTrueFalseTotal
ref $on_tick or croak("Expected 'on_tick' as a reference") 0 5 5
131  
132 5 $self->{on_tick} = $on_tick;
133 5 undef $self->{cb}; # Will be lazily constructed when needed
134 }
135  
136 33 ▶ if( exists $params{interval} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"interval"}) 25 8 33
137 25 ▶ $self->is_running and croak "Cannot configure 'interval' of a running timer\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_running and croak("Cannot configure 'interval' of a running timer\n") 1 24 25
138  
139 24 my $interval = delete $params{interval};
140 24 ▶ $interval > 0 or croak "Expected a 'interval' as a positive number";
Branch: 1 branch
BranchTrueFalseTotal
$interval > 0 or croak("Expected a 'interval' as a positive number") 0 24 24
141  
142 24 $self->{interval} = $interval;
143 }
144  
145 32 ▶ if( exists $params{first_interval} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"first_interval"}) 1 31 32
146 1 ▶ $self->is_running and croak "Cannot configure 'first_interval' of a running timer\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_running and croak("Cannot configure 'first_interval' of a running timer\n") 0 1 1
147  
148 1 my $first_interval = delete $params{first_interval};
149 1 ▶ $first_interval >= 0 or croak "Expected a 'first_interval' as a non-negative number";
Branch: 1 branch
BranchTrueFalseTotal
$first_interval >= 0 or croak("Expected a 'first_interval' as a non-negative number") 0 1 1
150  
151 1 $self->{first_interval} = $first_interval;
152 }
153  
154 32 ▶ if( exists $params{reschedule} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"reschedule"}) 2 30 32
155 2 ▶ my $resched = delete $params{reschedule} || "hard";
Condition50%1/2
delete $params{'reschedule'} || 'hard'
l!l
20
MC/DC0%0/1
delete $params{'reschedule'} || 'hard'
delete $params{'reschedule'}
Truth table50%1/2
A
A= delete $params{'reschedule'}
Aresult
11
00
156 2 ▶ grep { $_ eq $resched } qw( hard skip drift ) or
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $resched;} "hard", "skip", "drift" or croak("Expected 'reschedule' to be one of hard, skip, drift") 0 2 2
157 croak "Expected 'reschedule' to be one of hard, skip, drift";
158  
159 2 $self->{reschedule} = $resched;
160 }
161  
162 32 ▶ unless( $self->can_event( 'on_tick' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can_event("on_tick")) 0 32 32
163 0 croak 'Expected either a on_tick callback or an ->on_tick method';
164 }
165  
166 32 $self->SUPER::configure( %params );
167 }
168  
169 sub _reschedule
170 {
171 46 ▶ my $self = shift;
Subroutine _reschedule: called CC=7 SCAR=19.5
172  
173 46 my $now = $self->loop->time;
174 46 my $resched = $self->{reschedule};
175  
176 my $next_interval = $self->{is_first} && defined $self->{first_interval}
177 46 ▶ ? $self->{first_interval} : $self->{interval};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'is_first'} && defined $self->{'first_interval'} ? : 1 45 46
Condition100%3/3
$self->{"is_first"} and defined $self->{"first_interval"}
!ll&&!rl&&r
29161
MC/DC100%2/2
$self->{"is_first"} and defined $self->{"first_interval"}
$self->{"is_first"} defined $self->{"first_interval"}
Truth table100%3/3
A and B
A= $self->{"is_first"}B= defined $self->{"first_interval"}
ABresult
0X0
100
111
178 46 delete $self->{is_first};
179  
180 46 ▶ if( !defined $self->{next_time} ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $self->{'next_time'}) { } 17 29 46
elsif ($resched eq 'hard') { } 25 4 29
elsif ($resched eq 'skip') { } 2 2 4
elsif ($resched eq 'drift') { } 2 0 2
181 17 $self->{next_time} = $now + $next_interval;
182 }
183 elsif( $resched eq "hard" ) {
184 25 $self->{next_time} += $next_interval;
185 }
186 elsif( $resched eq "skip" ) {
187 # How many ticks are needed?
188 2 my $ticks = POSIX::ceil( ( $now - $self->{next_time} ) / $next_interval );
189 # $self->{last_ticks} = $ticks;
190 2 $self->{next_time} += $next_interval * $ticks;
191 }
192 elsif( $resched eq "drift" ) {
193 2 $self->{next_time} = $now + $next_interval;
194 }
195  
196 46 $self->SUPER::start;
197 }
198  
199 sub start
200 {
201 40 ▶ my $self = shift;
Subroutine start: called CC=2 SCAR=6.9
Pod documented
202  
203 40 $self->{is_first} = 1;
204  
205 # Only actually define a time if we've got a loop; otherwise it'll just
206 # become start-pending. We'll calculate it properly when it gets added to
207 # the Loop
208 40 ▶ if( $self->loop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->loop) { } 17 23 40
209 17 $self->_reschedule;
210 }
211 else {
212 23 $self->SUPER::start;
213 }
214 }
215  
216 sub stop
217 {
218 27 ▶ my $self = shift;
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
219 27 $self->SUPER::stop;
220  
221 27 undef $self->{next_time};
222 }
223  
224 sub _make_cb
225 {
226 15 ▶ my $self = shift;
Subroutine _make_cb: called CC=1 SCAR=0.0
227  
228 return $self->_capture_weakself( sub {
229 30 ▶ my $self = shift or return;
Subroutine __ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 30 30
230  
231 30 undef $self->{id};
232  
233 30 ▶ my $ok = eval { $self->invoke_event( on_tick => ); 1 } or
Branch: 1 branch
BranchTrueFalseTotal
my $ok = eval { do { $self->invoke_event("on_tick"); 1 } } or my $e = $@ 2 28 30
234 my $e = $@;
235  
236 # detect ->stop
237 30 ▶ $self->_reschedule if defined $self->{next_time};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"next_time"} 29 1 30
238  
239 30 ▶ die $e if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
if not $ok 2 28 30
240 15 } );
241 }
242  
243 sub _make_enqueueargs
244 {
245 46 ▶ my $self = shift;
Subroutine _make_enqueueargs: called CC=1 SCAR=0.0
246  
247 46 return at => $self->{next_time};
248 }
249  
250 =head1 AUTHOR
251  
252 Paul Evans <leonerd@leonerd.org.uk>
253  
254 =cut
255  
256 4 0x55AA;
« blib/lib/IO/Async/LoopTests.pm ↑ Summary blib/lib/IO/Async/OS.pm »