| 1 | package XAS::Collector::Input::Stomp; | ||
| 2 | |||
| 3 | our $VERSION = '0.03'; | ||
| 4 | |||
| 5 | 1 | ▶ | use POE; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use XAS::Lib::POE::PubSub; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Params::Validate qw(HASHREF); | ||
| 9 | |||
| 10 | use XAS::Class | ||
| 11 | debug => 0, | ||
| 12 | version => $VERSION, | ||
| 13 | base => 'XAS::Lib::Stomp::POE::Client', | ||
| 14 | mixin => 'XAS::Lib::Mixins::Handlers', | ||
| 15 | utils => 'dotid', | ||
| 16 | codec => 'JSON', | ||
| 17 | constants => 'HASH', | ||
| 18 | mutators => 'connected', | ||
| 19 | accessors => 'pubsub', | ||
| 20 | vars => { | ||
| 21 | PARAMS => { | ||
| 22 | '-types' => { type => HASHREF }, | ||
| 23 | '-prefetch' => { optional => 1, default => 0 }, | ||
| 24 | }, | ||
| 25 | } | ||
| 26 | ; | ||
| 27 | |||
| 28 | #use Data::Dumper; | ||
| 29 | # rabbitmq - optional "prefetch-count" with subscribe frame. | ||
| 30 | |||
| 31 | # ---------------------------------------------------------------------- | ||
| 32 | # Public Methods | ||
| 33 | # ---------------------------------------------------------------------- | ||
| 34 | |||
| 35 | sub session_initialize { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | my $alias = $self->alias; | ||
| 39 | |||
| 40 | $self->log->debug("$alias: entering session_initialize()"); | ||
| 41 | |||
| 42 | # public events | ||
| 43 | |||
| 44 | $poe_kernel->state('pause_processing', $self); | ||
| 45 | $poe_kernel->state('resume_processing', $self); | ||
| 46 | |||
| 47 | # private events | ||
| 48 | |||
| 49 | $poe_kernel->state('stop_queue', $self, '_stop_queue'); | ||
| 50 | $poe_kernel->state('start_queue', $self, '_start_queue'); | ||
| 51 | |||
| 52 | $self->pubsub->subscribe($alias); | ||
| 53 | |||
| 54 | # walk the chain | ||
| 55 | |||
| 56 | $self->SUPER::session_initialize(); | ||
| 57 | |||
| 58 | $self->log->debug("$alias: leaving session_initialize()"); | ||
| 59 | |||
| 60 | } | ||
| 61 | |||
| 62 | sub session_shutdown { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | my $alias = $self->alias; | ||
| 66 | |||
| 67 | $self->log->debug("$alias: entering session_shutdown"); | ||
| 68 | |||
| 69 | $poe_kernel->alarm_remove_all(); | ||
| 70 | |||
| 71 | # walk the chain | ||
| 72 | |||
| 73 | $self->SUPER::session_shutdown(); | ||
| 74 | |||
| 75 | $self->log->debug("$alias: leaving session_shutdown"); | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | # ---------------------------------------------------------------------- | ||
| 80 | # Public Events | ||
| 81 | # ---------------------------------------------------------------------- | ||
| 82 | |||
| 83 | sub handle_connected { | ||
| 84 | my ($self, $frame) = @_[OBJECT, ARG0]; | ||
| 85 | |||
| 86 | my $alias = $self->alias; | ||
| 87 | |||
| 88 | if ($self->tcp_keepalive) { | ||
| 89 | |||
| 90 | $self->log->info_msg('tcp_keepalive_enabled', $alias); | ||
| 91 | |||
| 92 | $self->init_keepalive(); | ||
| 93 | $self->enable_keepalive($self->socket); | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | $self->log->info_msg('collector_connected', $alias, $self->host, $self->port); | ||
| 98 | $self->connected(1); | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | sub handle_message { | ||
| 103 | my ($self, $frame) = @_[OBJECT,ARG0]; | ||
| 104 | |||
| 105 | my $data; | ||
| 106 | my $type; | ||
| 107 | my $nframe; | ||
| 108 | my $format; | ||
| 109 | my $output; | ||
| 110 | my $message; | ||
| 111 | my $message_id; | ||
| 112 | my $alias = $self->alias; | ||
| 113 | |||
| 114 | try { | ||
| 115 | |||
| 116 | $message = decode($frame->body); | ||
| 117 | $message_id = $frame->header->message_id; | ||
| 118 | $nframe = $self->stomp->ack(-message_id => $message_id); | ||
| 119 | |||
| 120 | if ($type = $message->{'type'}) { | ||
| 121 | |||
| 122 | $self->log->info_msg('collector_received', | ||
| 123 | $alias, | ||
| 124 | $message_id, | ||
| 125 | $message->{'type'}, | ||
| 126 | $message->{'hostname'} | ||
| 127 | ); | ||
| 128 | |||
| 129 | if (defined($self->{'types'}->{$type})) { | ||
| 130 | |||
| 131 | $data = $message->{'data'}; | ||
| 132 | $format = $self->{'types'}->{$type}->{'format'}; | ||
| 133 | $output = $self->{'types'}->{$type}->{'output'}; | ||
| 134 | |||
| 135 | $poe_kernel->post($format, 'format_data', $data, $nframe, $alias, $output); | ||
| 136 | |||
| 137 | } else { | ||
| 138 | |||
| 139 | $self->throw_msg( | ||
| 140 | dotid($self->class) . '.input.stomp.unknowntype', | ||
| 141 | 'collector_unknowntype', | ||
| 142 | $message->{'type'}, | ||
| 143 | ); | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | } else { | ||
| 148 | |||
| 149 | $self->throw_msg( | ||
| 150 | dotid($self->class) . '.input.stomp.notype', | ||
| 151 | 'collector_notype', | ||
| 152 | $alias, | ||
| 153 | ); | ||
| 154 | |||
| 155 | } | ||
| 156 | |||
| 157 | } catch { | ||
| 158 | |||
| 159 | my $ex = $_; | ||
| 160 | |||
| 161 | $self->exception_handler($ex); | ||
| 162 | $self->log->error(Dumper($frame)); | ||
| 163 | |||
| 164 | $poe_kernel->post($alias, 'write_data', $nframe); | ||
| 165 | |||
| 166 | }; | ||
| 167 | |||
| 168 | } | ||
| 169 | |||
| 170 | sub connection_down { | ||
| 171 | my ($self) = $_[OBJECT]; | ||
| 172 | |||
| 173 | my $alias = $self->alias; | ||
| 174 | |||
| 175 | $self->connected(0); | ||
| 176 | $poe_kernel->post($alias, 'pause_processing'); | ||
| 177 | |||
| 178 | $self->log->debug("$alias: connection down"); | ||
| 179 | |||
| 180 | } | ||
| 181 | |||
| 182 | sub connection_up { | ||
| 183 | my ($self) = $_[OBJECT]; | ||
| 184 | |||
| 185 | my $alias = $self->alias; | ||
| 186 | |||
| 187 | $self->connected(1); | ||
| 188 | $poe_kernel->post($alias, 'resume_processing'); | ||
| 189 | |||
| 190 | $self->log->debug("$alias: connection up"); | ||
| 191 | |||
| 192 | } | ||
| 193 | |||
| 194 | sub pause_processing { | ||
| 195 | my ($self) = $_[OBJECT]; | ||
| 196 | |||
| 197 | my $alias = $self->alias; | ||
| 198 | |||
| 199 | $self->log->debug("$alias: entering pause_processing()"); | ||
| 200 | |||
| 201 | foreach my $type (keys %{$self->{'types'}}) { | ||
| 202 | |||
| 203 | my $ralias = $self->{'types'}->{$type}->{'input'}; | ||
| 204 | |||
| 205 | $poe_kernel->post($alias, 'stop_queue', $ralias); | ||
| 206 | |||
| 207 | } | ||
| 208 | |||
| 209 | $self->log->debug("$alias: leaving pause_processing()"); | ||
| 210 | |||
| 211 | } | ||
| 212 | |||
| 213 | sub resume_processing { | ||
| 214 | my ($self) = $_[OBJECT]; | ||
| 215 | |||
| 216 | my $alias = $self->alias; | ||
| 217 | |||
| 218 | $self->log->debug("$alias: entering resume_processing()"); | ||
| 219 | |||
| 220 | foreach my $type (keys %{$self->{'types'}}) { | ||
| 221 | |||
| 222 | my $ralias = $self->{'types'}->{$type}->{'input'}; | ||
| 223 | |||
| 224 | $poe_kernel->post($alias, 'start_queue', $ralias); | ||
| 225 | |||
| 226 | } | ||
| 227 | |||
| 228 | $self->log->debug("$alias: leaving resume_processing()"); | ||
| 229 | |||
| 230 | } | ||
| 231 | |||
| 232 | # ---------------------------------------------------------------------- | ||
| 233 | # Private Events | ||
| 234 | # ---------------------------------------------------------------------- | ||
| 235 | |||
| 236 | sub _start_queue { | ||
| 237 | my ($self, $ralias) = @_[OBJECT,ARG0]; | ||
| 238 | |||
| 239 | my $alias = $self->alias; | ||
| 240 | my $queue = $self->_find_queue($ralias); | ||
| 241 | |||
| 242 | if ($self->connected) { | ||
| 243 | |||
| 244 | my $frame = $self->stomp->subscribe( | ||
| 245 | -destination => $queue, | ||
| 246 | -ack => 'client', | ||
| 247 | -prefetch => $self->prefetch, | ||
| 248 | ); | ||
| 249 | |||
| 250 | $self->log->info_msg('collector_subscribed', $alias, $queue); | ||
| 251 | $poe_kernel->post($alias, 'write_data', $frame); | ||
| 252 | |||
| 253 | } else { | ||
| 254 | |||
| 255 | $poe_kernel->delay_add('start_queue', 5, $ralias); | ||
| 256 | $self->log->warn_msg('collector_waiting', $alias, $queue); | ||
| 257 | |||
| 258 | } | ||
| 259 | |||
| 260 | } | ||
| 261 | |||
| 262 | sub _stop_queue { | ||
| 263 | my ($self, $ralias) = @_[OBJECT,ARG0]; | ||
| 264 | |||
| 265 | my $alias = $self->alias; | ||
| 266 | my $queue = $self->_find_queue($ralias); | ||
| 267 | |||
| 268 | if ($self->connected) { | ||
| 269 | |||
| 270 | my $frame = $self->stomp->unsubscribe( | ||
| 271 | -destination => $queue, | ||
| 272 | ); | ||
| 273 | |||
| 274 | $self->log->warn_msg('collector_unsubscribed', $alias, $queue); | ||
| 275 | $poe_kernel->post($alias, 'write_data', $frame); | ||
| 276 | |||
| 277 | } | ||
| 278 | |||
| 279 | } | ||
| 280 | |||
| 281 | # ---------------------------------------------------------------------- | ||
| 282 | # Private Methods | ||
| 283 | # ---------------------------------------------------------------------- | ||
| 284 | |||
| 285 | sub init { | ||
| 286 | my $class = shift; | ||
| 287 | |||
| 288 | my $self = $class->SUPER::init(@_); | ||
| 289 | |||
| 290 | $self->{'pubsub'} = XAS::Lib::POE::PubSub->new(); | ||
| 291 | $self->{'connected'} = 0; | ||
| 292 | |||
| 293 | return $self; | ||
| 294 | |||
| 295 | } | ||
| 296 | |||
| 297 | sub _find_queue { | ||
| 298 | my $self = shift; | ||
| 299 | my $ralias = shift; | ||
| 300 | |||
| 301 | my $queue = ''; | ||
| 302 | my $alias = $self->alias; | ||
| 303 | |||
| 304 | $self->log->debug(sprintf('%s: find_queue() alias = %s', $alias, $ralias)); | ||
| 305 | |||
| 306 | while (my ($key, $value) = each(%{$self->{'types'}})) { | ||
| 307 | |||
| 308 | if (($value->{'input'} eq $ralias) or | ||
| 309 | ($value->{'output'} eq $ralias)) { | ||
| 310 | |||
| 311 | $queue = $value->{'queue'}; | ||
| 312 | |||
| 313 | } | ||
| 314 | |||
| 315 | } | ||
| 316 | |||
| 317 | return $queue; | ||
| 318 | |||
| 319 | } | ||
| 320 | |||
| 321 | 1; | ||
| 322 | |||
| 323 | __END__ | ||
| 324 | |||
| 325 | =head1 NAME | ||
| 326 | |||
| 327 | XAS::Collector::Input::Stomp - A class for the XAS environment | ||
| 328 | |||
| 329 | =head1 SYNOPSIS | ||
| 330 | |||
| 331 | use XAS::Collector::Input::Stomp; | ||
| 332 | |||
| 333 | my $types => { | ||
| 334 | 'xas-alerts' => { | ||
| 335 | queue => '/queue/alerts', | ||
| 336 | format => 'format-alerts', | ||
| 337 | input => 'input-stomp', | ||
| 338 | output => 'output-logstash', | ||
| 339 | }, | ||
| 340 | }; | ||
| 341 | |||
| 342 | my $processor = XAS::Collector::Input::Stomp->new( | ||
| 343 | -alias => 'input-stomp', | ||
| 344 | -types => $types | ||
| 345 | ); | ||
| 346 | |||
| 347 | =head1 DESCRIPTION | ||
| 348 | |||
| 349 | This module will monitor a queue on a STOMP based message queue server. | ||
| 350 | It will attempt to maintain a connection to the server. | ||
| 351 | |||
| 352 | =head1 METHODS | ||
| 353 | |||
| 354 | =head2 new | ||
| 355 | |||
| 356 | This method will initialize the module and takes these parameters: | ||
| 357 | |||
| 358 | =over 4 | ||
| 359 | |||
| 360 | =item B<-types> | ||
| 361 | |||
| 362 | The message types that this input module can handle. | ||
| 363 | |||
| 364 | =item B<-prefetch> | ||
| 365 | |||
| 366 | The number of messages to prefetch from the queue. Default is unlimitied. This | ||
| 367 | may have meaning only on a L<RabbitMQ|http://www.rabbitmq.com/stomp.html> server. | ||
| 368 | |||
| 369 | =back | ||
| 370 | |||
| 371 | =head1 PUBLIC EVENTS | ||
| 372 | |||
| 373 | This module declares the following events: | ||
| 374 | |||
| 375 | =head2 pause_processing | ||
| 376 | |||
| 377 | This event is broadcasted when the connection to the message queue server | ||
| 378 | is down. It's purpose is to stop processing messages on a queue. | ||
| 379 | |||
| 380 | =head2 resume_processing | ||
| 381 | |||
| 382 | This event is broadcasted when the connection to the message queue server | ||
| 383 | is down. It's purpose is to start processing messages on a queue. | ||
| 384 | |||
| 385 | =head1 SEE ALSO | ||
| 386 | |||
| 387 | =over 4 | ||
| 388 | |||
| 389 | =item L<XAS::Collector|XAS::Collector> | ||
| 390 | |||
| 391 | =item L<XAS|XAS> | ||
| 392 | |||
| 393 | =back | ||
| 394 | |||
| 395 | =head1 AUTHOR | ||
| 396 | |||
| 397 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 398 | |||
| 399 | =head1 COPYRIGHT AND LICENSE | ||
| 400 | |||
| 401 | Copyright (c) 2012-2015 Kevin L. Esteb | ||
| 402 | |||
| 403 | This is free software; you can redistribute it and/or modify it under | ||
| 404 | the terms of the Artistic License 2.0. For details, see the full text | ||
| 405 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||
| 406 | |||
| 407 | =cut | ||