| 1 | package App::MultiModule::Tasks::Heartbeat; | ||
| 2 | $App::MultiModule::Tasks::Heartbeat::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 Message::Match qw(mmatch); | ||
| 9 | use Storable; | ||
| 10 | use POSIX ":sys_wait_h"; | ||
| 11 | |||
| 12 | use parent 'App::MultiModule::Task'; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | App::MultiModule::Tasks::Heartbeat - Detect missing streams or attributes | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | =head2 message | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | sub message { | ||
| 25 | my $self = shift; | ||
| 26 | my $message = shift; | ||
| 27 | my $config = $self->{config}; | ||
| 28 | my %args = @_; | ||
| 29 | $self->debug('message', message => $message) | ||
| 30 | if $self->{debug} > 5; | ||
| 31 | my $instances = $self->{state}->{instances}; | ||
| 32 | while(my($hb_group_name, $hb_group) = each %{$config->{hb_groups}}) { | ||
| 33 | #first find any hb_groups that apply to us | ||
| 34 | next unless $hb_group->{match}; | ||
| 35 | next unless mmatch($message, $hb_group->{match}); | ||
| 36 | next unless $hb_group->{hb_instance}; | ||
| 37 | mtransform($message, { hb_instance => $hb_group->{hb_instance} }); | ||
| 38 | my $hb_instance = $message->{hb_instance}; | ||
| 39 | if(not $hb_instance) { | ||
| 40 | $self->error("App::MultiModule::Tasks::Heartbeat::message: no hb_instance set bis \$hb_group_name=$hb_group_name \$hb_group->{hb_instance}=$hb_group->{hb_instance}"); | ||
| 41 | next; | ||
| 42 | } | ||
| 43 | $instances->{$hb_group_name} = {} unless $instances->{$hb_group_name}; | ||
| 44 | if(not $instances->{$hb_group_name}->{$hb_instance}) { | ||
| 45 | $instances->{$hb_group_name}->{$hb_instance} = { | ||
| 46 | last_emit_ts => 0, | ||
| 47 | } | ||
| 48 | } | ||
| 49 | my $instance = $instances->{$hb_group_name}->{$hb_instance}; | ||
| 50 | $instance->{message_receive_ts} = time; | ||
| 51 | $instance->{last_message} = Storable::dclone($message); | ||
| 52 | delete $instance->{last_message}->{'.ipc_transit_meta'}; | ||
| 53 | #branch to different types of heartbeat config here | ||
| 54 | if( $hb_group->{changing_fields} and | ||
| 55 | ref $hb_group->{changing_fields} and | ||
| 56 | ref $hb_group->{changing_fields} eq 'ARRAY') { | ||
| 57 | #this is the 'changing_fields' type | ||
| 58 | $instance->{changed_fields} = {} unless $instance->{changed_fields}; | ||
| 59 | |||
| 60 | foreach my $changing_fieldname (@{$hb_group->{changing_fields}}) { | ||
| 61 | $instance->{changed_fields}->{$changing_fieldname} = {} | ||
| 62 | unless $instance->{changed_fields}->{$changing_fieldname}; | ||
| 63 | my $field = $instance->{changed_fields}->{$changing_fieldname}; | ||
| 64 | next unless defined $message->{$changing_fieldname}; | ||
| 65 | if( not defined $field->{last_value} or | ||
| 66 | $field->{last_value} ne $message->{$changing_fieldname}) { | ||
| 67 | $field->{last_change_ts} = time; | ||
| 68 | $field->{last_value} = $message->{$changing_fieldname}; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } #else other types | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _tick { | ||
| 76 | my $self = shift; | ||
| 77 | my $config = $self->{config}; | ||
| 78 | my $instances = $self->{state}->{instances}; | ||
| 79 | my $timeout = $self->{config}->{tick_timeout} || 1; | ||
| 80 | my $ts = time; | ||
| 81 | eval { | ||
| 82 | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||
| 83 | alarm $timeout; | ||
| 84 | while(my($hb_group_name,$hb_groups) = each %$instances) { | ||
| 85 | while(my($hb_instance_name, $instance) = each %$hb_groups) { | ||
| 86 | my $group_config = $config->{hb_groups}->{$hb_group_name}; | ||
| 87 | my $emit_ts_span = $group_config->{emit_ts_span} || 10; | ||
| 88 | my $last_emit_ts = $instance->{last_emit_ts}; | ||
| 89 | next if $ts - $emit_ts_span < $last_emit_ts; | ||
| 90 | my $message = $instance->{last_message}; | ||
| 91 | mtransform($message, $group_config->{transform}) | ||
| 92 | if $group_config->{transform}; | ||
| 93 | $instance->{last_emit_ts} = $ts; | ||
| 94 | #now set hearbeat_last_change_ts_span | ||
| 95 | $message->{hearbeat_last_change_ts_span} = 0; #default | ||
| 96 | while(my($change_field_name, $field) = each %{$instance->{changed_fields}}) { | ||
| 97 | $message->{hearbeat_last_change_ts_span} = $ts - $field->{last_change_ts} | ||
| 98 | if $ts - $field->{last_change_ts} > $message->{hearbeat_last_change_ts_span}; | ||
| 99 | } | ||
| 100 | $self->emit($message); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | }; | ||
| 104 | if($@) { | ||
| 105 | print STDERR "error: $@\n"; | ||
| 106 | $self->error("App::MultiModule::Tasks::Heartbeat::_tick: general exception: $@"); | ||
| 107 | } | ||
| 108 | alarm 0; | ||
| 109 | } | ||
| 110 | |||
| 111 | =head2 set_config | ||
| 112 | |||
| 113 | =cut | ||
| 114 | sub set_config { | ||
| 115 | my $self = shift; | ||
| 116 | my $config = shift; | ||
| 117 | $self->{config} = $config; | ||
| 118 | $self->{config}->{hb_groups} = {} | ||
| 119 | unless $self->{config}->{hb_groups}; | ||
| 120 | $self->{state}->{instances} = {} | ||
| 121 | unless $self->{state}->{instances}; | ||
| 122 | $self->named_recur( | ||
| 123 | recur_name => 'Heartbeat_tick', | ||
| 124 | repeat_interval => 1, | ||
| 125 | work => sub { | ||
| 126 | $self->_tick; | ||
| 127 | } | ||
| 128 | ); | ||
| 129 | } | ||
| 130 | |||
| 131 | =head2 is_stateful | ||
| 132 | |||
| 133 | =cut | ||
| 134 | sub is_stateful { | ||
| 135 | return 'certainly'; | ||
| 136 | } | ||
| 137 | |||
| 138 | =head1 AUTHOR | ||
| 139 | |||
| 140 | Dana M. Diederich, C<< <dana@realms.org> >> | ||
| 141 | |||
| 142 | =head1 BUGS | ||
| 143 | |||
| 144 | Please report any bugs or feature requests through L<https://github.com/dana/perl-App-MultiModule-Tasks-Heartbeat/issues>. I will be notified, and then you'll | ||
| 145 | automatically be notified of progress on your bug as I make changes. | ||
| 146 | |||
| 147 | =head1 SUPPORT | ||
| 148 | |||
| 149 | You can find documentation for this module with the perldoc command. | ||
| 150 | |||
| 151 | perldoc App::MultiModule::Tasks::Heartbeat | ||
| 152 | |||
| 153 | |||
| 154 | You can also look for information at: | ||
| 155 | |||
| 156 | =over 4 | ||
| 157 | |||
| 158 | =item * Report bugs here: | ||
| 159 | |||
| 160 | L<https://github.com/dana/perl-App-MultiModule-Tasks-Heartbeat/issues> | ||
| 161 | |||
| 162 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 163 | |||
| 164 | L<http://annocpan.org/dist/App-MultiModule-Tasks-Heartbeat> | ||
| 165 | |||
| 166 | =item * CPAN Ratings | ||
| 167 | |||
| 168 | L<http://cpanratings.perl.org/d/App-MultiModule-Tasks-Heartbeat> | ||
| 169 | |||
| 170 | =item * Search CPAN | ||
| 171 | |||
| 172 | L<https://metacpan.org/module/App::MultiModule::Tasks::Heartbeat> | ||
| 173 | |||
| 174 | =back | ||
| 175 | |||
| 176 | =head1 ACKNOWLEDGEMENTS | ||
| 177 | |||
| 178 | =head1 LICENSE AND COPYRIGHT | ||
| 179 | |||
| 180 | Copyright 2016 Dana M. Diederich. | ||
| 181 | |||
| 182 | This program is free software; you can redistribute it and/or modify it | ||
| 183 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 184 | copy of the full license at: | ||
| 185 | |||
| 186 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 187 | |||
| 188 | Any use, modification, and distribution of the Standard or Modified | ||
| 189 | Versions is governed by this Artistic License. By using, modifying or | ||
| 190 | distributing the Package, you accept this license. Do not use, modify, | ||
| 191 | or distribute the Package, if you do not accept this license. | ||
| 192 | |||
| 193 | If your Modified Version has been derived from a Modified Version made | ||
| 194 | by someone other than you, you are nevertheless required to ensure that | ||
| 195 | your Modified Version complies with the requirements of this license. | ||
| 196 | |||
| 197 | This license does not grant you the right to use any trademark, service | ||
| 198 | mark, tradename, or logo of the Copyright Holder. | ||
| 199 | |||
| 200 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 201 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 202 | otherwise transfer the Package with respect to any patent claims | ||
| 203 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 204 | Package. If you institute patent litigation (including a cross-claim or | ||
| 205 | counterclaim) against any party alleging that the Package constitutes | ||
| 206 | direct or contributory patent infringement, then this Artistic License | ||
| 207 | to you shall terminate on the date that such litigation is filed. | ||
| 208 | |||
| 209 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 210 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 211 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 212 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 213 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 214 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 215 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 216 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 217 | |||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | 1; # End of App::MultiModule::Tasks::Heartbeat |