| 1 | package XAS::Spooler::Connector; | ||
| 2 | |||
| 3 | our $VERSION = '0.02'; | ||
| 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 | |||
| 9 | use XAS::Class | ||
| 10 | debug => 0, | ||
| 11 | version => $VERSION, | ||
| 12 | base => 'XAS::Lib::Stomp::POE::Client', | ||
| 13 | accessors => 'events', | ||
| 14 | constants => 'TRUE FALSE ARRAY', | ||
| 15 | codec => 'JSON', | ||
| 16 | filesystem => 'File', | ||
| 17 | vars => { | ||
| 18 | PARAMS => { | ||
| 19 | -hostname => { optional => 1, default => undef }, | ||
| 20 | } | ||
| 21 | } | ||
| 22 | ; | ||
| 23 | |||
| 24 | #use Data::Dumper; | ||
| 25 | |||
| 26 | # --------------------------------------------------------------------- | ||
| 27 | # Public Events | ||
| 28 | # --------------------------------------------------------------------- | ||
| 29 | |||
| 30 | sub handle_receipt { | ||
| 31 | my ($self, $frame) = @_[OBJECT, ARG0]; | ||
| 32 | |||
| 33 | my $alias = $self->alias; | ||
| 34 | my ($palias, $filename) = split(';', $frame->header->receipt_id); | ||
| 35 | |||
| 36 | $self->log->debug("$alias: alias = $palias, file = $filename"); | ||
| 37 | |||
| 38 | $poe_kernel->post($palias, 'unlink_file', $filename); | ||
| 39 | |||
| 40 | } | ||
| 41 | |||
| 42 | sub connection_down { | ||
| 43 | my ($self) = $_[OBJECT]; | ||
| 44 | |||
| 45 | my $alias = $self->alias; | ||
| 46 | |||
| 47 | $self->log->debug("$alias: entering connection_down()"); | ||
| 48 | |||
| 49 | $self->events->publish( | ||
| 50 | -event => 'pause_processing' | ||
| 51 | ); | ||
| 52 | |||
| 53 | $self->log->debug("$alias: leaving connection_down()"); | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | sub connection_up { | ||
| 58 | my ($self) = $_[OBJECT]; | ||
| 59 | |||
| 60 | my $alias = $self->alias; | ||
| 61 | |||
| 62 | $self->log->debug("$alias: entering connection_up()"); | ||
| 63 | |||
| 64 | $self->events->publish( | ||
| 65 | -event => 'resume_processing' | ||
| 66 | ); | ||
| 67 | |||
| 68 | $self->log->debug("$alias: leaving connection_up()"); | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | sub send_packet { | ||
| 73 | my ($self, $palias, $type, $queue, $data, $file) = @_[OBJECT,ARG0..ARG4]; | ||
| 74 | |||
| 75 | my $alias = $self->alias; | ||
| 76 | |||
| 77 | try { | ||
| 78 | |||
| 79 | my $message = { | ||
| 80 | hostname => $self->hostname, | ||
| 81 | timestamp => time(), | ||
| 82 | type => $type, | ||
| 83 | data => decode($data), | ||
| 84 | }; | ||
| 85 | |||
| 86 | my $packet = encode($message); | ||
| 87 | |||
| 88 | my $frame = $self->stomp->send( | ||
| 89 | -destination => $queue, | ||
| 90 | -message => $packet, | ||
| 91 | -receipt => sprintf("%s;%s", $palias, $file->name), | ||
| 92 | -persistent => 'true' | ||
| 93 | ); | ||
| 94 | |||
| 95 | $self->log->info("$alias: sending $file to $queue"); | ||
| 96 | |||
| 97 | $poe_kernel->call($alias, 'write_data', $frame); | ||
| 98 | |||
| 99 | } catch { | ||
| 100 | |||
| 101 | my $ex = $_; | ||
| 102 | |||
| 103 | $self->log->error("$alias: unable to encode/decode packet, reason: $ex"); | ||
| 104 | $self->log->debug("$alias: alias = $palias, file = $file"); | ||
| 105 | |||
| 106 | $poe_kernel->post($palias, 'unlink_file', $file); | ||
| 107 | |||
| 108 | }; | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | # --------------------------------------------------------------------- | ||
| 113 | # Public Methods | ||
| 114 | # --------------------------------------------------------------------- | ||
| 115 | |||
| 116 | sub session_initialize { | ||
| 117 | my $self = shift; | ||
| 118 | |||
| 119 | my $alias = $self->alias; | ||
| 120 | |||
| 121 | $self->log->debug("$alias: entering session_initialize()"); | ||
| 122 | |||
| 123 | $poe_kernel->state('send_packet', $self); | ||
| 124 | |||
| 125 | # walk the chain | ||
| 126 | |||
| 127 | $self->SUPER::session_initialize(); | ||
| 128 | |||
| 129 | $self->log->debug("$alias: leaving session_initialize()"); | ||
| 130 | |||
| 131 | } | ||
| 132 | |||
| 133 | # --------------------------------------------------------------------- | ||
| 134 | # Private Methods | ||
| 135 | # --------------------------------------------------------------------- | ||
| 136 | |||
| 137 | sub init { | ||
| 138 | my $class = shift; | ||
| 139 | |||
| 140 | my $self = $class->SUPER::init(@_); | ||
| 141 | |||
| 142 | unless (defined($self->{'hostname'})) { | ||
| 143 | |||
| 144 | $self->{'hostname'} = $self->env->host; | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | $self->{'events'} = XAS::Lib::POE::PubSub->new(); | ||
| 149 | |||
| 150 | return $self; | ||
| 151 | |||
| 152 | } | ||
| 153 | |||
| 154 | 1; | ||
| 155 | |||
| 156 | __END__ | ||
| 157 | |||
| 158 | =head1 NAME | ||
| 159 | |||
| 160 | XAS::Spooler::Connector - Perl extension for the XAS environment | ||
| 161 | |||
| 162 | =head1 SYNOPSIS | ||
| 163 | |||
| 164 | use XAS::Spooler::Connector; | ||
| 165 | |||
| 166 | my $connection = XAS::Spooler::Connector->new( | ||
| 167 | -alias => 'connector', | ||
| 168 | -host => $hostname, | ||
| 169 | -port => $port, | ||
| 170 | -retry_reconnect => TRUE, | ||
| 171 | -tcp_keepalive => TRUE, | ||
| 172 | -hostname => $env->host, | ||
| 173 | ); | ||
| 174 | |||
| 175 | =head1 DESCRIPTION | ||
| 176 | |||
| 177 | This module connects to a message queue server for spoolers. All of the spool | ||
| 178 | processors funnel messages thru this module. If the connection is lost to | ||
| 179 | the server, it signals the processor to stop processing until it is able to | ||
| 180 | reconnect to the server. | ||
| 181 | |||
| 182 | =head1 METHODS | ||
| 183 | |||
| 184 | =head2 new | ||
| 185 | |||
| 186 | This method inherits from L<XAS::Lib::Stomp::POE::Client|XAS::Lib::Stomp::POE::Client> | ||
| 187 | and takes these additional parameters: | ||
| 188 | |||
| 189 | =over | ||
| 190 | |||
| 191 | =item B<-hostname> | ||
| 192 | |||
| 193 | An optional name for the host that is processing these spool files. | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 197 | =head1 PUBLIC EVENTS | ||
| 198 | |||
| 199 | =head2 connection_down(OBJECT) | ||
| 200 | |||
| 201 | This event broadcasts that the connection had been dropped. | ||
| 202 | |||
| 203 | =over 4 | ||
| 204 | |||
| 205 | =item B<OBJECT> | ||
| 206 | |||
| 207 | The handle for the current self. | ||
| 208 | |||
| 209 | =back | ||
| 210 | |||
| 211 | =head2 connection_up(OBJECT) | ||
| 212 | |||
| 213 | This event broadcasts when the connection is established. | ||
| 214 | |||
| 215 | =over 4 | ||
| 216 | |||
| 217 | =item B<OBJECT> | ||
| 218 | |||
| 219 | The handle for the current self. | ||
| 220 | |||
| 221 | =back | ||
| 222 | |||
| 223 | =head2 send_packet(OBJECT,ARG0, ARG1, ARG2, ARG3, ARG4) | ||
| 224 | |||
| 225 | Process the data received from the processors. This processing includes | ||
| 226 | creating the standard message header, decoding the data and creating a | ||
| 227 | serialized message using JSON. This message is then sent to message queue | ||
| 228 | server. | ||
| 229 | |||
| 230 | =over 4 | ||
| 231 | |||
| 232 | =item B<OBJECT> | ||
| 233 | |||
| 234 | The handle for the current self. | ||
| 235 | |||
| 236 | =item B<ARG0> | ||
| 237 | |||
| 238 | The alias of the processor. | ||
| 239 | |||
| 240 | =item B<ARG1> | ||
| 241 | |||
| 242 | The type of data. | ||
| 243 | |||
| 244 | =item B<ARG2> | ||
| 245 | |||
| 246 | The queue to send the message too. | ||
| 247 | |||
| 248 | =item B<ARG3> | ||
| 249 | |||
| 250 | The actual data to process. This is usually a JSON formated string. | ||
| 251 | |||
| 252 | =item B<ARG4> | ||
| 253 | |||
| 254 | The full qualified name of the file that was processed. This, along with | ||
| 255 | the processor alias, is used for the STOMP receipt. | ||
| 256 | |||
| 257 | =back | ||
| 258 | |||
| 259 | =head1 SEE ALSO | ||
| 260 | |||
| 261 | =over 4 | ||
| 262 | |||
| 263 | =item L<XAS::Spooler::Processor|XAS::Spooler::Processor> | ||
| 264 | |||
| 265 | =item L<XAS::Spooler|XAS::Spooler> | ||
| 266 | |||
| 267 | =item L<XAS|XAS> | ||
| 268 | |||
| 269 | =back | ||
| 270 | |||
| 271 | =head1 AUTHOR | ||
| 272 | |||
| 273 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 274 | |||
| 275 | =head1 COPYRIGHT AND LICENSE | ||
| 276 | |||
| 277 | Copyright (c) 2012-2015 Kevin L. Esteb | ||
| 278 | |||
| 279 | This is free software; you can redistribute it and/or modify it under | ||
| 280 | the terms of the Artistic License 2.0. For details, see the full text | ||
| 281 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||
| 282 | |||
| 283 | =cut | ||