File Coverage

blib/lib/McBain/WithZeroMQ.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package McBain::WithZeroMQ;
2              
3             # ABSTRACT: Load a McBain API as a ZeroMQ service
4              
5 1     1   36462 use warnings;
  1         3  
  1         33  
6 1     1   5 use strict;
  1         2  
  1         34  
7 1     1   1333 use 5.10.0;
  1         176  
  1         916  
8              
9 1     1   8 use Carp;
  1         171  
  1         1775  
10 1     1   2731 use JSON;
  1         32286  
  1         7  
11 1     1   632 use ZMQ::LibZMQ3;
  0            
  0            
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 turns your L API into a L,
40             making it easy to consume your APIs with ZMQ REQ clients. The generated worker code is based
41             on the L in the L. Note that the ZeroMQ request-reply pattern requires
42             a broker. The guide also has examples for L
43             and L, though two scripts based on these examples
44             are also provided with this module - more for example than actual usage - see L
45             and L, 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 key, which holds
49             the complete path of the route/method to invoke (for example, C). 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 in topic
54             C 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
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:
70              
71             =head2 init( )
72              
73             Creates the L 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'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 : 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 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 depends on the following CPAN modules:
173            
174             =over
175              
176             =item * L
177            
178             =item * L
179              
180             =item * L
181              
182             =item * L
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, or through the web interface at
194             L.
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
209            
210             =item * AnnoCPAN: Annotated CPAN documentation
211            
212             L
213            
214             =item * CPAN Ratings
215            
216             L
217            
218             =item * Search CPAN
219            
220             L
221            
222             =back
223            
224             =head1 AUTHOR
225            
226             Ido Perlmuter
227              
228             =head1 SEE ALSO
229              
230             L
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
239             and L.
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__