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