File Coverage

blib/lib/App/MultiModule/Tasks/Scheduler.pm
Criterion Covered Total %
statement 20 80 25.0
branch 0 24 0.0
condition n/a
subroutine 7 13 53.8
pod 3 3 100.0
total 30 120 25.0


line stmt bran cond sub pod time code
1             package App::MultiModule::Tasks::Scheduler;
2             $App::MultiModule::Tasks::Scheduler::VERSION = '1.161160';
3 4     4   2006 use 5.006;
  4         18  
4 4     4   12 use strict;
  4         4  
  4         74  
5 4     4   10 use warnings FATAL => 'all';
  4         4  
  4         112  
6 4     4   12 use Data::Dumper;
  4         2  
  4         176  
7 4     4   12 use Message::Transform qw(mtransform);
  4         4  
  4         136  
8 4     4   14 use Storable;
  4         2  
  4         204  
9              
10 4     4   14 use parent 'App::MultiModule::Task';
  4         4  
  4         22  
11              
12             =head1 NAME
13              
14             App::MultiModule::Tasks::Scheduler - Schedule messages, repeated and singletons
15              
16             =cut
17              
18             =head2 message
19              
20             =cut
21              
22             sub message {
23 0     0 1   my $self = shift;
24 0           my $message = shift;
25 0           my %args = @_;
26             $self->debug('message', message => $message)
27 0 0         if $self->{debug} > 5;
28 0           my $state = $self->{state};
29 0           my $dynamic_schedule = $state->{dynamic_schedule};
30 0 0         if($message->{dynamic_config}) {
31 0           mtransform($dynamic_schedule, $message->{dynamic_config});
32             }
33             }
34              
35             sub _tick {
36 0     0     my $self = shift;
37 0           my $config = $self->{config};
38 0           my $state = $self->{state};
39 0           my $tick = $state->{tick};
40 0           my $schedule = $config->{schedule};
41 0           my $scheduler_sends = $state->{scheduler_sends};
42 0           my $dynamic_schedule = $state->{dynamic_schedule};
43 0           eval {
44 0     0     local $SIG{ALRM} = sub { die "timed out\n"; };
  0            
45 0           alarm 1;
46              
47             #so we should probably merge dynamic_schedule into
48             #schedule here.
49 0           my $merged_schedule = Storable::dclone($schedule);
50 0           mtransform($merged_schedule, $dynamic_schedule);
51 0           my $ts = time;
52 0           my @scheduled_keys = keys %$merged_schedule;
53 0           foreach my $scheduled_key (@scheduled_keys) {
54 0           my $scheduled_info = $merged_schedule->{$scheduled_key};
55 0 0         if($scheduled_info->{runAt}) {
56 0 0         if($scheduled_info->{runAt} < $ts) {
57 0           my $message = Storable::dclone($scheduled_info);
58 0           $message->{scheduler_scheduled_key} = $scheduled_key;
59 0           $self->emit($message);
60 0           delete $dynamic_schedule->{$scheduled_key};
61 0           next;
62             }
63             }
64             #assuming $scheduled_info->{recur} at this point
65 0           my $recur = $scheduled_info->{recur};
66 0 0         next unless defined $recur;
67             $scheduler_sends->{$scheduled_key} = {
68             scheduler_create_ts => time,
69             scheduler_send_ts => 0,
70             scheduler_send_count => 1,
71 0 0         } unless $scheduler_sends->{$scheduled_key};
72 0           my $scheduler_send_ts = $scheduler_sends->{$scheduled_key}->{scheduler_send_ts};
73 0 0         if($ts > $scheduler_send_ts + $recur) {
74             #send a message
75             #what message?
76             #start with what's in the schedule, which should at this
77             #point contain stuff merged in from $state->{dynamic_schedule}
78             #Then merge in $scheduler_sends->{$scheduled_key}
79 0           my $message = Storable::dclone($scheduled_info);
80 0           mtransform($message, $scheduler_sends->{$scheduled_key});
81 0           $message->{scheduler_scheduled_key} = $scheduled_key;
82 0           $self->emit($message);
83              
84 0           $scheduler_sends->{$scheduled_key}->{scheduler_send_count}++;
85 0           $scheduler_sends->{$scheduled_key}->{scheduler_send_ts} = $ts;
86             }
87             }
88             };
89 0           alarm 0;
90 0 0         if($@) {
91 0           $self->error("_tick failure: $@");
92             }
93             #this has to happen no matter what
94 0           $state->{tick} = time;
95             }
96              
97             =head2 set_config
98              
99             =cut
100             sub set_config {
101 0     0 1   my $self = shift;
102 0           my $config = shift;
103 0           $self->{config} = $config;
104 0           my $state = $self->{state};
105 0 0         if(not $state->{start_tick}) {
106 0           $state->{start_tick} = time;
107 0           $state->{tick} = time;
108             }
109 0 0         $config->{schedule} = {} unless $config->{schedule};
110 0 0         $state->{scheduler_sends} = {} unless $state->{scheduler_sends};
111 0 0         $state->{dynamic_schedule} = {} unless $state->{dynamic_schedule};
112             $self->named_recur(
113             recur_name => 'scheduler_tick',
114             repeat_interval => 1,
115             work => sub {
116 0     0     $self->_tick,
117             },
118 0           );
119             }
120              
121             =head2 is_stateful
122              
123             =cut
124             sub is_stateful {
125 0     0 1   return 'absolultely required';
126             }
127              
128             =head1 AUTHOR
129              
130             Dana M. Diederich, C<< <dana@realms.org> >>
131              
132             =head1 BUGS
133              
134             Please report any bugs or feature requests through L<https://github.com/dana/perl-App-MultiModule-Tasks-Scheduler/issues>. I will be notified, and then you'll
135             automatically be notified of progress on your bug as I make changes.
136              
137             =head1 SUPPORT
138              
139             You can find documentation for this module with the perldoc command.
140              
141             perldoc App::MultiModule::Tasks::Scheduler
142              
143              
144             You can also look for information at:
145              
146             =over 4
147              
148             =item * Report bugs here:
149              
150             L<https://github.com/dana/perl-App-MultiModule-Tasks-Scheduler/issues>
151              
152             =item * AnnoCPAN: Annotated CPAN documentation
153              
154             L<http://annocpan.org/dist/App-MultiModule-Tasks-Scheduler>
155              
156             =item * CPAN Ratings
157              
158             L<http://cpanratings.perl.org/d/App-MultiModule-Tasks-Scheduler>
159              
160             =item * Search CPAN
161              
162             L<https://metacpan.org/module/App::MultiModule::Tasks::Scheduler>
163              
164             =back
165              
166             =head1 ACKNOWLEDGEMENTS
167              
168             =head1 LICENSE AND COPYRIGHT
169              
170             Copyright 2016 Dana M. Diederich.
171              
172             This program is free software; you can redistribute it and/or modify it
173             under the terms of the the Artistic License (2.0). You may obtain a
174             copy of the full license at:
175              
176             L<http://www.perlfoundation.org/artistic_license_2_0>
177              
178             Any use, modification, and distribution of the Standard or Modified
179             Versions is governed by this Artistic License. By using, modifying or
180             distributing the Package, you accept this license. Do not use, modify,
181             or distribute the Package, if you do not accept this license.
182              
183             If your Modified Version has been derived from a Modified Version made
184             by someone other than you, you are nevertheless required to ensure that
185             your Modified Version complies with the requirements of this license.
186              
187             This license does not grant you the right to use any trademark, service
188             mark, tradename, or logo of the Copyright Holder.
189              
190             This license includes the non-exclusive, worldwide, free-of-charge
191             patent license to make, have made, use, offer to sell, sell, import and
192             otherwise transfer the Package with respect to any patent claims
193             licensable by the Copyright Holder that are necessarily infringed by the
194             Package. If you institute patent litigation (including a cross-claim or
195             counterclaim) against any party alleging that the Package constitutes
196             direct or contributory patent infringement, then this Artistic License
197             to you shall terminate on the date that such litigation is filed.
198              
199             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
200             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
201             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
202             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
203             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
204             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
205             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
206             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
207              
208              
209             =cut
210              
211             1; # End of App::MultiModule::Tasks::Scheduler