| 1 | package AnyEvent::RabbitMQ::RPC; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use AnyEvent::RabbitMQ; | ||
| 7 | use Try::Tiny; | ||
| 8 | |||
| 9 | our $VERSION = '0.5'; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $class = shift; | ||
| 13 | my %args = @_; | ||
| 14 | |||
| 15 | my $self = bless {}, $class; | ||
| 16 | |||
| 17 | my $cv = AE::cv; | ||
| 18 | my $success = $args{on_success} || $cv; | ||
| 19 | my $failure = $args{on_failure} || sub { | ||
| 20 | warn "@_"; | ||
| 21 | $cv->(undef); | ||
| 22 | }; | ||
| 23 | |||
| 24 | $self->{connection} = $args{connection}; | ||
| 25 | my $channel = sub { | ||
| 26 | $self->connection->open_channel( | ||
| 27 | on_success => sub { | ||
| 28 | $self->{channel} = shift; | ||
| 29 | $self->{channel}->qos; | ||
| 30 | $success->($self); | ||
| 31 | }, | ||
| 32 | on_failure => sub { | ||
| 33 | $failure->("Channel failed: @_"); | ||
| 34 | } | ||
| 35 | ); | ||
| 36 | }; | ||
| 37 | if ($self->connection) { | ||
| 38 | $channel->(); | ||
| 39 | } else { | ||
| 40 | AnyEvent::RabbitMQ->load_xml_spec; | ||
| 41 | $self->{connection} = AnyEvent::RabbitMQ->new(timeout => 1, verbose => 0); | ||
| 42 | $self->connection->connect( | ||
| 43 | %args, | ||
| 44 | on_success => $channel, | ||
| 45 | on_failure => sub { | ||
| 46 | $failure->("Connect failed: @_"); | ||
| 47 | } | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | $args{serialize} ||= ''; | ||
| 52 | if ($args{serialize} eq "YAML") { | ||
| 53 | require YAML::Any; | ||
| 54 | $self->{serialize} = \&YAML::Any::Dump; | ||
| 55 | $self->{unserialize} = \&YAML::Any::Load; | ||
| 56 | } elsif ($args{serialize} eq "JSON") { | ||
| 57 | require JSON::Any; | ||
| 58 | JSON::Any->import; | ||
| 59 | my $json = JSON::Any->new; | ||
| 60 | $self->{serialize} = sub { $json->objToJson( [@_] ) }; | ||
| 61 | $self->{unserialize} = sub { (@{ $json->jsonToObj(@_) })[0] }; | ||
| 62 | } elsif ($args{serialize} eq "Storable") { | ||
| 63 | require Storable; | ||
| 64 | $self->{serialize} = sub { Storable::nfreeze( [@_] )}; | ||
| 65 | $self->{unserialize} = sub { (@{ Storable::thaw(@_) })[0] }; | ||
| 66 | } | ||
| 67 | |||
| 68 | # If they have a callback waiting for them, bail now | ||
| 69 | return if $args{on_success}; | ||
| 70 | |||
| 71 | # Otherwise, block on having set up the channel | ||
| 72 | return $cv->recv; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub connection { | ||
| 76 | my $self = shift; | ||
| 77 | return $self->{connection}; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub channel { | ||
| 81 | my $self = shift; | ||
| 82 | return $self->{channel}; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub rpc_queue { | ||
| 86 | my $self = shift; | ||
| 87 | my %args = @_; | ||
| 88 | |||
| 89 | # These queues are durable -- as such, we should only need to check | ||
| 90 | # that they are there once per process. | ||
| 91 | return $args{on_success}->() | ||
| 92 | if $self->{queues}{$args{queue}}; | ||
| 93 | |||
| 94 | $self->channel->declare_queue( | ||
| 95 | no_ack => 0, | ||
| 96 | durable => 1, | ||
| 97 | exclusive => 0, | ||
| 98 | %args, | ||
| 99 | on_success => sub { | ||
| 100 | $self->{queues}{$args{queue}}++; | ||
| 101 | $args{on_success}->(); | ||
| 102 | }, | ||
| 103 | ); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub reply_queue { | ||
| 107 | my $self = shift; | ||
| 108 | my %args = @_; | ||
| 109 | |||
| 110 | $self->channel->declare_queue( | ||
| 111 | no_ack => 1, | ||
| 112 | durable => 0, | ||
| 113 | exclusive => 1, | ||
| 114 | on_success => sub { | ||
| 115 | $args{on_success}->(shift->method_frame->queue); | ||
| 116 | }, | ||
| 117 | on_failure => $args{on_failure}, | ||
| 118 | ); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub register { | ||
| 122 | my $self = shift; | ||
| 123 | my %args = ( | ||
| 124 | name => undef, | ||
| 125 | run => sub {}, | ||
| 126 | on_failure => sub { warn "Failure: @_" }, | ||
| 127 | @_ | ||
| 128 | ); | ||
| 129 | |||
| 130 | # Ensure we have the queue | ||
| 131 | $self->rpc_queue( | ||
| 132 | queue => $args{name}, | ||
| 133 | on_success => sub { | ||
| 134 | # And set up a listen on it | ||
| 135 | $self->channel->consume( | ||
| 136 | queue => $args{name}, | ||
| 137 | no_ack => 0, | ||
| 138 | on_consume => sub { | ||
| 139 | my $frame = shift; | ||
| 140 | my $failed; | ||
| 141 | my $args = $frame->{body}->payload; | ||
| 142 | if ($self->{unserialize}) { | ||
| 143 | try { | ||
| 144 | $args = $self->{unserialize}->($args); | ||
| 145 | } catch { | ||
| 146 | $failed = 1; | ||
| 147 | $args{on_failure}->("Unserialization failed: $_"); | ||
| 148 | }; | ||
| 149 | return if $failed; | ||
| 150 | } | ||
| 151 | |||
| 152 | # Call the sub | ||
| 153 | my $return; | ||
| 154 | try { | ||
| 155 | $return = $args{run}->( $args ); | ||
| 156 | } catch { | ||
| 157 | $failed = 1; | ||
| 158 | $args{on_failure}->("Call died: $_"); | ||
| 159 | }; | ||
| 160 | return if $failed; | ||
| 161 | |||
| 162 | # Send the response, if they asked for it | ||
| 163 | if (my $reply_to = $frame->{header}->reply_to) { | ||
| 164 | if ($self->{serialize}) { | ||
| 165 | try { | ||
| 166 | $return = $self->{serialize}->($return); | ||
| 167 | } catch { | ||
| 168 | $failed = 1; | ||
| 169 | $args{on_failure}->("Serialization failed: $_"); | ||
| 170 | }; | ||
| 171 | return if $failed; | ||
| 172 | } | ||
| 173 | |||
| 174 | $return = "0E0" if not $return; | ||
| 175 | $self->channel->publish( | ||
| 176 | exchange => '', | ||
| 177 | routing_key => $reply_to, | ||
| 178 | body => $return, | ||
| 179 | ); | ||
| 180 | } | ||
| 181 | |||
| 182 | # And finally mark the task as complete | ||
| 183 | $self->channel->ack; | ||
| 184 | }, | ||
| 185 | on_failure => $args{on_failure}, | ||
| 186 | ); | ||
| 187 | }, | ||
| 188 | on_failure => $args{on_failure}, | ||
| 189 | ); | ||
| 190 | } | ||
| 191 | |||
| 192 | sub call { | ||
| 193 | my $self = shift; | ||
| 194 | |||
| 195 | my %args = ( | ||
| 196 | name => undef, | ||
| 197 | args => undef, | ||
| 198 | on_sent => undef, | ||
| 199 | on_failure => sub { warn "RPC Failure: @_" }, | ||
| 200 | @_ | ||
| 201 | ); | ||
| 202 | |||
| 203 | my $finished; | ||
| 204 | if (defined wantarray and not $args{on_reply}) { | ||
| 205 | # We we're called in a not-void context, and without a reply | ||
| 206 | # callback, assume this is a syncronous call, and set up | ||
| 207 | # $finished to block on the reply | ||
| 208 | $args{on_reply} = $finished = AE::cv; | ||
| 209 | my $fail = $args{on_failure}; | ||
| 210 | $args{on_failure} = sub { | ||
| 211 | $fail->(@_) if $fail; | ||
| 212 | $finished->send(undef); | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | my $sent_failure = $args{on_sent} ? sub { | ||
| 217 | $args{on_sent}->(0); | ||
| 218 | $args{on_failure}->(@_); | ||
| 219 | } : $args{on_failure}; | ||
| 220 | |||
| 221 | my $send; $send = sub { | ||
| 222 | my $REPLIES = shift; | ||
| 223 | my $args = $args{args}; | ||
| 224 | if ($self->{serialize}) { | ||
| 225 | my $failed; | ||
| 226 | try { | ||
| 227 | $args = $self->{serialize}->($args); | ||
| 228 | } catch { | ||
| 229 | $failed = 1; | ||
| 230 | $args{on_failure}->("Serialization failed: $_"); | ||
| 231 | }; | ||
| 232 | return if $failed; | ||
| 233 | } | ||
| 234 | $args = "0E0" if not $args; | ||
| 235 | $self->channel->publish( | ||
| 236 | exchange => '', | ||
| 237 | routing_key => $args{name}, | ||
| 238 | body => $args, | ||
| 239 | header => { | ||
| 240 | ($REPLIES ? (reply_to => $REPLIES) : ()), | ||
| 241 | delivery_mode => 2, # Persistent storage | ||
| 242 | }, | ||
| 243 | ); | ||
| 244 | $args{on_sent}->(1) if $args{on_sent}; | ||
| 245 | }; | ||
| 246 | |||
| 247 | unless ($args{on_reply}) { | ||
| 248 | # Fire and forget | ||
| 249 | $self->rpc_queue( | ||
| 250 | queue => $args{name}, | ||
| 251 | on_success => sub { $send->(undef) }, | ||
| 252 | on_failure => $sent_failure, | ||
| 253 | ); | ||
| 254 | return; | ||
| 255 | } | ||
| 256 | |||
| 257 | # We need to set up an ephemeral reply queue | ||
| 258 | $self->rpc_queue( | ||
| 259 | queue => $args{name}, | ||
| 260 | on_success => sub { | ||
| 261 | $self->reply_queue( | ||
| 262 | on_success => sub { | ||
| 263 | my $REPLIES = shift; | ||
| 264 | $self->channel->consume( | ||
| 265 | queue => $REPLIES, | ||
| 266 | no_ack => 1, | ||
| 267 | on_consume => sub { | ||
| 268 | my $frame = shift; | ||
| 269 | # We got a reply, tear down our reply queue | ||
| 270 | $self->channel->delete_queue( | ||
| 271 | queue => $REPLIES, | ||
| 272 | ); | ||
| 273 | my $return = $frame->{body}->payload; | ||
| 274 | if ($self->{unserialize}) { | ||
| 275 | my $failed; | ||
| 276 | try { | ||
| 277 | $return = $self->{unserialize}->($return); | ||
| 278 | } catch { | ||
| 279 | $args{on_failure}->("Unserialization failed: $_"); | ||
| 280 | $failed = 1; | ||
| 281 | }; | ||
| 282 | return if $failed; | ||
| 283 | } | ||
| 284 | $args{on_reply}->($return); | ||
| 285 | }, | ||
| 286 | on_success => sub { $send->($REPLIES) }, | ||
| 287 | on_failure => $sent_failure, | ||
| 288 | ); | ||
| 289 | }, | ||
| 290 | on_failure => $sent_failure, | ||
| 291 | ); | ||
| 292 | }, | ||
| 293 | on_failure => $sent_failure, | ||
| 294 | ); | ||
| 295 | |||
| 296 | return $finished->recv if $finished; | ||
| 297 | return 1; | ||
| 298 | } | ||
| 299 | |||
| 300 | 1; | ||
| 301 | |||
| 302 | __END__ |