| 1 | package Net::Async::AMQP::RPC::Base; | ||||||||||||||||||||||||||||
| 2 | 3 | $Net::Async::AMQP::RPC::Base::VERSION = '2.000'; | |||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Net::Async::AMQP::RPC::Base - base class for client and server RPC handling | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | version 2.000 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | This is used internally by L<Net::Async::AMQP::RPC::Server> and | ||||||||||||||||||||||||||||
| 19 | L<Net::Async::AMQP::RPC::Client>, see those classes for details. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use Net::Async::AMQP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | ▶ | use Variable::Disposition qw(retain_future); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 3 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 3 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head2 mq | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | Returns the L<Net::Async::AMQP> instance. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub mq { | ||||||||||||||||||||||||||
|
Subroutine
mq: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | return $self->{mq} if $self->{mq}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | $log->debugf("Establishing connection to MQ server at %s:%s", $self->{host}, $self->{port}); | |||||||||||||||||||||||||||
| 39 | $self->add_child( | ||||||||||||||||||||||||||||
| 40 | 0 | $self->{mq} = my $mq = Net::Async::AMQP->new | |||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | $mq->connect( | ||||||||||||||||||||||||||||
| 43 | host => $self->{host}, | ||||||||||||||||||||||||||||
| 44 | user => $self->{user}, | ||||||||||||||||||||||||||||
| 45 | pass => $self->{pass}, | ||||||||||||||||||||||||||||
| 46 | port => $self->{port}, | ||||||||||||||||||||||||||||
| 47 | vhost => $self->{vhost}, | ||||||||||||||||||||||||||||
| 48 | 0 | client_properties => { | |||||||||||||||||||||||||||
| 49 | capabilities => { | ||||||||||||||||||||||||||||
| 50 | 'consumer_cancel_notify' => Net::AMQP::Value->true, | ||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | 0 | $mq | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head2 queue_name | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Returns a L<Future> which resolves to the queue name once the queue has been declared. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub queue_name { | ||||||||||||||||||||||||||
|
Subroutine
queue_name: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $_[0]->{queue_name} ||= $_[0]->future(set_label => 'queue name')->on_done(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 0 | $log->infof("Queue name is %s", shift); | |||||||||||||||||||||||||||
| 66 | }) | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | } | ||||||||||||||||||||||||||
|
Condition0%0/3
$_[0]{"queue_name"} ||= $_[0]->future("set_label", "queue name")->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->infof("Queue name is %s", shift());
}
)
MC/DC0%0/2
$_[0]{"queue_name"} ||= $_[0]->future("set_label", "queue name")->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->infof("Queue name is %s", shift());
}
)
$_[0]{"queue_name"}
$_[0]->future("set_label", "queue name")->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->infof("Queue name is %s", shift());
}
)
Truth table0%0/3
A ||= B
A= $_[0]{"queue_name"}B= $_[0]->future("set_label", "queue name")->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->infof("Queue name is %s", shift());
}
)
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head2 routing_key | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | The routing key used for publishing. Defaults to the empty string. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub routing_key { '' } | ||||||||||||||||||||||||||
|
Subroutine
routing_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head2 exchange | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | The exchange messages should be published to (or queues bound to). | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub exchange { shift->{exchange} } | ||||||||||||||||||||||||||
|
Subroutine
exchange: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head2 future | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Helper method for instantiating a L<Future>. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub future { my $self = shift; $self->mq->future(@_) } | ||||||||||||||||||||||||||
|
Subroutine
future: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 _add_to_loop | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Called when this instance is added to a L<IO::Async::Loop>. Requires both an | ||||||||||||||||||||||||||||
| 96 | L</mq> instance and a valid L</exchange> name. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub _add_to_loop { | ||||||||||||||||||||||||||
|
Subroutine
_add_to_loop: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 101 | 0 | my ($self, $loop) = @_; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | die "Need an MQ connection" unless $self->mq; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | die "Need an exchange name" unless defined $self->{exchange}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | retain_future( | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $self->connected->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 106 | 0 | $log->debug("Connected to MQ server, activating consumer"); | |||||||||||||||||||||||||||
| 107 | 0 | $self->consumer | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | })->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 109 | 0 | $log->info("Ready for requests"); | |||||||||||||||||||||||||||
| 110 | 0 | $self->active->done | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | })->on_fail(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 112 | 0 | $log->errorf("Failure: %s", shift); | |||||||||||||||||||||||||||
| 113 | }) | ||||||||||||||||||||||||||||
| 114 | 0 | ) | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head2 connected | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Returns a L<Future> which resolves once the underlying L<Net::Async::AMQP> connection | ||||||||||||||||||||||||||||
| 120 | is established. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub connected { shift->mq->connected } | ||||||||||||||||||||||||||
|
Subroutine
connected: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 client_queue | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Sets up a queue for an RPC client. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub client_queue { | ||||||||||||||||||||||||||
|
Subroutine
client_queue: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | $self->{client_queue} ||= $self->consumer_channel->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 135 | 0 | my ($ch) = @_; | |||||||||||||||||||||||||||
| 136 | 0 | $log->debug("Declaring queue"); | |||||||||||||||||||||||||||
| 137 | $ch->queue_declare( | ||||||||||||||||||||||||||||
| 138 | queue => $self->{queue} // '', | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | )->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 140 | 0 | my ($q) = @_; | |||||||||||||||||||||||||||
| 141 | 0 | $self->queue_name->done($q->queue_name); | |||||||||||||||||||||||||||
| 142 | 0 | Future->done($q) | |||||||||||||||||||||||||||
| 143 | }) | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | })->on_fail(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$self->{"queue"} // ""
MC/DC0%0/1
$self->{"queue"} // ""
$self->{"queue"}
Truth table0%0/2
A
A= $self->{"queue"}
| |||||||||||||||||||||||||||||
| 145 | 0 | $log->errorf("Failed to set up client queue: %s", shift) | |||||||||||||||||||||||||||
| 146 | }) | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | } | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"client_queue"} ||= $self->consumer_channel->then(sub {
my($ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Declaring queue");
$ch->queue_declare("queue", $self->{"queue"} // "")->then(sub {
my($q) = @_;
$self->queue_name->done($q->queue_name);
"Future"->done($q);
}
);
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up client queue: %s", shift());
}
)
MC/DC0%0/2
$self->{"client_queue"} ||= $self->consumer_channel->then(sub {
my($ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Declaring queue");
$ch->queue_declare("queue", $self->{"queue"} // "")->then(sub {
my($q) = @_;
$self->queue_name->done($q->queue_name);
"Future"->done($q);
}
);
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up client queue: %s", shift());
}
)
$self->{"client_queue"}
$self->consumer_channel->then(sub {
my($ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Declaring queue");
$ch->queue_declare("queue", $self->{"queue"} // "")->then(sub {
my($q) = @_;
$self->queue_name->done($q->queue_name);
"Future"->done($q);
}
);
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up client queue: %s", shift());
}
)
Truth table0%0/3
A ||= B
A= $self->{"client_queue"}B= $self->consumer_channel->then(sub {
my($ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Declaring queue");
$ch->queue_declare("queue", $self->{"queue"} // "")->then(sub {
my($q) = @_;
$self->queue_name->done($q->queue_name);
"Future"->done($q);
}
);
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up client queue: %s", shift());
}
)
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 server_queue | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Sets up a queue for an RPC server. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | sub server_queue { | ||||||||||||||||||||||||||
|
Subroutine
server_queue: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | $self->{server_queue} ||= $self->consumer_channel->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 158 | 0 | my ($ch) = @_; | |||||||||||||||||||||||||||
| 159 | 0 | $log->debug("Declaring queue"); | |||||||||||||||||||||||||||
| 160 | Future->needs_all( | ||||||||||||||||||||||||||||
| 161 | $ch->queue_declare( | ||||||||||||||||||||||||||||
| 162 | queue => $self->{queue} // '', | ||||||||||||||||||||||||||||
| 163 | ), | ||||||||||||||||||||||||||||
| 164 | $ch->exchange_declare( | ||||||||||||||||||||||||||||
| 165 | exchange => $self->{exchange}, | ||||||||||||||||||||||||||||
| 166 | type => 'topic', | ||||||||||||||||||||||||||||
| 167 | ) | ||||||||||||||||||||||||||||
| 168 | 0 | ▶ | )->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 169 | 0 | my ($q) = @_; | |||||||||||||||||||||||||||
| 170 | 0 | $self->queue_name->done($q->queue_name); | |||||||||||||||||||||||||||
| 171 | 0 | $log->debugf("Binding queue %s to exchange %s", $q->queue_name, $self->{exchange}); | |||||||||||||||||||||||||||
| 172 | $q->bind_exchange( | ||||||||||||||||||||||||||||
| 173 | channel => $ch, | ||||||||||||||||||||||||||||
| 174 | exchange => $self->{exchange}, | ||||||||||||||||||||||||||||
| 175 | routing_key => $self->{routing_key} // '', | ||||||||||||||||||||||||||||
| 176 | )->transform( | ||||||||||||||||||||||||||||
| 177 | 0 | ▶ | done => sub { $q } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 178 | ) | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | }) | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"routing_key"} // ""
MC/DC0%0/1
$self->{"routing_key"} // ""
$self->{"routing_key"}
Truth table0%0/2
A
A= $self->{"routing_key"}
| |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | })->on_fail(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$self->{"queue"} // ""
MC/DC0%0/1
$self->{"queue"} // ""
$self->{"queue"}
Truth table0%0/2
A
A= $self->{"queue"}
| |||||||||||||||||||||||||||||
| 181 | 0 | $log->errorf("Failed to set up server queue: %s", shift) | |||||||||||||||||||||||||||
| 182 | }) | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | } | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"server_queue"} ||= $self->consumer_channel->then(sub {
my($ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Declaring queue");
"Future"->needs_all($ch->queue_declare("queue", $self->{"queue"} // ""), $ch->exchange_declare("exchange", $self->{"exchange"}, "type", "topic"))->then(sub {
my($q) = @_;
$self->queue_name->done($q->queue_name);
$Net::Async::AMQP::RPC::Base::log->debugf("Binding queue %s to exchange %s", $q->queue_name, $self->{"exchange"});
$q->bind_exchange("channel", $ch, "exchange", $self->{"exchange"}, "routing_key", $self->{"routing_key"} // "")->transform("done", sub {
$q;
}
);
}
);
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up server queue: %s", shift());
}
)
MC/DC0%0/2
$self->{"server_queue"} ||= $self->consumer_channel->then(sub {
my($ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Declaring queue");
"Future"->needs_all($ch->queue_declare("queue", $self->{"queue"} // ""), $ch->exchange_declare("exchange", $self->{"exchange"}, "type", "topic"))->then(sub {
my($q) = @_;
$self->queue_name->done($q->queue_name);
$Net::Async::AMQP::RPC::Base::log->debugf("Binding queue %s to exchange %s", $q->queue_name, $self->{"exchange"});
$q->bind_exchange("channel", $ch, "exchange", $self->{"exchange"}, "routing_key", $self->{"routing_key"} // "")->transform("done", sub {
$q;
}
);
}
);
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up server queue: %s", shift());
}
)
$self->{"server_queue"}
$self->consumer_channel->then(sub {
my($ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Declaring queue");
"Future"->needs_all($ch->queue_declare("queue", $self->{"queue"} // ""), $ch->exchange_declare("exchange", $self->{"exchange"}, "type", "topic"))->then(sub {
my($q) = @_;
$self->queue_name->done($q->queue_name);
$Net::Async::AMQP::RPC::Base::log->debugf("Binding queue %s to exchange %s", $q->queue_name, $self->{"exchange"});
$q->bind_exchange("channel", $ch, "exchange", $self->{"exchange"}, "routing_key", $self->{"routing_key"} // "")->transform("done", sub {
$q;
}
);
}
);
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up server queue: %s", shift());
}
)
Truth table0%0/3
A ||= B
A= $self->{"server_queue"}B= $self->consumer_channel->then(sub {
my($ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Declaring queue");
"Future"->needs_all($ch->queue_declare("queue", $self->{"queue"} // ""), $ch->exchange_declare("exchange", $self->{"exchange"}, "type", "topic"))->then(sub {
my($q) = @_;
$self->queue_name->done($q->queue_name);
$Net::Async::AMQP::RPC::Base::log->debugf("Binding queue %s to exchange %s", $q->queue_name, $self->{"exchange"});
$q->bind_exchange("channel", $ch, "exchange", $self->{"exchange"}, "routing_key", $self->{"routing_key"} // "")->transform("done", sub {
$q;
}
);
}
);
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up server queue: %s", shift());
}
)
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head2 reply | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Publishes a reply to an RPC message. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Expects the following: | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =over 4 | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =item * reply_to - which queue to deliver to | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item * correlation_id - the ID to use for this message | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =item * type - message type | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =item * payload - message content | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =item * content_type - what's in the message | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =item * content_encoding - any encoding layers | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =back | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub reply { | ||||||||||||||||||||||||||
|
Subroutine
reply: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 211 | 0 | ▶ | $self->publisher_channel->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 212 | 0 | my ($ch) = @_; | |||||||||||||||||||||||||||
| 213 | $ch->publish( | ||||||||||||||||||||||||||||
| 214 | exchange => '', | ||||||||||||||||||||||||||||
| 215 | routing_key => $args{reply_to}, | ||||||||||||||||||||||||||||
| 216 | delivery_mode => 2, # persistent | ||||||||||||||||||||||||||||
| 217 | correlation_id => $args{correlation_id}, | ||||||||||||||||||||||||||||
| 218 | type => $args{type}, | ||||||||||||||||||||||||||||
| 219 | payload => $args{payload}, | ||||||||||||||||||||||||||||
| 220 | content_type => $args{content_type} // 'application/binary', | ||||||||||||||||||||||||||||
| 221 | content_encoding => $args{content_encoding}, | ||||||||||||||||||||||||||||
| 222 | ) | ||||||||||||||||||||||||||||
| 223 | 0 | ▶ | }); | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"content_type"} // "application/binary"
MC/DC0%0/1
$args{"content_type"} // "application/binary"
$args{"content_type"}
Truth table0%0/2
A
A= $args{"content_type"}
| |||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head2 consumer | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Activates a consumer. Resolves when the consumer is running. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | ▶ | sub consumer { | ||||||||||||||||||||||||||
|
Subroutine
consumer: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 234 | $self->{consumer} ||= Future->needs_all( | ||||||||||||||||||||||||||||
| 235 | $self->queue, | ||||||||||||||||||||||||||||
| 236 | $self->consumer_channel | ||||||||||||||||||||||||||||
| 237 | 0 | ▶ | )->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 238 | 0 | my ($q, $ch) = @_; | |||||||||||||||||||||||||||
| 239 | 0 | $log->debug("Starting consumer"); | |||||||||||||||||||||||||||
| 240 | 0 | $q->consumer( | |||||||||||||||||||||||||||
| 241 | channel => $ch, | ||||||||||||||||||||||||||||
| 242 | ack => 1, | ||||||||||||||||||||||||||||
| 243 | on_message => $self->curry::weak::on_message($ch), | ||||||||||||||||||||||||||||
| 244 | ) | ||||||||||||||||||||||||||||
| 245 | 0 | ▶ | })->on_fail(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 246 | 0 | $log->errorf("Failed to set up consumer: %s", shift) | |||||||||||||||||||||||||||
| 247 | }) | ||||||||||||||||||||||||||||
| 248 | 0 | ▶ | } | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"consumer"} ||= "Future"->needs_all($self->queue, $self->consumer_channel)->then(sub {
my($q, $ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Starting consumer");
$q->consumer("channel", $ch, "ack", 1, "on_message", $self->curry::weak::on_message($ch));
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up consumer: %s", shift());
}
)
MC/DC0%0/2
$self->{"consumer"} ||= "Future"->needs_all($self->queue, $self->consumer_channel)->then(sub {
my($q, $ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Starting consumer");
$q->consumer("channel", $ch, "ack", 1, "on_message", $self->curry::weak::on_message($ch));
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up consumer: %s", shift());
}
)
$self->{"consumer"}
"Future"->needs_all($self->queue, $self->consumer_channel)->then(sub {
my($q, $ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Starting consumer");
$q->consumer("channel", $ch, "ack", 1, "on_message", $self->curry::weak::on_message($ch));
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up consumer: %s", shift());
}
)
Truth table0%0/3
A ||= B
A= $self->{"consumer"}B= "Future"->needs_all($self->queue, $self->consumer_channel)->then(sub {
my($q, $ch) = @_;
$Net::Async::AMQP::RPC::Base::log->debug("Starting consumer");
$q->consumer("channel", $ch, "ack", 1, "on_message", $self->curry::weak::on_message($ch));
}
)->on_fail(sub {
$Net::Async::AMQP::RPC::Base::log->errorf("Failed to set up consumer: %s", shift());
}
)
| |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =head2 on_message | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | Called when there's a message. Receives the L<Net::Async::AMQP::Channel> followed by some named parameters: | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =over 4 | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =item * type | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =item * payload - scalar containing the raw binary data for this message | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | =item * consumer_tag - which consumer tag received this message | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =item * delivery_tag - the delivery information for L<Net::Async::AMQP::Channel/ack> | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =item * routing_key - routing key used for this message | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =item * properties - any properties for the message | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =item * headers - custom headers | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =back | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | See L<Net::Async::AMQP::Queue/consumer> for more details (including the contents of C<properties> and C<headers>). | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =cut | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | ▶ | sub on_message { | ||||||||||||||||||||||||||
|
Subroutine
on_message: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 277 | 0 | my ($self, $ch, %args) = @_; | |||||||||||||||||||||||||||
| 278 | # { my %x = %{$args{properties}}; $log->debugf("have: %s", join ',', map { $_ . '=' . $x{$_} } sort keys %x); } | ||||||||||||||||||||||||||||
| 279 | 0 | $log->debugf("Received message of type %s, correlation ID %s, reply_to %s", $args{type}, $args{properties}{correlation_id}, $args{properties}{reply_to}); | |||||||||||||||||||||||||||
| 280 | 0 | my $dtag = $args{delivery_tag}; | |||||||||||||||||||||||||||
| 281 | (eval { | ||||||||||||||||||||||||||||
| 282 | my $f = $self->process_message( | ||||||||||||||||||||||||||||
| 283 | type => $args{type}, | ||||||||||||||||||||||||||||
| 284 | id => $args{properties}{correlation_id}, | ||||||||||||||||||||||||||||
| 285 | reply_to => $args{properties}{reply_to}, | ||||||||||||||||||||||||||||
| 286 | payload => $args{payload}, | ||||||||||||||||||||||||||||
| 287 | content_type => $args{properties}{content_type}, | ||||||||||||||||||||||||||||
| 288 | user_id => $args{properties}{user_id}, | ||||||||||||||||||||||||||||
| 289 | 0 | ); | |||||||||||||||||||||||||||
| 290 | 0 | ▶ | $f = Future->done($f) unless Scalar::Util::blessed($f) && $f->isa('Future'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($f) and $f->isa("Future")
MC/DC0%0/2
builtin::blessed($f) and $f->isa("Future")
builtin::blessed($f)
$f->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($f)B= $f->isa("Future")
| |||||||||||||||||||||||||||||
| 291 | 0 | $f | |||||||||||||||||||||||||||
| 292 | } or do { | ||||||||||||||||||||||||||||
| 293 | 0 | my $err = $@; | |||||||||||||||||||||||||||
| 294 | 0 | $log->errorf("Error processing: %s", $err); | |||||||||||||||||||||||||||
| 295 | 0 | Future->fail($err); | |||||||||||||||||||||||||||
| 296 | 0 | ▶ | })->on_ready(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 297 | $self->{pending}{$dtag} = $ch->ack( | ||||||||||||||||||||||||||||
| 298 | delivery_tag => $dtag | ||||||||||||||||||||||||||||
| 299 | 0 | ▶ | )->on_ready(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 300 | 0 | delete $self->{pending}{$dtag} | |||||||||||||||||||||||||||
| 301 | 0 | }); | |||||||||||||||||||||||||||
| 302 | }) | ||||||||||||||||||||||||||||
| 303 | 0 | ▶ | } | ||||||||||||||||||||||||||
|
Condition0%0/3
eval {
do {
my $f = $self->process_message('type', $args{'type'}, 'id', $args{'properties'}{'correlation_id'}, 'reply_to', $args{'properties'}{'reply_to'}, 'payload', $args{'payload'}, 'content_type', $args{'properties'}{'content_type'}, 'user_id', $args{'properties'}{'user_id'});
$f = 'Future'->done($f) unless builtin::blessed($f) and $f->isa('Future');
$f
}
} || do {
my $err = $@;
$Net::Async::AMQP::RPC::Base::log->errorf('Error processing: %s', $err);
'Future'->fail($err)
}
MC/DC0%0/2
eval {
do {
my $f = $self->process_message('type', $args{'type'}, 'id', $args{'properties'}{'correlation_id'}, 'reply_to', $args{'properties'}{'reply_to'}, 'payload', $args{'payload'}, 'content_type', $args{'properties'}{'content_type'}, 'user_id', $args{'properties'}{'user_id'});
$f = 'Future'->done($f) unless builtin::blessed($f) and $f->isa('Future');
$f
}
} || do {
my $err = $@;
$Net::Async::AMQP::RPC::Base::log->errorf('Error processing: %s', $err);
'Future'->fail($err)
}
eval {
do {
my $f = $self->process_message('type', $args{'type'}, 'id', $args{'properties'}{'correlation_id'}, 'reply_to', $args{'properties'}{'reply_to'}, 'payload', $args{'payload'}, 'content_type', $args{'properties'}{'content_type'}, 'user_id', $args{'properties'}{'user_id'});
$f = 'Future'->done($f) unless builtin::blessed($f) and $f->isa('Future');
$f
}
}
do {
my $err = $@;
$Net::Async::AMQP::RPC::Base::log->errorf('Error processing: %s', $err);
'Future'->fail($err)
}
Truth table0%0/3
A || B
A= eval {
do {
my $f = $self->process_message('type', $args{'type'}, 'id', $args{'properties'}{'correlation_id'}, 'reply_to', $args{'properties'}{'reply_to'}, 'payload', $args{'payload'}, 'content_type', $args{'properties'}{'content_type'}, 'user_id', $args{'properties'}{'user_id'});
$f = 'Future'->done($f) unless builtin::blessed($f) and $f->isa('Future');
$f
}
}B= do {
my $err = $@;
$Net::Async::AMQP::RPC::Base::log->errorf('Error processing: %s', $err);
'Future'->fail($err)
}
| |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =head2 process_message | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | Abstract method for message processing. Will receive the following named parameters: | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | The base implementation here will raise an exception. Override this in your subclass | ||||||||||||||||||||||||||||
| 310 | to do something more useful. | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =cut | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 0 | ▶ | sub process_message { die 'abstract method ->process_message called - please subclass and override' } | ||||||||||||||||||||||||||
|
Subroutine
process_message: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =head2 consumer_channel | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | Returns a L<Future> which resolves to the L<Net::Async::AMQP::Channel> used for the consumer. | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | =cut | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 0 | ▶ | sub consumer_channel { | ||||||||||||||||||||||||||
|
Subroutine
consumer_channel: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 323 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 324 | 0 | ▶ | $self->{consumer_channel} ||= $self->connected->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 325 | 0 | $self->mq->open_channel | |||||||||||||||||||||||||||
| 326 | 0 | ▶ | })->on_done(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 327 | 0 | $log->debugf("Receiver channel ID %d", shift->id); | |||||||||||||||||||||||||||
| 328 | }) | ||||||||||||||||||||||||||||
| 329 | 0 | ▶ | } | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"consumer_channel"} ||= $self->connected->then(sub {
$self->mq->open_channel;
}
)->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->debugf("Receiver channel ID %d", (shift())->id);
}
)
MC/DC0%0/2
$self->{"consumer_channel"} ||= $self->connected->then(sub {
$self->mq->open_channel;
}
)->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->debugf("Receiver channel ID %d", (shift())->id);
}
)
$self->{"consumer_channel"}
$self->connected->then(sub {
$self->mq->open_channel;
}
)->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->debugf("Receiver channel ID %d", (shift())->id);
}
)
Truth table0%0/3
A ||= B
A= $self->{"consumer_channel"}B= $self->connected->then(sub {
$self->mq->open_channel;
}
)->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->debugf("Receiver channel ID %d", (shift())->id);
}
)
| |||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =head2 publisher_channel | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | Returns a L<Future> which resolves to the L<Net::Async::AMQP::Channel> used for the publisher. | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =cut | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 0 | ▶ | sub publisher_channel { | ||||||||||||||||||||||||||
|
Subroutine
publisher_channel: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 338 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 339 | 0 | ▶ | $self->{consumer_channel} ||= $self->connected->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 340 | 0 | $self->mq->open_channel | |||||||||||||||||||||||||||
| 341 | 0 | ▶ | })->on_done(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 342 | 0 | $log->debugf("Sender channel ID %d", shift->id); | |||||||||||||||||||||||||||
| 343 | }) | ||||||||||||||||||||||||||||
| 344 | 0 | ▶ | } | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"consumer_channel"} ||= $self->connected->then(sub {
$self->mq->open_channel;
}
)->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->debugf("Sender channel ID %d", (shift())->id);
}
)
MC/DC0%0/2
$self->{"consumer_channel"} ||= $self->connected->then(sub {
$self->mq->open_channel;
}
)->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->debugf("Sender channel ID %d", (shift())->id);
}
)
$self->{"consumer_channel"}
$self->connected->then(sub {
$self->mq->open_channel;
}
)->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->debugf("Sender channel ID %d", (shift())->id);
}
)
Truth table0%0/3
A ||= B
A= $self->{"consumer_channel"}B= $self->connected->then(sub {
$self->mq->open_channel;
}
)->on_done(sub {
$Net::Async::AMQP::RPC::Base::log->debugf("Sender channel ID %d", (shift())->id);
}
)
| |||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =head2 active | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | Returns a L<Future> which resolves when the underlying MQ connection is ready for use. | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | =cut | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 0 | ▶ | sub active { | ||||||||||||||||||||||||||
|
Subroutine
active: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 353 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 354 | 0 | ▶ | $self->{active} ||= $self->mq->future | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"active"} ||= $self->mq->future
MC/DC0%0/2
$self->{"active"} ||= $self->mq->future
$self->{"active"}
$self->mq->future
Truth table0%0/3
A ||= B
A= $self->{"active"}B= $self->mq->future
| |||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 0 | ▶ | sub configure { | ||||||||||||||||||||||||||
|
Subroutine
configure: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 358 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 359 | 0 | for (qw(mq user pass host port vhost mq queue exchange)) { | |||||||||||||||||||||||||||
| 360 | 0 | ▶ | $self->{$_} = delete $args{$_} if exists $args{$_} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | 0 | $self->SUPER::configure(%args) | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 3 | 1; | |||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | __END__ | ||||||||||||||||||||||||||||