| 1 | package Schedule::Poll; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use List::Util 'max'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use POSIX qw/ floor /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Schedule::Poll - Evenly schedule recurring events with various intervals | ||||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | =cut | ||||||||||
| 17 | |||||||||||
| 18 | 3 | our $VERSION = '0.02'; | |||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | use Schedule::Poll; | ||||||||||
| 25 | |||||||||||
| 26 | # Let's run a few things every 3 seconds, | ||||||||||
| 27 | # and some things every 6 seconds | ||||||||||
| 28 | |||||||||||
| 29 | my $config = { | ||||||||||
| 30 | foo => 3, | ||||||||||
| 31 | bar => 3, | ||||||||||
| 32 | baz => 3, | ||||||||||
| 33 | zip => 6, | ||||||||||
| 34 | zoo => 6, | ||||||||||
| 35 | zat => 6 | ||||||||||
| 36 | }; | ||||||||||
| 37 | |||||||||||
| 38 | my $poll = Schedule::Poll->new( $config ); | ||||||||||
| 39 | |||||||||||
| 40 | while(1) { | ||||||||||
| 41 | |||||||||||
| 42 | if (my $aref = $poll->which ) { | ||||||||||
| 43 | |||||||||||
| 44 | for my $each (@$aref) { | ||||||||||
| 45 | |||||||||||
| 46 | print "$each fired!\n"; | ||||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | sleep 1; | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | =head1 METHODS | ||||||||||
| 54 | |||||||||||
| 55 | =head2 new | ||||||||||
| 56 | |||||||||||
| 57 | Constructor. Accepts a hashref with the values being an interval in seconds. Each interval used should be a divisor of 86400. | ||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | my $poll = Schedule::Poll->new({ | ||||||||||
| 61 | |||||||||||
| 62 | foo => 300 # 5 minutes | ||||||||||
| 63 | bar => 600 # 10 minutes | ||||||||||
| 64 | baz => 5 # 5 seconds | ||||||||||
| 65 | |||||||||||
| 66 | }); | ||||||||||
| 67 | |||||||||||
| 68 | =cut | ||||||||||
| 69 | |||||||||||
| 70 | 3 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=16 SCAR=27.7
Pod
documented
| |||||||||||
| 71 | 3 | my $class = shift; | |||||||||
| 72 | 3 | my $self = { }; | |||||||||
| 73 | 3 | $self->{config} = $_[0]; | |||||||||
| 74 | 3 | ▶ | croak "Missing data" unless exists $self->{config}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 3 | ▶ | croak "data is not a hashref" unless ref $self->{config} eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | |||||||||||
| 77 | 3 | my $config = $self->{config}; | |||||||||
| 78 | 3 | my @intervals; | |||||||||
| 79 | |||||||||||
| 80 | 3 | for my $interval (keys %{$config}) { | |||||||||
| 81 | 9 | ▶ | croak "$config->{$interval} is not an even divisor of 86400" if (86400 % $config->{$interval} != 0); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 9 | push (@intervals,$config->{$interval}); | |||||||||
| 83 | } | ||||||||||
| 84 | 3 | my $max = max(@intervals); | |||||||||
| 85 | 3 | $self->{max} = $max; | |||||||||
| 86 | |||||||||||
| 87 | 3 | my %groups = ( ); | |||||||||
| 88 | 3 | for my $each (keys %{$config}) { | |||||||||
| 89 | 9 | push ( @{$groups{ $config->{$each} } }, $each); | |||||||||
| 90 | } | ||||||||||
| 91 | 3 | my %schedule; | |||||||||
| 92 | 3 | undef @intervals; | |||||||||
| 93 | 3 | undef $config; | |||||||||
| 94 | 3 | undef $self->{config}; | |||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | 3 | for my $interval (keys %groups) { | |||||||||
| 98 | # Count of members in each group: | ||||||||||
| 99 | 5 | my $members = scalar @{$groups{$interval}}; | |||||||||
| 100 | |||||||||||
| 101 | 5 | my $iter = 1; | |||||||||
| 102 | |||||||||||
| 103 | 5 | ▶ | if ($members/$interval >= 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | |||||||||||
| 105 | # > 1 req per second. Loop | ||||||||||
| 106 | # through the members and assign | ||||||||||
| 107 | # them to slots in the interval | ||||||||||
| 108 | # | ||||||||||
| 109 | # Ex: | ||||||||||
| 110 | # members = 7 | ||||||||||
| 111 | # interval = 5 | ||||||||||
| 112 | # | ||||||||||
| 113 | # 1 2 3 4 5 | ||||||||||
| 114 | # --------- | ||||||||||
| 115 | # | | | | | | ||||||||||
| 116 | # | | | ||||||||||
| 117 | # | ||||||||||
| 118 | # With the above example, the 1st and 2nd second slots in that interval | ||||||||||
| 119 | # will contain 2 requests, the remaining slots will have 1 | ||||||||||
| 120 | |||||||||||
| 121 | |||||||||||
| 122 | # First, We need to determine how many times this | ||||||||||
| 123 | # interval group will repeat given a max interval. | ||||||||||
| 124 | |||||||||||
| 125 | 1 | my $sets = $max/$interval; | |||||||||
| 126 | |||||||||||
| 127 | 1 | ▶ | while ($members >= 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 4 | ▶ | $iter = 1 if $iter > $interval; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | |||||||||||
| 130 | 4 | my $set = 0; | |||||||||
| 131 | |||||||||||
| 132 | 4 | ▶ | while ($set < $sets) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | |||||||||||
| 134 | 8 | my $slot; | |||||||||
| 135 | 8 | ▶ | if ($set == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 4 | $slot = $iter; | |||||||||
| 137 | } | ||||||||||
| 138 | else { | ||||||||||
| 139 | 4 | $slot = ($set * $interval) + $iter; | |||||||||
| 140 | } | ||||||||||
| 141 | 8 | push ( @{$schedule{$slot}}, $groups{$interval}[$members -1]); | |||||||||
| 142 | 8 | $set++; | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 4 | $iter++; | |||||||||
| 146 | 4 | $members--; | |||||||||
| 147 | } | ||||||||||
| 148 | } | ||||||||||
| 149 | else { | ||||||||||
| 150 | |||||||||||
| 151 | # < 1 requests per sec. We can spread | ||||||||||
| 152 | # the reqests out over multiple seconds | ||||||||||
| 153 | # | ||||||||||
| 154 | # Ex: | ||||||||||
| 155 | # requests: 3 | ||||||||||
| 156 | # interval: 9 | ||||||||||
| 157 | # | ||||||||||
| 158 | # 1 2 3 4 5 6 7 8 9 | ||||||||||
| 159 | # ----------------- | ||||||||||
| 160 | # | | | | ||||||||||
| 161 | # | ||||||||||
| 162 | # .. so 1 request for every 3 seconds. | ||||||||||
| 163 | |||||||||||
| 164 | 4 | my $rate = floor 1/($members/$interval); | |||||||||
| 165 | |||||||||||
| 166 | |||||||||||
| 167 | 4 | my $sets = $max/$interval; | |||||||||
| 168 | 4 | ▶ | while ($members >=1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 5 | ▶ | $iter = 1 if $iter >= $interval; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 5 | my $set = 0; | |||||||||
| 171 | 5 | ▶ | while ($set < $sets) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 124 | my $slot; | |||||||||
| 173 | 124 | ▶ | if ($set == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 5 | $slot = $iter + ($rate -1); | |||||||||
| 175 | |||||||||||
| 176 | } | ||||||||||
| 177 | else { | ||||||||||
| 178 | 119 | $slot = ($set * $interval) + ($iter + ($rate-1)); | |||||||||
| 179 | } | ||||||||||
| 180 | 124 | push ( @{$schedule{$slot}}, $groups{$interval}[$members -1]); | |||||||||
| 181 | 124 | $set++; | |||||||||
| 182 | } | ||||||||||
| 183 | 5 | $iter += $rate; | |||||||||
| 184 | 5 | $members--; | |||||||||
| 185 | } | ||||||||||
| 186 | |||||||||||
| 187 | } | ||||||||||
| 188 | } | ||||||||||
| 189 | 3 | $self->{schedule} = \%schedule; | |||||||||
| 190 | 3 | bless $self,$class; | |||||||||
| 191 | }; | ||||||||||
| 192 | |||||||||||
| 193 | |||||||||||
| 194 | 6 | ▶ | sub current { | ||||||||
|
Subroutine
current: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||
| 195 | 6 | my $self = shift; | |||||||||
| 196 | |||||||||||
| 197 | 6 | my $max = $self->{max}; | |||||||||
| 198 | 6 | my @time = localtime(); | |||||||||
| 199 | 6 | my $second = ($time[2] * 3600) + ($time[1] * 60) + $time[0]; | |||||||||
| 200 | 6 | my $y=0; | |||||||||
| 201 | 6 | ▶ | if ($second > $max) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 202 | 6 | ▶ | while ($y < $second) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | 83679 | $y += $max; | |||||||||
| 204 | } | ||||||||||
| 205 | 6 | return $second - ($y - $max); | |||||||||
| 206 | } else { | ||||||||||
| 207 | 0 | return $second; | |||||||||
| 208 | } | ||||||||||
| 209 | |||||||||||
| 210 | } | ||||||||||
| 211 | |||||||||||
| 212 | =head2 which | ||||||||||
| 213 | |||||||||||
| 214 | Returns an arrary reference containing the items for that current tick interval. | ||||||||||
| 215 | |||||||||||
| 216 | $poll->which; | ||||||||||
| 217 | |||||||||||
| 218 | |||||||||||
| 219 | =cut | ||||||||||
| 220 | |||||||||||
| 221 | 6 | ▶ | sub which { | ||||||||
|
Subroutine
which: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 222 | 6 | my $self = shift; | |||||||||
| 223 | 6 | my $current = $self->current; | |||||||||
| 224 | 6 | my @who; | |||||||||
| 225 | 6 | ▶ | if (exists $self->{schedule}{$current}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 226 | 6 | for my $each (@{$self->{schedule}{$current}}) { | |||||||||
| 227 | 10 | push(@who,$each); | |||||||||
| 228 | } | ||||||||||
| 229 | 6 | return \@who; | |||||||||
| 230 | } | ||||||||||
| 231 | 0 | return 0; | |||||||||
| 232 | } | ||||||||||
| 233 | |||||||||||
| 234 | =head2 Examples | ||||||||||
| 235 | |||||||||||
| 236 | $href = { | ||||||||||
| 237 | a => 3, | ||||||||||
| 238 | b => 3, | ||||||||||
| 239 | c => 3 | ||||||||||
| 240 | }; | ||||||||||
| 241 | |||||||||||
| 242 | Timeline: | ||||||||||
| 243 | interval | 1 2 3 4 5 6 | ||||||||||
| 244 | ---------+------------------ | ||||||||||
| 245 | key | a b c a b c | ||||||||||
| 246 | |||||||||||
| 247 | |||||||||||
| 248 | $href = { | ||||||||||
| 249 | a => 3, | ||||||||||
| 250 | b => 3, | ||||||||||
| 251 | c => 3, | ||||||||||
| 252 | d => 6, | ||||||||||
| 253 | e => 6, | ||||||||||
| 254 | f => 6 | ||||||||||
| 255 | }; | ||||||||||
| 256 | |||||||||||
| 257 | Timeline: | ||||||||||
| 258 | interval | 1 2 3 4 5 6 7 8 9 10 11 12 | ||||||||||
| 259 | ---------+-------------------------------------- | ||||||||||
| 260 | key | b a c b a c b a c b a c | ||||||||||
| 261 | | d e f d e f | ||||||||||
| 262 | |||||||||||
| 263 | |||||||||||
| 264 | =head1 AUTHOR | ||||||||||
| 265 | |||||||||||
| 266 | Michael Kroher, C<< <michael at kroher.net> >> | ||||||||||
| 267 | |||||||||||
| 268 | =cut | ||||||||||
| 269 | 3 | 1; # End of Schedule::Poll | |||||||||