DC/Sched.pm untested

filter: Statementstmt 0.0% 0 / 32 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 11 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 62 CC 5 SCAR 22.3
CC
5
Cov
0%
new
20.0
_timeout
6.0
_sched
2.0
CRAP 9.3
1 # -*- perl -*-
2  
3 # Copyright (c) 2009 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2009-Mar-27 11:47 (EDT)
6 # Function: run things periodically
7 #
8 # $Id: Sched.pm,v 1.4 2010/09/07 15:31:29 jaw Exp $
9  
10 package AC::DC::Sched;
11 use AC::DC::Debug 'sched';
12 use Carp 'carp';
13 use strict;
14  
15 our @ISA = qw(AC::DC::IO);
16  
17 sub new {
18 my $class = shift;
19 my $p = { @_ };
20 # { info, time, freq, phi, func, args }
21  
22 my $me = bless {
23 sched => $p,
24 }, $class;
25  
26 $p->{info} ||= 'scheduled function';
27 $p->{phi} = rand($p->{freq}) if $p->{freq} && !defined($p->{phi});
28 $p->{time} ||= $p->{freq} + $p->{phi} + $^T if $p->{freq} && !$p->{time};
29 carp "cannot schedule, no time, no freq.\n" unless $p->{time};
30  
31 $me->{info} = $p->{info};
32  
33 debug("installing scheduled func ($me->{info})");
34 $me->_sched();
35  
36 return $me;
37 }
38  
39 sub _sched {
40 my $me = shift;
41  
42 $me->timeout_abs( $me->{sched}{time} );
43 }
44  
45 sub _resched {
46 my $me = shift;
47 while( $me->{sched}{time} < $^T ){ $me->{sched}{time} += $me->{sched}{freq} }
48 $me->_sched();
49 }
50  
51 sub _timeout {
52 my $me = shift;
53  
54 # run specified func
55 debug("running scheduled func ($me->{info})");
56 $me->{sched}{func}->($me->{sched}{args});
57 $me->_resched() if $me->{sched}{freq};
58 }
59  
60  
61 1;