| 1 | package AMQP::Subscriber; | ||||||||||
| 2 | 1 | our $VERSION = '0.01'; | |||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Mojo::Base 'AMQP'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use AnyEvent::RabbitMQ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Sys::Hostname; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | has 'debug' => 1; | |||||||||
| 9 | 1 | has 'host' => 'localhost'; | |||||||||
| 10 | 1 | has 'port' => 5672; | |||||||||
| 11 | 1 | has 'username' => 'guest'; | |||||||||
| 12 | 1 | has 'password' => 'guest'; | |||||||||
| 13 | 1 | has 'vhost' => '/'; | |||||||||
| 14 | 1 | has 'timeout' => 1; | |||||||||
| 15 | 1 | has 'heartbeat' => 30; | |||||||||
| 16 | 1 | has 'exchange' => 'test'; | |||||||||
| 17 | 1 | has 'type' => 'topic'; | |||||||||
| 18 | 1 | has 'key' => '#'; | |||||||||
| 19 | 1 | has 'queue' => 'test'; | |||||||||
| 20 | 1 | has 'rabbit'; | |||||||||
| 21 | 1 | has 'connection'; | |||||||||
| 22 | 1 | has 'channel'; | |||||||||
| 23 | 1 | has 'status'; | |||||||||
| 24 | 1 | has 'tag' => $ENV{LOGNAME} . "@" . hostname; | |||||||||
| 25 | 1 | has 'on_message'; | |||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | sub attach { | ||||||||
|
Subroutine
attach: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 28 | 0 | my $self = shift; | |||||||||
| 29 | 0 | $self->useragent(Mojo::UserAgent->new); | |||||||||
| 30 | 0 | $self->status(AnyEvent->condvar); | |||||||||
| 31 | 0 | $self->rabbit(AnyEvent::RabbitMQ->new); | |||||||||
| 32 | 0 | $self->rabbit->load_xml_spec(); | |||||||||
| 33 | $self->rabbit->connect( | ||||||||||
| 34 | host => $self->host, | ||||||||||
| 35 | port => $self->port, | ||||||||||
| 36 | username => $self->username, | ||||||||||
| 37 | pass => $self->password, | ||||||||||
| 38 | vhost => $self->vhost, | ||||||||||
| 39 | timeout => $self->timeout, | ||||||||||
| 40 | tune => { heartbeat => $self->heartbeat }, | ||||||||||
| 41 | 0 | ▶ | on_success => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 42 | 0 | ▶ | say "Connected to amqp://" . $self->host . ":" . $self->port . $self->vhost if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | $self->connection(shift); | |||||||||
| 44 | $self->connection->open_channel( | ||||||||||
| 45 | on_failure => $self->status, | ||||||||||
| 46 | 0 | ▶ | on_close => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 47 | 0 | ▶ | say "Channel closed" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | $self->status->send; | |||||||||
| 49 | }, | ||||||||||
| 50 | 0 | ▶ | on_success => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 51 | 0 | ▶ | say "Opened channel" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | $self->channel(shift); | |||||||||
| 53 | $self->channel->declare_exchange( | ||||||||||
| 54 | exchange => $self->exchange, | ||||||||||
| 55 | type => $self->type, | ||||||||||
| 56 | auto_delete => 1, | ||||||||||
| 57 | on_failure => $self->status, | ||||||||||
| 58 | 0 | ▶ | on_success => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 59 | 0 | ▶ | say "Declared exchange " . $self->exchange if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | $self->channel->declare_queue( | ||||||||||
| 61 | queue => $self->queue, | ||||||||||
| 62 | auto_delete => 1, | ||||||||||
| 63 | on_failure => $self->status, | ||||||||||
| 64 | 0 | ▶ | on_success => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 65 | 0 | ▶ | say "Declared queue " . $self->queue if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | $self->channel->bind_queue( | ||||||||||
| 67 | queue => $self->queue, | ||||||||||
| 68 | exchange => $self->exchange, | ||||||||||
| 69 | routing_key => $self->key, | ||||||||||
| 70 | on_failure => $self->status, | ||||||||||
| 71 | 0 | ▶ | on_success => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 72 | 0 | ▶ | say "Bound " . $self->queue . " to " . $self->exchange . " " . $self->key if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | $self->channel->consume( | ||||||||||
| 74 | consumer_tag => $self->tag, | ||||||||||
| 75 | 0 | ▶ | on_success => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 76 | 0 | ▶ | say 'Consuming from ' . $self->queue if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | }, | ||||||||||
| 78 | 0 | ▶ | on_consume => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 79 | 0 | my $msg = shift; | |||||||||
| 80 | 0 | $self->on_message->($self,$msg); | |||||||||
| 81 | }, | ||||||||||
| 82 | 0 | ▶ | on_cancel => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 83 | 0 | ▶ | say "Consumption canceled" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | $self->status->send; | |||||||||
| 85 | }, | ||||||||||
| 86 | 0 | on_failure => $self->status, | |||||||||
| 87 | ); | ||||||||||
| 88 | } | ||||||||||
| 89 | 0 | ); | |||||||||
| 90 | } | ||||||||||
| 91 | 0 | ); | |||||||||
| 92 | } | ||||||||||
| 93 | 0 | ); | |||||||||
| 94 | }, | ||||||||||
| 95 | 0 | ); | |||||||||
| 96 | }, | ||||||||||
| 97 | on_failure => $self->status, | ||||||||||
| 98 | 0 | ▶ | on_read_failure => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 99 | 0 | ▶ | say "Failed to read" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | $self->status->send; | |||||||||
| 101 | }, | ||||||||||
| 102 | 0 | ▶ | on_return => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 103 | 0 | ▶ | say "Failed to send" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | $self->status->send; | |||||||||
| 105 | }, | ||||||||||
| 106 | 0 | ▶ | on_close => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 107 | 0 | ▶ | say "Connection closed" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 0 | $self->status->send; | |||||||||
| 109 | } | ||||||||||
| 110 | 0 | ); | |||||||||
| 111 | 0 | $self->status->recv; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | 1 | 1; | |||||||||
| 116 | |||||||||||
| 117 | __END__ | ||||||||||