| 1 | package McBain::WithZeroMQ; | ||
| 2 | |||
| 3 | # ABSTRACT: Load a McBain API as a ZeroMQ service | ||
| 4 | |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use 5.10.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use ZMQ::LibZMQ3; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use ZMQ::Constants qw(ZMQ_REP); | ||
| 13 | |||
| 14 | our $VERSION = "2.000000"; | ||
| 15 | $VERSION = eval $VERSION; | ||
| 16 | |||
| 17 | my $json = JSON->new->utf8->convert_blessed; | ||
| 18 | my $MAX_MSGLEN = 255; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | McBain::WithZeroMQ - Load a McBain API as a ZeroMQ service | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | # write your API as you normally would, and create | ||
| 27 | # a simple start-up script: | ||
| 28 | |||
| 29 | #!/usr/bin/perl -w | ||
| 30 | |||
| 31 | use warnings; | ||
| 32 | use strict; | ||
| 33 | use MyAPI -withZeroMQ; | ||
| 34 | |||
| 35 | MyAPI->work('localhost', 5560); | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | C<McBain::WithZeroMQ> turns your L<McBain> API into a L<ZeroMQ REP worker|ZMQ::LibZMQ3>, | ||
| 40 | making it easy to consume your APIs with ZMQ REQ clients. The generated worker code is based | ||
| 41 | on the L<request-reply worker example|http://zguide.zeromq.org/pl:rrworker> in the L<ZeroMQ guide|http://zguide.zeromq.org/page:all>. Note that the ZeroMQ request-reply pattern requires | ||
| 42 | a broker. The guide also has examples for L<a broker|http://zguide.zeromq.org/pl:rrbroker> | ||
| 43 | and L<a client|http://zguide.zeromq.org/pl:rrclient>, though two scripts based on these examples | ||
| 44 | are also provided with this module - more for example than actual usage - see L<mcbain-zmq-broker> | ||
| 45 | and L<mcbain-zmq-client>, respectively. | ||
| 46 | |||
| 47 | The workers created by this module receive payloads in JSON format, and convert them into the | ||
| 48 | hash-refs your API's methods expect to receive. The payload must have a C<path> key, which holds | ||
| 49 | the complete path of the route/method to invoke (for example, C<GET:/math/sum>). Results are sent | ||
| 50 | back to the clients in JSON as well. Note that if an API method does not return a hash-ref, this runner | ||
| 51 | module will automatically turn it into a hash-ref to ensure that conversion into JSON will | ||
| 52 | be possible. The created hash-ref will have one key - holding the method's name, with whatever | ||
| 53 | was returned from the method as its value. For example, if method C<GET:/divide> in topic | ||
| 54 | C</math> returns an integer (say 7), then the client will get the JSON C<{ "GET:/math/divide": 7 }>. | ||
| 55 | To avoid, make sure your API's methods return hash-refs. | ||
| 56 | |||
| 57 | =head1 METHODS EXPORTED TO YOUR API | ||
| 58 | |||
| 59 | =head2 work( [ $host, $port ] ) | ||
| 60 | |||
| 61 | Connects the ZeroMQ worker created by the module to the ZeroMQ broker | ||
| 62 | listening at the host and port provided. If none are provided, C<localhost> | ||
| 63 | and C<5560> are used, respectively. | ||
| 64 | |||
| 65 | The method never returns, so that the worker listens for jobs continuously. | ||
| 66 | |||
| 67 | =head1 METHODS REQUIRED BY MCBAIN | ||
| 68 | |||
| 69 | This runner module implements the following methods required by C<McBain>: | ||
| 70 | |||
| 71 | =head2 init( ) | ||
| 72 | |||
| 73 | Creates the L</"work( $host, $port )"> method for the root topic of the API. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub init { | ||
| 78 | my ($class, $target) = @_; | ||
| 79 | |||
| 80 | if ($target->is_root) { | ||
| 81 | no strict 'refs'; | ||
| 82 | *{"${target}::work"} = sub { | ||
| 83 | my ($pkg, $host, $port) = @_; | ||
| 84 | |||
| 85 | $host ||= 'localhost'; | ||
| 86 | $port ||= 5560; | ||
| 87 | |||
| 88 | my $context = zmq_init(); | ||
| 89 | |||
| 90 | # Socket to talk to clients | ||
| 91 | my $responder = zmq_socket($context, ZMQ_REP); | ||
| 92 | zmq_connect($responder, "tcp://${host}:${port}"); | ||
| 93 | |||
| 94 | while (1) { | ||
| 95 | # Wait for next request from client | ||
| 96 | my $size = zmq_recv($responder, my $buf, $MAX_MSGLEN); | ||
| 97 | return undef if $size < 0; | ||
| 98 | my $payload = substr($buf, 0, $size); | ||
| 99 | |||
| 100 | # Process the request | ||
| 101 | my $res = $pkg->call($payload); | ||
| 102 | |||
| 103 | # Send reply back to client | ||
| 104 | zmq_send($responder, $res, -1); | ||
| 105 | } | ||
| 106 | }; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | =head2 generate_env( $job ) | ||
| 111 | |||
| 112 | Receives the JSON payload and generates C<McBain>'s standard env | ||
| 113 | hash-ref from it. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub generate_env { | ||
| 118 | my ($self, $payload) = @_; | ||
| 119 | |||
| 120 | $payload = $json->decode($payload); | ||
| 121 | |||
| 122 | confess { code => 400, error => "Payload does not define path to invoke" } | ||
| 123 | unless $payload->{path}; | ||
| 124 | |||
| 125 | confess { code => 400, error => "Namespace must match <METHOD>:<ROUTE> where METHOD is one of GET, POST, PUT, DELETE or OPTIONS" } | ||
| 126 | unless $payload->{path} =~ m/^(GET|POST|PUT|DELETE|OPTIONS):[^:]+$/; | ||
| 127 | |||
| 128 | my ($method, $route) = split(/:/, delete($payload->{path})); | ||
| 129 | |||
| 130 | return { | ||
| 131 | METHOD => $method, | ||
| 132 | ROUTE => $route, | ||
| 133 | PAYLOAD => $payload | ||
| 134 | }; | ||
| 135 | } | ||
| 136 | |||
| 137 | =head2 generate_res( $env, $res ) | ||
| 138 | |||
| 139 | Converts the result from an API method in JSON. Read the discussion under | ||
| 140 | L</"DESCRIPTION"> for more info. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub generate_res { | ||
| 145 | my ($self, $env, $res) = @_; | ||
| 146 | |||
| 147 | $res = { $env->{METHOD}.':'.$env->{ROUTE} => $res } | ||
| 148 | unless ref $res eq 'HASH'; | ||
| 149 | |||
| 150 | return encode_json($res); | ||
| 151 | } | ||
| 152 | |||
| 153 | =head2 handle_exception( $err ) | ||
| 154 | |||
| 155 | Simply calls C<< $job->send_fail >> to return a job failed | ||
| 156 | status to the client. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | sub handle_exception { | ||
| 161 | my ($class, $err) = @_; | ||
| 162 | |||
| 163 | return $json->encode($err); | ||
| 164 | } | ||
| 165 | |||
| 166 | =head1 CONFIGURATION AND ENVIRONMENT | ||
| 167 | |||
| 168 | No configuration files are required. | ||
| 169 | |||
| 170 | =head1 DEPENDENCIES | ||
| 171 | |||
| 172 | C<McBain::WithZeroMQ> depends on the following CPAN modules: | ||
| 173 | |||
| 174 | =over | ||
| 175 | |||
| 176 | =item * L<Carp> | ||
| 177 | |||
| 178 | =item * L<ZMQ::Constants> | ||
| 179 | |||
| 180 | =item * L<ZMQ::LibZMQ3> | ||
| 181 | |||
| 182 | =item * L<JSON> | ||
| 183 | |||
| 184 | =back | ||
| 185 | |||
| 186 | =head1 INCOMPATIBILITIES WITH OTHER MODULES | ||
| 187 | |||
| 188 | None reported. | ||
| 189 | |||
| 190 | =head1 BUGS AND LIMITATIONS | ||
| 191 | |||
| 192 | Please report any bugs or feature requests to | ||
| 193 | C<bug-McBain-WithZeroMQ@rt.cpan.org>, or through the web interface at | ||
| 194 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=McBain-WithZeroMQ>. | ||
| 195 | |||
| 196 | =head1 SUPPORT | ||
| 197 | |||
| 198 | You can find documentation for this module with the perldoc command. | ||
| 199 | |||
| 200 | perldoc McBain::WithZeroMQ | ||
| 201 | |||
| 202 | You can also look for information at: | ||
| 203 | |||
| 204 | =over 4 | ||
| 205 | |||
| 206 | =item * RT: CPAN's request tracker | ||
| 207 | |||
| 208 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=McBain-WithZeroMQ> | ||
| 209 | |||
| 210 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 211 | |||
| 212 | L<http://annocpan.org/dist/McBain-WithZeroMQ> | ||
| 213 | |||
| 214 | =item * CPAN Ratings | ||
| 215 | |||
| 216 | L<http://cpanratings.perl.org/d/McBain-WithZeroMQ> | ||
| 217 | |||
| 218 | =item * Search CPAN | ||
| 219 | |||
| 220 | L<http://search.cpan.org/dist/McBain-WithZeroMQ/> | ||
| 221 | |||
| 222 | =back | ||
| 223 | |||
| 224 | =head1 AUTHOR | ||
| 225 | |||
| 226 | Ido Perlmuter <ido@ido50.net> | ||
| 227 | |||
| 228 | =head1 SEE ALSO | ||
| 229 | |||
| 230 | L<McBain> | ||
| 231 | |||
| 232 | =head1 LICENSE AND COPYRIGHT | ||
| 233 | |||
| 234 | Copyright (c) 2014, Ido Perlmuter C<< ido@ido50.net >>. | ||
| 235 | |||
| 236 | This module is free software; you can redistribute it and/or | ||
| 237 | modify it under the same terms as Perl itself, either version | ||
| 238 | 5.8.1 or any later version. See L<perlartistic|perlartistic> | ||
| 239 | and L<perlgpl|perlgpl>. | ||
| 240 | |||
| 241 | The full text of the license can be found in the | ||
| 242 | LICENSE file included with this module. | ||
| 243 | |||
| 244 | =head1 DISCLAIMER OF WARRANTY | ||
| 245 | |||
| 246 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY | ||
| 247 | FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN | ||
| 248 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES | ||
| 249 | PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER | ||
| 250 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 251 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE | ||
| 252 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH | ||
| 253 | YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL | ||
| 254 | NECESSARY SERVICING, REPAIR, OR CORRECTION. | ||
| 255 | |||
| 256 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING | ||
| 257 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR | ||
| 258 | REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE | ||
| 259 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, | ||
| 260 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE | ||
| 261 | THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING | ||
| 262 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A | ||
| 263 | FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF | ||
| 264 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF | ||
| 265 | SUCH DAMAGES. | ||
| 266 | |||
| 267 | =cut | ||
| 268 | |||
| 269 | 1; | ||
| 270 | __END__ | ||