| 1 | package Daemonise::Plugin::RabbitMQ; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use Mouse::Role; | ||
| 5 | use experimental 'smartmatch'; | ||
| 6 | |||
| 7 | # ABSTRACT: Daemonise RabbitMQ plugin | ||
| 8 | |||
| 9 | use Net::AMQP::RabbitMQ 2.300000; | ||
| 10 | use Carp; | ||
| 11 | use JSON; | ||
| 12 | use Try::Tiny; | ||
| 13 | |||
| 14 | |||
| 15 | our $js = JSON->new; | ||
| 16 | $js->utf8; | ||
| 17 | $js->allow_blessed; | ||
| 18 | $js->convert_blessed; | ||
| 19 | $js->allow_nonref; | ||
| 20 | |||
| 21 | |||
| 22 | has 'is_worker' => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'Bool', | ||
| 25 | lazy => 1, | ||
| 26 | default => sub { 0 }, | ||
| 27 | ); | ||
| 28 | |||
| 29 | |||
| 30 | has 'rabbit_host' => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => 'Str', | ||
| 33 | lazy => 1, | ||
| 34 | default => sub { 'localhost' }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | |||
| 38 | has 'rabbit_port' => ( | ||
| 39 | is => 'rw', | ||
| 40 | isa => 'Int', | ||
| 41 | lazy => 1, | ||
| 42 | default => sub { 5672 }, | ||
| 43 | ); | ||
| 44 | |||
| 45 | |||
| 46 | has 'rabbit_user' => ( | ||
| 47 | is => 'rw', | ||
| 48 | isa => 'Str', | ||
| 49 | lazy => 1, | ||
| 50 | default => sub { 'guest' }, | ||
| 51 | ); | ||
| 52 | |||
| 53 | |||
| 54 | has 'rabbit_pass' => ( | ||
| 55 | is => 'rw', | ||
| 56 | isa => 'Str', | ||
| 57 | lazy => 1, | ||
| 58 | default => sub { 'guest' }, | ||
| 59 | ); | ||
| 60 | |||
| 61 | |||
| 62 | has 'rabbit_vhost' => ( | ||
| 63 | is => 'rw', | ||
| 64 | isa => 'Str', | ||
| 65 | lazy => 1, | ||
| 66 | default => sub { '/' }, | ||
| 67 | ); | ||
| 68 | |||
| 69 | |||
| 70 | has 'rabbit_exchange' => ( | ||
| 71 | is => 'rw', | ||
| 72 | isa => 'Str', | ||
| 73 | lazy => 1, | ||
| 74 | default => sub { 'amq.direct' }, | ||
| 75 | ); | ||
| 76 | |||
| 77 | |||
| 78 | has 'rabbit_channel' => ( | ||
| 79 | is => 'rw', | ||
| 80 | isa => 'Int', | ||
| 81 | lazy => 1, | ||
| 82 | default => sub { int(rand(63999)) + 1 }, | ||
| 83 | ); | ||
| 84 | |||
| 85 | |||
| 86 | has 'rabbit_consumer_tag' => ( | ||
| 87 | is => 'rw', | ||
| 88 | isa => 'Str', | ||
| 89 | lazy => 1, | ||
| 90 | default => sub { '' }, | ||
| 91 | ); | ||
| 92 | |||
| 93 | |||
| 94 | has 'last_delivery_tag' => ( | ||
| 95 | is => 'rw', | ||
| 96 | isa => 'Math::UInt64', | ||
| 97 | ); | ||
| 98 | |||
| 99 | |||
| 100 | has 'admin_queue' => ( | ||
| 101 | is => 'rw', | ||
| 102 | isa => 'Bool', | ||
| 103 | lazy => 1, | ||
| 104 | default => sub { 1 }, | ||
| 105 | ); | ||
| 106 | |||
| 107 | |||
| 108 | has 'reply_queue' => ( | ||
| 109 | is => 'rw', | ||
| 110 | lazy => 1, | ||
| 111 | clearer => 'no_reply', | ||
| 112 | predicate => 'wants_reply', | ||
| 113 | default => sub { undef }, | ||
| 114 | ); | ||
| 115 | |||
| 116 | |||
| 117 | has 'correlation_id' => ( | ||
| 118 | is => 'rw', | ||
| 119 | isa => 'Str', | ||
| 120 | clearer => 'no_correlation_id', | ||
| 121 | ); | ||
| 122 | |||
| 123 | |||
| 124 | has 'mq' => ( | ||
| 125 | is => 'rw', | ||
| 126 | isa => 'Net::AMQP::RabbitMQ', | ||
| 127 | lazy => 1, | ||
| 128 | default => sub { Net::AMQP::RabbitMQ->new }, | ||
| 129 | ); | ||
| 130 | |||
| 131 | |||
| 132 | after 'configure' => sub { | ||
| 133 | my ($self, $reconfig) = @_; | ||
| 134 | |||
| 135 | $self->log("configuring RabbitMQ plugin") if $self->debug; | ||
| 136 | |||
| 137 | if ($reconfig) { | ||
| 138 | $self->log("closing channel " . $self->rabbit_channel) if $self->debug; | ||
| 139 | $self->mq->channel_close($self->rabbit_channel); | ||
| 140 | $self->log("disconnecting from rabbitMQ server: " . $self->rabbit_host) | ||
| 141 | if $self->debug; | ||
| 142 | $self->mq->disconnect; | ||
| 143 | $self->mq(Net::AMQP::RabbitMQ->new); | ||
| 144 | } | ||
| 145 | |||
| 146 | $self->_setup_rabbit_connection; | ||
| 147 | |||
| 148 | return; | ||
| 149 | }; | ||
| 150 | |||
| 151 | |||
| 152 | sub dont_reply { | ||
| 153 | my ($self) = @_; | ||
| 154 | |||
| 155 | $self->no_reply; | ||
| 156 | $self->no_correlation_id; | ||
| 157 | |||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | sub queue { | ||
| 163 | my ($self, $queue, $hash, $reply_to, $exchange) = @_; | ||
| 164 | |||
| 165 | my $rpc; | ||
| 166 | $rpc = 1 if defined wantarray; | ||
| 167 | |||
| 168 | unless (defined $hash) { | ||
| 169 | $self->log("message hash missing"); | ||
| 170 | return; | ||
| 171 | } | ||
| 172 | |||
| 173 | unless ($queue) { | ||
| 174 | $self->log("target queue missing"); | ||
| 175 | return; | ||
| 176 | } | ||
| 177 | |||
| 178 | # we want the receiver to reply to same priority queue | ||
| 179 | $reply_to .= '.' . $hash->{meta}->{priority} | ||
| 180 | if defined $reply_to | ||
| 181 | and exists $hash->{meta}->{priority} | ||
| 182 | and $hash->{meta}->{priority} =~ m/^(high|low)$/; | ||
| 183 | |||
| 184 | my $tag; | ||
| 185 | my $reply_channel = $self->rabbit_channel + 1; | ||
| 186 | if ($rpc) { | ||
| 187 | try { | ||
| 188 | $self->mq->channel_open($reply_channel); | ||
| 189 | } | ||
| 190 | catch { | ||
| 191 | warn "Could not open channel on first try! >>" . $@ . "<<"; | ||
| 192 | $self->_setup_rabbit_connection; | ||
| 193 | $self->mq->channel_open($reply_channel); | ||
| 194 | }; | ||
| 195 | $self->log("opened channel $reply_channel for reply") if $self->debug; | ||
| 196 | |||
| 197 | $reply_to = | ||
| 198 | $self->mq->queue_declare($reply_channel, '', | ||
| 199 | { durable => 0, auto_delete => 1, exclusive => 1 }); | ||
| 200 | $self->log("declared reply queue $reply_to") if $self->debug; | ||
| 201 | |||
| 202 | $tag = $self->mq->consume($reply_channel, $reply_to); | ||
| 203 | $self->log("got consumer tag: " . $tag) if $self->debug; | ||
| 204 | } | ||
| 205 | |||
| 206 | my $props = { content_type => 'application/json' }; | ||
| 207 | $props->{reply_to} = $reply_to if defined $reply_to; | ||
| 208 | |||
| 209 | # If the queue we're sending to is Daemonise's reply_queue, then this is | ||
| 210 | # probably a response for an RPC message. Include the correlation_id | ||
| 211 | if (defined $self->reply_queue | ||
| 212 | and $queue eq $self->reply_queue) | ||
| 213 | { | ||
| 214 | $props->{correlation_id} = $self->correlation_id | ||
| 215 | if defined $self->correlation_id; | ||
| 216 | } | ||
| 217 | else { | ||
| 218 | # HACK: priorities should be implemented using rabbitMQ properties in the | ||
| 219 | # future, however for now we just rename the queue and hope... | ||
| 220 | $queue .= '.' . $hash->{meta}->{priority} | ||
| 221 | if ref $hash eq 'HASH' | ||
| 222 | and exists $hash->{meta} | ||
| 223 | and exists $hash->{meta}->{priority} | ||
| 224 | and $hash->{meta}->{priority} =~ m/^(high|low)$/; | ||
| 225 | } | ||
| 226 | |||
| 227 | my $options; | ||
| 228 | $options->{exchange} = $exchange if $exchange; | ||
| 229 | |||
| 230 | $self->log("sending message to '$queue' via channel " | ||
| 231 | . $self->rabbit_channel | ||
| 232 | . " using " | ||
| 233 | . ($exchange ? $exchange : 'amq.direct') . ": " | ||
| 234 | . $self->dump($hash)) | ||
| 235 | if $self->debug; | ||
| 236 | |||
| 237 | my $json = $js->encode($hash); | ||
| 238 | utf8::encode($json); | ||
| 239 | |||
| 240 | try { | ||
| 241 | $self->mq->publish($self->rabbit_channel, | ||
| 242 | $queue, $json, $options, $props); | ||
| 243 | } | ||
| 244 | catch { | ||
| 245 | $self->log( | ||
| 246 | "sending message to '$queue' failed on first try! >>" . $@ . "<<"); | ||
| 247 | $self->_setup_rabbit_connection; | ||
| 248 | $self->mq->publish($self->rabbit_channel, | ||
| 249 | $queue, $json, $options, $props); | ||
| 250 | }; | ||
| 251 | |||
| 252 | if ($rpc) { | ||
| 253 | my $msg = $self->dequeue($tag); | ||
| 254 | |||
| 255 | $self->mq->channel_close($reply_channel); | ||
| 256 | $self->log("closed reply channel $reply_channel") if $self->debug; | ||
| 257 | |||
| 258 | return $msg; | ||
| 259 | } | ||
| 260 | |||
| 261 | return; | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | sub dequeue { | ||
| 266 | my ($self, $tag) = @_; | ||
| 267 | |||
| 268 | # clear reply_queue if this not an RPC response | ||
| 269 | $self->dont_reply unless defined $tag; | ||
| 270 | |||
| 271 | my $frame; | ||
| 272 | my $msg; | ||
| 273 | while (1) { | ||
| 274 | try { | ||
| 275 | $frame = $self->mq->recv(); | ||
| 276 | } | ||
| 277 | catch { | ||
| 278 | warn "receiving message failed on first try! >>" . $@ . "<<"; | ||
| 279 | $self->_setup_rabbit_connection; | ||
| 280 | $frame = $self->mq->recv(); | ||
| 281 | }; | ||
| 282 | |||
| 283 | if ($frame) { | ||
| 284 | if (defined $tag) { | ||
| 285 | unless (($frame->{consumer_tag} eq $tag) | ||
| 286 | or ($frame->{consumer_tag} eq $self->rabbit_consumer_tag)) | ||
| 287 | { | ||
| 288 | $self->log("LOSING MESSAGE: " . $self->dump($frame)); | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | # ACK all admin messages no matter what | ||
| 293 | $self->mq->ack($self->rabbit_channel, $frame->{delivery_tag}) | ||
| 294 | if ($frame->{routing_key} =~ m/^admin/); | ||
| 295 | |||
| 296 | # decode | ||
| 297 | eval { $msg = $js->decode($frame->{body} || '{}'); }; | ||
| 298 | if ($@) { | ||
| 299 | $self->log("JSON parsing error: $@"); | ||
| 300 | $msg = {}; | ||
| 301 | } | ||
| 302 | |||
| 303 | $self->log("received message body: " . $self->dump($msg)) | ||
| 304 | if $self->debug; | ||
| 305 | |||
| 306 | last unless ($frame->{routing_key} =~ m/^admin/); | ||
| 307 | |||
| 308 | if ( ($frame->{routing_key} eq 'admin') | ||
| 309 | or ($frame->{routing_key} eq 'admin.' . $self->hostname)) | ||
| 310 | { | ||
| 311 | given ($msg->{command} || 'stop') { | ||
| 312 | when ('configure') { | ||
| 313 | $self->log("reconfiguring"); | ||
| 314 | $self->configure('reconfig'); | ||
| 315 | } | ||
| 316 | when ('stop') { | ||
| 317 | my $name = $self->name; | ||
| 318 | if (grep { $_ eq $name } | ||
| 319 | @{ $msg->{daemons} || [$name] }) | ||
| 320 | { | ||
| 321 | $self->stop; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | } | ||
| 325 | } | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | # store delivery tag to ack later | ||
| 330 | $self->last_delivery_tag($frame->{delivery_tag}) unless $tag; | ||
| 331 | $self->reply_queue($frame->{props}->{reply_to}) | ||
| 332 | if exists $frame->{props}->{reply_to}; | ||
| 333 | $self->correlation_id($frame->{props}->{correlation_id}) | ||
| 334 | if exists $frame->{props}->{correlation_id}; | ||
| 335 | |||
| 336 | return $msg; | ||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | sub ack { | ||
| 341 | my ($self) = @_; | ||
| 342 | |||
| 343 | $self->log("acknowledging AMQP message") if $self->debug; | ||
| 344 | $self->mq->ack($self->rabbit_channel, $self->last_delivery_tag); | ||
| 345 | |||
| 346 | return; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub _setup_rabbit_connection { | ||
| 350 | my $self = shift; | ||
| 351 | |||
| 352 | if (ref($self->config->{rabbitmq}) eq 'HASH') { | ||
| 353 | foreach | ||
| 354 | my $conf_key ('user', 'pass', 'host', 'port', 'vhost', 'exchange') | ||
| 355 | { | ||
| 356 | my $attr = "rabbit_" . $conf_key; | ||
| 357 | $self->$attr($self->config->{rabbitmq}->{$conf_key}) | ||
| 358 | if defined $self->config->{rabbitmq}->{$conf_key}; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | eval { | ||
| 363 | $self->mq->connect( | ||
| 364 | $self->rabbit_host, { | ||
| 365 | user => $self->rabbit_user, | ||
| 366 | password => $self->rabbit_pass, | ||
| 367 | port => $self->rabbit_port, | ||
| 368 | vhost => $self->rabbit_vhost, | ||
| 369 | }); | ||
| 370 | }; | ||
| 371 | my $err = $@; | ||
| 372 | |||
| 373 | if ($err) { | ||
| 374 | confess "Could not connect to the RabbitMQ server '" | ||
| 375 | . $self->rabbit_host | ||
| 376 | . "': $err\n"; | ||
| 377 | } | ||
| 378 | |||
| 379 | $self->mq->channel_open($self->rabbit_channel); | ||
| 380 | $self->log("opened channel " . $self->rabbit_channel) if $self->debug; | ||
| 381 | |||
| 382 | return unless $self->is_worker; | ||
| 383 | |||
| 384 | $self->log("preparing worker queue") if $self->debug; | ||
| 385 | |||
| 386 | $self->mq->basic_qos($self->rabbit_channel, { prefetch_count => 1 }); | ||
| 387 | $self->mq->queue_declare($self->rabbit_channel, $self->name, | ||
| 388 | { durable => 1, auto_delete => 0 }); | ||
| 389 | $self->mq->queue_bind( | ||
| 390 | $self->rabbit_channel, $self->name, | ||
| 391 | $self->rabbit_exchange, $self->name, | ||
| 392 | ); | ||
| 393 | $self->rabbit_consumer_tag( | ||
| 394 | $self->mq->consume($self->rabbit_channel, $self->name, { no_ack => 0 }) | ||
| 395 | ); | ||
| 396 | $self->log("got consumer tag: " . $self->rabbit_consumer_tag) | ||
| 397 | if $self->debug; | ||
| 398 | |||
| 399 | # setup admin queue per worker using z.queue.host.pid | ||
| 400 | # and bind to fanout exchange | ||
| 401 | if ($self->admin_queue) { | ||
| 402 | my $admin_queue = join('.', 'z', $self->name, $self->hostname, $$); | ||
| 403 | |||
| 404 | $self->log("declaring and binding admin queue " . $admin_queue) | ||
| 405 | if $self->debug; | ||
| 406 | |||
| 407 | $self->mq->queue_declare($self->rabbit_channel, $admin_queue, | ||
| 408 | { durable => 0, auto_delete => 1 }); | ||
| 409 | $self->mq->queue_bind($self->rabbit_channel, $admin_queue, 'amq.fanout', | ||
| 410 | 'admin'); | ||
| 411 | $self->mq->queue_bind($self->rabbit_channel, $admin_queue, 'amq.fanout', | ||
| 412 | 'admin.' . $self->hostname); | ||
| 413 | $self->mq->consume($self->rabbit_channel, $admin_queue, | ||
| 414 | { no_ack => 0 }); | ||
| 415 | } | ||
| 416 | |||
| 417 | return; | ||
| 418 | } | ||
| 419 | |||
| 420 | 1; | ||
| 421 | |||
| 422 | __END__ |