| 1 | package Zing::Logic::Timer; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 56 | ▶ | use 5.014; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 56 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 56 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 56 | ▶ | use registry 'Zing::Types'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 56 | ▶ | use routines; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 56 | ▶ | use Data::Object::Class; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 56 | ▶ | use Data::Object::ClassHas; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 56 | extends 'Zing::Logic'; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 56 | ▶ | use Zing::Flow; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 56 | ▶ | use Zing::Queue; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 56 | ▶ | use Time::Crontab; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 56 | ▶ | use Time::Piece; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 56 | our $VERSION = '0.26'; # VERSION | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # ATTRIBUTES | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 56 | has 'on_timer' => ( | |||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||
| 28 | isa => 'CodeRef', | ||||||||||||||||||||
| 29 | new => 1, | ||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | ▶ | fun new_on_timer($self) { | ||||||||||||||||||
|
Subroutine
new_on_timer: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 33 | 0 | $self->can('handle_timer_event') | |||||||||||||||||||
| 34 | 56 | } | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 56 | has 'schedules' => ( | |||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||
| 38 | isa => 'ArrayRef[Schedule]', | ||||||||||||||||||||
| 39 | new => 1 | ||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | ▶ | fun new_schedules($self) { | ||||||||||||||||||
|
Subroutine
new_schedules: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 43 | 0 | $self->process->schedules | |||||||||||||||||||
| 44 | 56 | } | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 56 | has 'relays' => ( | |||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||
| 48 | isa => 'HashRef[Queue]', | ||||||||||||||||||||
| 49 | new => 1 | ||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | fun new_relays($self) { | ||||||||||||||||||
|
Subroutine
new_relays: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 53 | 0 | +{map {$_, Zing::Queue->new(name => $_)} map @{$$_[1]}, @{$self->schedules}} | |||||||||||||||||||
| 54 | 56 | } | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | # SHIMS | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | sub _tick { | ||||||||||||||||||||
| 59 | 0 | ▶ | localtime->truncate(to => 'minute')->epoch | ||||||||||||||||||
|
Subroutine
_tick: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | sub _time { | ||||||||||||||||||||
| 63 | CORE::time | ||||||||||||||||||||
| 64 | 0 | ▶ | } | ||||||||||||||||||
|
Subroutine
_time: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | # METHODS | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | ▶ | method flow() { | ||||||||||||||||||
|
Subroutine
flow: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 69 | 0 | my $step_0 = $self->next::method; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | ▶ | my $step_1 = Zing::Flow->new( | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 72 | name => 'on_timer', | ||||||||||||||||||||
| 73 | 0 | code => fun($step, $loop) { $self->trace('on_timer')->($self) } | |||||||||||||||||||
| 74 | ); | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | $step_0->append($step_1); | |||||||||||||||||||
| 77 | 0 | $step_0 | |||||||||||||||||||
| 78 | 56 | } | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 56 | my $aliases = { | |||||||||||||||||||
| 81 | # at 00:00 on day-of-month 1 in january | ||||||||||||||||||||
| 82 | '@yearly' => '0 0 1 1 *', | ||||||||||||||||||||
| 83 | # at 00:00 on day-of-month 1 in january | ||||||||||||||||||||
| 84 | '@annually' => '0 0 1 1 *', | ||||||||||||||||||||
| 85 | # at 00:00 on day-of-month 1 | ||||||||||||||||||||
| 86 | '@monthly' => '0 0 1 * *', | ||||||||||||||||||||
| 87 | # at 00:00 on monday | ||||||||||||||||||||
| 88 | '@weekly' => '0 0 * * 1', | ||||||||||||||||||||
| 89 | # at 00:00 on saturday | ||||||||||||||||||||
| 90 | '@weekend' => '0 0 * * 6', | ||||||||||||||||||||
| 91 | # at 00:00 every day | ||||||||||||||||||||
| 92 | '@daily' => '0 0 * * *', | ||||||||||||||||||||
| 93 | # at minute 0 every hour | ||||||||||||||||||||
| 94 | '@hourly' => '0 * * * *', | ||||||||||||||||||||
| 95 | # at every minute | ||||||||||||||||||||
| 96 | '@minute' => '* * * * *', | ||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | ▶ | method handle_timer_event($name) { | ||||||||||||||||||
|
Subroutine
handle_timer_event: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||
| 100 | 0 | my $process = $self->process; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | my $_tick = _tick; | |||||||||||||||||||
| 103 | 0 | my $_time = _time; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | ▶ | for (my $i = 0; $i < @{$self->schedules}; $i++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | # run each schedule initially, and then once per minute | ||||||||||||||||||||
| 107 | next if $_tick == ( | ||||||||||||||||||||
| 108 | 0 | ▶ | $self->{tick}[$i] || 0 | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->{'tick'}[$i] || 0
MC/DC0%0/1
$self->{'tick'}[$i] || 0
$self->{'tick'}[$i]
Truth table0%0/2
A
A= $self->{'tick'}[$i]
| |||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | # unpack schedule | ||||||||||||||||||||
| 112 | 0 | my $schedule = $self->schedules->[$i]; | |||||||||||||||||||
| 113 | 0 | ▶ | my $frequency = $schedule->[0] || ''; | ||||||||||||||||||
|
Condition0%0/2
$schedule->[0] || ''
MC/DC0%0/1
$schedule->[0] || ''
$schedule->[0]
Truth table0%0/2
A
A= $schedule->[0]
| |||||||||||||||||||||
| 114 | 0 | ▶ | my $cronexpr = $aliases->{$frequency} || $frequency; | ||||||||||||||||||
|
Condition0%0/3
$aliases->{$frequency} || $frequency
MC/DC0%0/2
$aliases->{$frequency} || $frequency
$aliases->{$frequency}
$frequency
Truth table0%0/3
A || B
A= $aliases->{$frequency}B= $frequency
| |||||||||||||||||||||
| 115 | 0 | my $queues = $schedule->[1]; | |||||||||||||||||||
| 116 | 0 | my $message = $schedule->[2]; | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | # cached crontab object | ||||||||||||||||||||
| 119 | 0 | ▶ | my $cron = $self->{cron}[$i] ||= Time::Crontab->new($cronexpr); | ||||||||||||||||||
|
Condition0%0/3
$self->{"cron"}[$i] ||= "Time::Crontab"->new($cronexpr)
MC/DC0%0/2
$self->{"cron"}[$i] ||= "Time::Crontab"->new($cronexpr)
$self->{"cron"}[$i]
"Time::Crontab"->new($cronexpr)
Truth table0%0/3
A ||= B
A= $self->{"cron"}[$i]B= "Time::Crontab"->new($cronexpr)
| |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # next unless our times is here! | ||||||||||||||||||||
| 122 | 0 | ▶ | next unless $cron->match($_time); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | # record tick for once-per-minute excution | ||||||||||||||||||||
| 125 | 0 | $self->{tick}[$i] = $_tick; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | # deliver messages to queues | ||||||||||||||||||||
| 128 | 0 | for my $name (@$queues) { | |||||||||||||||||||
| 129 | 0 | $self->relays->{$name}->send($message); | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | return $self; | |||||||||||||||||||
| 134 | 56 | } | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 56 | 1; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =encoding utf8 | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head1 NAME | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Zing::Logic::Timer - Timer Logic | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =head1 ABSTRACT | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | Timer Process Logic Chain | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =head1 SYNOPSIS | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | package Process; | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | use parent 'Zing::Process'; | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | sub schedules { | ||||||||||||||||||||
| 159 | [['@minute', ['tasks'], { do => 1 }]] | ||||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | package main; | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | use Zing::Logic::Timer; | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | my $logic = Zing::Logic::Timer->new(process => Process->new); | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | # $logic->execute; | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =cut | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =head1 DESCRIPTION | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | This package provides the logic (or logic chain) to be executed by the timer | ||||||||||||||||||||
| 175 | process event-loop. | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =cut | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =head1 INHERITS | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | This package inherits behaviors from: | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | L<Zing::Logic> | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =head1 LIBRARIES | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | This package uses type constraints from: | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | L<Zing::Types> | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | This package has the following attributes: | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =head2 interupt | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | interupt(Interupt) | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | This attribute is read-only, accepts C<(Interupt)> values, and is optional. | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =head2 on_perform | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | on_perform(CodeRef) | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | =cut | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =head2 on_receive | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | on_receive(CodeRef) | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =head2 on_register | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | on_register(CodeRef) | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =head2 on_reset | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | on_reset(CodeRef) | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =cut | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | =head2 on_suicide | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | on_suicide(CodeRef) | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | =cut | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | =head2 on_timer | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | on_timer(CodeRef) | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | =cut | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | =head2 process | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | process(Process) | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | This attribute is read-only, accepts C<(Process)> values, and is required. | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | =head2 relays | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | relays(HashRef[Queue]) | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | This attribute is read-only, accepts C<(HashRef[Queue])> values, and is optional. | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | =cut | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | =head2 schedules | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | schedules(ArrayRef[Schedule]) | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | This attribute is read-only, accepts C<(ArrayRef[Schedule])> values, and is optional. | ||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | =head1 METHODS | ||||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | This package implements the following methods: | ||||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | =cut | ||||||||||||||||||||
| 286 | |||||||||||||||||||||
| 287 | =head2 flow | ||||||||||||||||||||
| 288 | |||||||||||||||||||||
| 289 | flow() : Flow | ||||||||||||||||||||
| 290 | |||||||||||||||||||||
| 291 | The flow method builds and returns the logic flow for the process event-loop. | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | =over 4 | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | =item flow example #1 | ||||||||||||||||||||
| 296 | |||||||||||||||||||||
| 297 | # given: synopsis | ||||||||||||||||||||
| 298 | |||||||||||||||||||||
| 299 | my $flow = $logic->flow; | ||||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | =back | ||||||||||||||||||||
| 302 | |||||||||||||||||||||
| 303 | =cut | ||||||||||||||||||||
| 304 | |||||||||||||||||||||
| 305 | =head2 signals | ||||||||||||||||||||
| 306 | |||||||||||||||||||||
| 307 | signals() : HashRef | ||||||||||||||||||||
| 308 | |||||||||||||||||||||
| 309 | The signals method builds and returns the process signal handlers. | ||||||||||||||||||||
| 310 | |||||||||||||||||||||
| 311 | =over 4 | ||||||||||||||||||||
| 312 | |||||||||||||||||||||
| 313 | =item signals example #1 | ||||||||||||||||||||
| 314 | |||||||||||||||||||||
| 315 | # given: synopsis | ||||||||||||||||||||
| 316 | |||||||||||||||||||||
| 317 | my $signals = $logic->signals; | ||||||||||||||||||||
| 318 | |||||||||||||||||||||
| 319 | =back | ||||||||||||||||||||
| 320 | |||||||||||||||||||||
| 321 | =cut | ||||||||||||||||||||
| 322 | |||||||||||||||||||||
| 323 | =head1 AUTHOR | ||||||||||||||||||||
| 324 | |||||||||||||||||||||
| 325 | Al Newkirk, C<awncorp@cpan.org> | ||||||||||||||||||||
| 326 | |||||||||||||||||||||
| 327 | =head1 LICENSE | ||||||||||||||||||||
| 328 | |||||||||||||||||||||
| 329 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||||||||||||||||||||
| 330 | |||||||||||||||||||||
| 331 | This is free software; you can redistribute it and/or modify it under the terms | ||||||||||||||||||||
| 332 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||||||||||||||||||||
| 333 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||||||||||||||||||||
| 334 | |||||||||||||||||||||
| 335 | =head1 PROJECT | ||||||||||||||||||||
| 336 | |||||||||||||||||||||
| 337 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||||||||||||||||||||
| 338 | |||||||||||||||||||||
| 339 | L<Project|https://github.com/iamalnewkirk/zing> | ||||||||||||||||||||
| 340 | |||||||||||||||||||||
| 341 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||||||||||||||||||||
| 342 | |||||||||||||||||||||
| 343 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||||||||||||||||||||
| 344 | |||||||||||||||||||||
| 345 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||||||||||||||||||||
| 346 | |||||||||||||||||||||
| 347 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||||||||||||||||||||
| 348 | |||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||