File Coverage

blib/lib/Dancer2/Plugin/Queue.pm
Criterion Covered Total %
statement 14 30 46.6
branch 0 18 0.0
condition 0 8 0.0
subroutine 5 6 83.3
pod n/a
total 19 62 30.6


line stmt bran cond sub pod time code
1 1     1   31 use 5.008001;
  1         3  
2 1     1   5 use strict;
  1         2  
  1         16  
3 1     1   3 use warnings;
  1         2  
  1         42  
4              
5             package Dancer2::Plugin::Queue;
6             # ABSTRACT: Dancer2 plugin for message queue abstractions
7              
8             our $VERSION = '0.006';
9              
10 1     1   498 use Dancer2::Plugin;
  1         9570  
  1         7  
11 1     1   2631 use Class::Load qw/try_load_class/;
  1         8055  
  1         257  
12              
13             my %queues;
14             my $conf;
15              
16             register queue => sub {
17 0     0     my ( $dsl, $name ) = @_;
18 0   0       $conf ||= plugin_setting();
19              
20             # if name not specified, DWIM or use 'default'
21 0 0         if ( not defined $name ) {
22 0 0         if ( keys %$conf == 1 ) {
    0          
23 0           ($name) = keys %$conf;
24             }
25             elsif ( exists $conf->{default} ) {
26 0           $name = "default";
27             }
28             else {
29 0           die "Can't determine a default queue name";
30             }
31             }
32              
33             # return cached object if already created
34 0 0         return $queues{$name} if defined $queues{$name};
35              
36             # otherwise, instantiate the object from config settings
37 0 0         my $queue_conf = $conf->{$name}
38             or die "No configuration for queue '$name'";
39              
40             my $class = $queue_conf->{class}
41 0 0         or die "No class specified for queue '$name'";
42              
43 0           $class = "Dancer2::Plugin::Queue::$class";
44              
45 0 0         try_load_class($class)
46             or die "Queue class '$class' could not be loaded";
47              
48 0 0 0       $class->can('DOES') && $class->DOES("Dancer2::Plugin::Queue::Role::Queue")
49             or die "Queue class '$class' does not implement the expected role";
50              
51 0 0 0       my $object = eval { $class->new( $queue_conf->{options} || {} ) }
  0            
52             or die "Could not create $class object: $@";
53              
54 0           return $queues{$name} = $object;
55             };
56              
57             register_plugin;
58             1;
59              
60              
61             # vim: ts=4 sts=4 sw=4 et:
62              
63             __END__
64              
65             =pod
66              
67             =encoding UTF-8
68              
69             =head1 NAME
70              
71             Dancer2::Plugin::Queue - Dancer2 plugin for message queue abstractions
72              
73             =head1 VERSION
74              
75             version 0.006
76              
77             =head1 SYNOPSIS
78              
79             # in config.yml
80              
81             plugins:
82             Queue:
83             default:
84             class: Array
85             options:
86             name: not_a_real_queue
87              
88             # in your app
89              
90             use Dancer2::Plugin::Queue;
91              
92             post '/add_fortune' => sub {
93             # assume a 'fortune' parameter submitted
94             queue->add_msg( params->{fortune} );
95             # ...
96             };
97              
98             get '/tell_fortune' => sub {
99             my ($msg, $body) = queue->get_msg;
100             queue->remove_msg( $msg );
101             return "Your fortune: $body";
102             };
103              
104             =head1 DESCRIPTION
105              
106             This module provides a generic interface to a message queue. Message queue
107             implementations must implement the L<Dancer2::Plugin::Queue::Role::Queue> role,
108             which defines the interface to abstract the specifics of the backend.
109              
110             =head1 USAGE
111              
112             =head2 queue
113              
114             queue;
115             queue($name);
116              
117             This function returns a C<Dancer2::Plugin::Queue::*> object. If no C<$name> is
118             provided, it attempts to return a default object. If there is only a single
119             queue defined, it will be used as the default. If there is more than one, a
120             queue called 'default' will be the default. If there are more than one and
121             none are named 'default', an error will be thrown.
122              
123             =head2 queue->add_msg
124              
125             queue->add_msg( $data );
126              
127             Adds C<$data> to the queue. It is up to the plugin implementation (or backend)
128             to serialize or otherwise modify C<$data>.
129              
130             =head2 queue->get_msg
131              
132             ( $msg, $data ) = queue->get_msg;
133              
134             Dequeues a message from the queue. C<$msg> will be either a 'raw' message object
135             from the backend or else an identifier that can be used with C<remove_msg>.
136             C<$data> should ideally be the same as the enqueued C<$data>, subject to any
137             round-trip limitations of the backend.
138              
139             =head2 queue->remove_msg
140              
141             queue->remove_msg( $msg );
142              
143             Removes a message permanently from the queue (if not already done by C<get_msg>).
144              
145             Some message queue implementations require handled messages to be manually
146             removed from the queue or else the message will time-out and be available
147             again.
148              
149             This method should always be called after a message has been handled in case
150             the backend requires such cleanup. For implementations that do not have
151             message durability, this method may do nothing.
152              
153             =for Pod::Coverage method_names_here
154              
155             =head1 CONFIGURATION
156              
157             Queue objects are defined by a C<< NAME => HASHREF >> pair. The hash reference
158             must contain a 'class' key, whose value is a class name suffix that will be
159             appended to C<Dancer2::Plugin::Queue::>. The resulting class will be loaded on
160             demand. If the hash reference contains an 'options' key, its value will be
161             passed to the constructor when the queue object is created.
162              
163             =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
164              
165             =head1 SUPPORT
166              
167             =head2 Bugs / Feature Requests
168              
169             Please report any bugs or feature requests through the issue tracker
170             at L<https://github.com/PerlDancer/dancer2-plugin-queue/issues>.
171             You will be notified automatically of any progress on your issue.
172              
173             =head2 Source Code
174              
175             This is open source software. The code repository is available for
176             public review and contribution under the terms of the license.
177              
178             L<https://github.com/PerlDancer/dancer2-plugin-queue>
179              
180             git clone https://github.com/PerlDancer/dancer2-plugin-queue.git
181              
182             =head1 AUTHOR
183              
184             David Golden <dagolden@cpan.org>
185              
186             =head1 CONTRIBUTORS
187              
188             =for stopwords Jason A. Crome Peter Mottram
189              
190             =over 4
191              
192             =item *
193              
194             Jason A. Crome <jason@crome-plated.com>
195              
196             =item *
197              
198             Peter Mottram <peter@sysnix.com>
199              
200             =back
201              
202             =head1 COPYRIGHT AND LICENSE
203              
204             This software is Copyright (c) 2012 by David Golden.
205              
206             This is free software, licensed under:
207              
208             The Apache License, Version 2.0, January 2004
209              
210             =cut